user1572522
user1572522

Reputation: 619

Un-Select JToggleButton

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?

Upvotes: 0

Views: 3293

Answers (1)

Sage
Sage

Reputation: 15418

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.

Upvotes: 2

Related Questions