How to link JToggleButton and JCheckBoxMenuItem in Java Swing?

How can i do this? Now I will try using 2 ChangeListener and 2 ActionListeners, but this will not work. When I click the button, the checkbox from MenuItem should be checked ...

thanks

+4
source share
2 answers
  • a common element is a common action
  • the associated property is the value for SELECTED_KEY

in code, something like

Action action = new AbstractAction("shared toggle") { @Override public void actionPerformed(ActionEvent e) { LOG.info("pressed"); } }; action.putValue(Action.SELECTED_KEY, Boolean.TRUE); JPopupMenu menu = new JPopupMenu(); menu.add(new JCheckBoxMenuItem(action)); JComponent box = new JPanel(); box.setComponentPopupMenu(menu); box.add(new JToggleButton(action)); box.add(new JCheckBox(action)); 
+4
source

You may have the same ButtonModel . setModel and getModel defined in AbstractButton .

+1
source

Source: https://habr.com/ru/post/1403005/


All Articles