Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

jtogglebutton and jcheckboxmenuitem  RSS feed

 
Leonardo Penha
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
I have a JToggleButton and a JCheckboxMenuItem. They have both been created with the same Action parameter. The action simply shows/hides a panel.
I would like to know what is the best way to update each control's state when the other control is clicked. For example: when the user clicks the JCheckboxMenuItem the JToggleButton's state has to change from unselected to selected and vice-versa.
The same is should be true if the user clicks on the JToggleButton.
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
let's assume, you want this feature also in the future. So simply create a new class (ButtonSelectionGroup). This contains a Vector as a member, where you can add only AbstractButtons.
So set the member as private access and add a public method addButton(AbstractButton b) to it.
In this method you first add the button to your vector and then you add a ActionListener to the button.
Everytime, when this action Listener is activated, you cast the calling object to an Abstract button. This button you can ask for it's selection state and then you go through your vector and set all contained elements to this state.
In your class, where you create your GUI, you simply add all elements, which belong together to an instance of the new class.
That's it. Ok it is a bit work, but you can reuse it wherever you want. Maybe you add your class to the saloon, so everybody can reuse it.
Greetings
Rene
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!