I am extending the JButton class and using an ImageIcon as my button background. I have also added a mouseListener so I can change the image of the button when the mouse hovers over the button or presses the button. This all works fine but the problem is that when the button is clicked a JOptionPane is displayed asking the user "Are you sure" while the option pane is displayed the buttons MouseClicked, mouseExited or mouseReleased methods are not executed so it looks like the button is stuck pressed until the use deals with the option dialog. Removing the dialog is not an option. Is there a way to get the mouse events executed before or during the dialog displaying?
Asked
Active
Viewed 183 times
0
-
While the dialog is present, the `actionPerformed` method won't "exit", which would then allow the button to return to it's normal state. You could wrap the call to the `JOptionPane` in a `SwingUtilities.invokeLater`, BUT, this would mean you would need to respond to the option dialog in a different context then the one you have now. Also, you should be monitoring the state of the button model instead, to change the state of your icons... – MadProgrammer May 03 '15 at 02:51
1 Answers
2
I have also added a mouseListener so I can change the image of the button when the mouse hovers over the button or presses the button.
Read the AbstractButton
API. There are methods that allow you to specify the Icon for these situations. For examples setPressedIcon()
.
it looks like the button is stuck pressed until the use deals with the option dialog.
Not sure if the ButtonModel state has been changed before the option pane is displayed or not. Anyway you can try using:
button.paintImmediately(...);
before the option pane is displayed. If the ButtonModel state has been updated the button should be repainted in its normal state. Note, this method should rarely be used since it can affect painting performance.

camickr
- 321,443
- 19
- 166
- 288
-
paintImmediately must be called out of EDT, [seems like as similair issue with](http://stackoverflow.com/questions/8282488/why-does-setselected-on-jcheckbox-lose-effect) – mKorbel May 03 '15 at 08:09