Draw round Button in LookAndFeel-Style - java

I'm experimenting with JRadioButton's to put them on a JToolbar and select the last one clicked. If i'd use JButtons they wouldn't keep the Selection.
Since JRadioButton always have that Dot, I need to draw them myself by overriding the paint-methods.
The Button's will be circles with an Icon in it. That works if I draw Images, but looks aweful. The problem I have is that I would like to draw the circle so that these Buttons always look like the JButtons with the current LookAndFeel.
How can i do that? I searched for a while now, but I didn't find methods to read some default-colors of the LookAndFeel which I could use.
So how can i read Background-Colors etc. of the current LookAndFeel to use it for some custom Button-Drawing?

So how can i read Background-Colors etc. of the current LookAndFeel to use it for some custom Button-Drawing?
See UIManager Defaults.
I need to draw them myself by overriding the paint-methods
Don't do custom painting in the component. If you don't like the default Icons, then create your own Icon and do the custom painting there or create an Image and use an ImageIcon. The you can use the setXXXIcon() methods.

Related

Using an image instead of JDialog/frame to hold swing components?

I currently have a JDialog (class that implements JDialog and is constructed like a jframe), and has 3 swing buttons placed on it. Currently I have it set, undecorated = true, to hide the outer frame. Is there any way to use my image to replace the default square frame?
This is what I aim for :
The blue square with shadow is the pre made image.
Regards
The blue square with shadow is the pre made image.
Well, the best way would be to set the background of the panel and then add a ShadowBorder to the panel. This will provide you with far more flexibility in the future as you can create many panels with different colors and reuse the same ShadowBorder instead of having to create an Image every time. I don't have an example of a ShadowBorder, but you might find one if you search the web.
Is there any way to use my image to replace the default square frame?
But if you really want to use your premade Image, then you can just:
create a JLabel and add your Image to the label as an Icon
add the label to the dialog
set the layout manager of the label
add your components to the label.

How can I change colors of components when the mouse is pressed in a JFrame in Java?

I am coding a piano in java using rectangles from the java.awt.graphics library. I am using a mouselistener to play the sound of each individual key when the mouse clicks a certain area on the JFrame.
How would I add a shape to the panel upon clicking, repaint, then repaint the keyboard back over top when the user releases the mouse?
Consider adding JLabels to a JPanel that uses GridLayout. Give each JLabel a MouseListener and either swap ImageIcons on mousePress/mouseRelease or change the JLabel's background with press and release. If you go the latter route, you'll want to make sure that the JLabels opaque property is set to true so that the background colors show.
Then for the black keys, you can add the above JPanel to a JLayeredPane and on top of this, add another JPanel that holds the black keys that function in the same way.
Also, you'll want to take care to "play" any notes in a background thread, such as can be obtained with a SwingWorker so as not to tie up the Swing event thread and completely freeze your program.
Consider solution: source
It might not be exactly what you're after, but it might give you an idea of how to approach your problem. It took me a long time to figure out how to use JLayeredPane without setting a null layout, but in the end this was the best I could come up with. Also, assumed some naming conventions for your sound files. :p

Line graphical component

I want to draw a line between different components in a JPanel, but the line should be a component, ie, it can be clicked, highlighted, selected and deleted instead of just painting a line in a panel, is there anything like this or I must implement it by myself. And if I must implement it, how?
You could use a JSeparator. But you'll have to implement the click, highlighting, selection and deletion yourself. A JSeparator is just use to... separate sections in a panel.
If you mean that all these operations should be available when designing your GUI in a wysiwyg editor like NetBeans Matisse, then JSeparator is just what you need.
I tried to use prepared things like JSeparator, But I found the best way by myself and I implement it. I used a JLayeredPane for my container. I add my own JPanel behind the all layers and override its paint() method. in paint() method I used Java2D to draw a curve between Components on higher layers in JLayeredPane. You can see the result in below.

Swing Custom GUI Component

I need to create a custom GUI Component about same like shown in the following image.
it has some buttons and labels on this.
How can i create like this
You could use a JWindow, with an Image for the background picture. For the buttons, use a JButton with an ImageIcon.
You can have a JFrame with nullLayout then use a JLabel with the image.
Now for buttons use setContentAreaFilled(false) and setBorderPainted(false) to remove the default button style and it will look exactly like the image that you have passed while creating the button.
To position the Frame at the center use setLocationRelativeTo(null) .
I think that should solve your problem.

Java Swing group of Jbuttons without raised portion

I wanted to know how to display a group of JButtons to look like smooth panel without raised portion.
thanks
button.setBorder(null);
You may want to look at some of the other "setXXX" method that control painting as well.
I've often just used standard JLabels and added mouseListeners to make them clickable. Alternatively, you could get more advanced and create your own ButtonUI class if you want to really fine-grained control over the rendering of the buttons.
If you want the buttons to be in a row, you can put them in a JToolBar and set Rollover to true. This will make flat buttons that, with mouse over, look raised.

Categories