Syncing JMenu ButtonGroups with JToolbar ButtonGroups

Imagine I'm making a simple word processor with Java Swing. I have a set of actions written to perform text alignment. On the MenuBar, I have a menu:

View
  Left justify
  Center jusitfy
  Right justify

This consists of JRadioButtonMenuItems and ButtonGroup to ensure that only one item is selected at any time.

Also, imagine that I have an equivalent toolbar consisting of JToggleButtons and again ButtonGroup to ensure that only one button can be active at any time.

Left alignment JRadioButtonMenu and JToggleButton are initialized using the same Action, etc. with other elements.

My question is: what is the best method for synchronizing two groups? If I click "Right Justify" on the toolbar, I want the group in the menu to be updated accordingly, and vice versa.

+5
source share
5 answers

After much searching, I found the information here . Basically, you can add this to your actionPerformed method action:

action.putValue(Action.SELECTED_KEY, Boolean.TRUE);

And that will do all the work for you!

Unfortunately, the official Sun tutorials do not cover this aspect (or at least I did not notice it), therefore, the difficulty in defining such a simple approach to solving my problem.

+2
source

. , - . . , () .

, , , .

+1

0

( ), 1 . . 1 , .

0

All Articles