Débora
Débora

Reputation: 5952

Change JButton's icon on mouse pressed

There are couple of JButton in one JPanel. When the user clicks one Jbutton, it is required to be shown as pressed even after mouse is released. I tried

jButton.setPressedIcon( pressedImageIcon );  

But it supports only until the mouse is being pressed. When mouse is released, the button's default icon appears. I want to change the icon of the JButton when pressed and keep the pressed icon as it is, so that user knows what he has clicked and icon is needed to be changed when another JButton is clicked. Is there any way to do this ?

Upvotes: 2

Views: 275

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

It sounds like you you shouldn't be using a JButton at all, but rather you should be using a JToggleButton, one that stays selected after pressing, until pressed again. You would want to set the selected icon via setSelectedIcon(...) to be what you wish to have shown when it is depressed or selected.

...so that user knows what he has clicked and icon is needed to be changed when another JButton is clicked.

To get this behavior, add your JToggleButtons to a single ButtonGroup object, similar to how you would do this for JRadioButtons.

Upvotes: 2

Related Questions