aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Pairing a CheckboxMenuItem with a JToggleButton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Pairing a CheckboxMenuItem with a JToggleButton" Watch "Pairing a CheckboxMenuItem with a JToggleButton" New topic
Author

Pairing a CheckboxMenuItem with a JToggleButton

Jack Bernstein
Greenhorn

Joined: Jan 13, 2010
Posts: 12
I have a GUI with a CheckboxMenuItem and a JToggleButton that perform the same action. I would like to somehow link them together so that they respond to each other as well. If I check the checkbox, I want the JToggleButton to be enabled. If I unclick the JToggleButton, I'd like the checkbox to automatically be unchecked as well.

Is there a way to accomplish this without passing references to the CheckboxMenuItem and the JToggleButton to each other? It seems as though I should be able to do that in the actionPerformed() method, right? Right now I am attempting to implement the Observer design pattern, but I haven't been able to figure it out yet. Am I missing some obvious way of doing this? Surely this is something that many people have wanted to do before.

Thanks for your help!
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10266
    
    8

You can make them share the same AbstractAction and toggle the selection/state in the actionPerformed


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Jack Bernstein
Greenhorn

Joined: Jan 13, 2010
Posts: 12
Well yes, that is what I'd like to do. Is it possible to toggle both the state of the button and that of the checkbox within that same AbstractAction? Can the AbstractAction determine whether it was the checkbox or the button that called it?

This code works if I assume the checkbox called the action:


And this code works if I assume the button called the action:


I know how to do the action regardless of which button calls it--just by using a boolean to say whether it is currently turned on or off--but I guess what I don't know is how to check the box if the button is what was selected, or how to deselect the button if the checkbox was unchecked. Does that make sense?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
code everything to one or the other eg the toggleButton - add the actionListener and in the code have

if(toggleButton.isSelected()) doSomething();
or
if(toggleButton.isSelected()) doSomething();
else doSomethingElse();

this one line will link them

checkBoxMenuItem.setModel(toggleButton.getModel());
Jack Bernstein
Greenhorn

Joined: Jan 13, 2010
Posts: 12
So I did this and it does link them together. So now I'm getting the behavior I want (when the box is checked, the button is automatically selected, etc.). The only problem is that now my AbstractAction no longer works. So pushing the button or checking the box no longer does anything. Is there a particular order for how this command is supposed to be used?
Jack Bernstein
Greenhorn

Joined: Jan 13, 2010
Posts: 12
If it's helpful, here's my code:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pairing a CheckboxMenuItem with a JToggleButton