0

I have a JToggleButton, not in a group, and if it's pressed, I want to be able to Un-Select it if I press another JButton.

I've tried using:

toggleButton.setSelected(false);
toggleButton.doClick();

but neither un-Select the toggle button, it stays "highlighted".

What can I do so that the toggle button goes back to the normal un-Selected state, like if I pressed it again?

Is it a matter of calling the above while in the UI Thread?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user1572522
  • 569
  • 2
  • 11
  • 26

1 Answers1

1

jToggleButton.doClick(): Programmatically perform a "click". This does the same thing as if the user had pressed and released the button.

     jToggleButton1.setSelected(false);
        jToggleButton1.doClick();

If you execute this code subsequently, it is actually doing nothing. Because, as soon as the first line set the jToggleButton1 as unselected second line set it as selected. If you want just jToggleButton to be unselected use jToggleButton1.setSelected(false) by removing doClick(). But if you want to toggle among selected and deselected using your other JButton click, use jToggleButton1.doClick() only.

Sage
  • 15,290
  • 3
  • 33
  • 38
  • Sorry. I didn't use them together, I should have said that I tired each one, and neither worked. I just tried using doClick(), again and it looked like it worked. Thanks – user1572522 Oct 26 '13 at 01:40