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.
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.
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, ...
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.
I found the answer: setRolloverIcon()
and setPressedIcon()
will do the magic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With