Java – how to change JButton icon when selecting?

I want to change the JButton icon when the user presses / releases the button

Icon image when JButton is selected

Icon image when deselecting JButton and releasing JButton:

How can I do this?

Solution

JButton b = new JButton(Icon x); // Create button with normal icon
JButton b = new JButton(Icon x); // Create button with normal icon
   b.setIcon(Icon x);
   b.setDisabledIcon(Icon x);
   b.setPressedIcon(Icon x);
   b.setSelectedIcon(Icon x);
   b.setDisabledSelectedIcon(Icon x);

reference resources: http://www.leepoint.net/notes-java/GUI/components/20buttons/23buttonicons.html

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>