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.
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?
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?
Joined: Jan 13, 2010
If it's helpful, here's my code:
subject: Pairing a CheckboxMenuItem with a JToggleButton