Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making Image button look pressed/clicked in Swing

I created a JButton which has an image set as an icon representing the button. Now, I've set

setContentAreaFilled(false);
setBorderPainted(false);
setOpaque(false);

the properties which this makes an image-button look nice with no borders or background colors etc.

Now the problem is that when I click on the image, it shows no onclick effects of a button, so when there is some background processing involved or some event is bring fired on click of button which takes time to process the request and return something, it gives an impression that the button was not clicked and the user might keep clicking it.

How do I make a button which has an icon set with the above mentioned properties and yet I have some control over setting the onpress or onmouseover events.

like image 898
Rohan Avatar asked Jul 05 '12 20:07

Rohan


People also ask

How do I make an image a button in Java?

To add icon to a button, use the Icon class, which will allow you to add an image to the button. Icon icon = new ImageIcon("E:\editicon. PNG"); JButton button7 = new JButton(icon); Above, we have set icon for button 7.

How do you change the button shape in a swing?

You can use customize your button by adding a border which will change its shape such as rounded or circular..the following code creates a rounded border..you can draw a circle to get a circular button class RoundedBorder extends AbstractBorder { public void paintBorder(Component c, Graphics g, int x, int y, int width, ...

What is swing button?

Swing defines four types of buttons: JButton, JToggleButton, JCheckBox, and JRadioButton. All are subclasses of the AbstractButton class, which extends JComponent. Thus, all buttons share a set of common traits. AbstractButton contains many methods that allow you to control the behavior of buttons.


1 Answers

I found the answer: setRolloverIcon() and setPressedIcon() will do the magic.

like image 79
Rohan Avatar answered Nov 12 '22 21:11

Rohan