• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pairing a CheckboxMenuItem with a JToggleButton

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can make them share the same AbstractAction and toggle the selection/state in the actionPerformed
 
Jack Bernstein
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it's helpful, here's my code:

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic