Java – swing: link toggle buttons and button groups, and corresponding menu items
For a school project, I need to make a simple painting application that can draw lines, ellipses and rectangles
Assign toolbar buttons and menu items that specify each type of shape I need
I want to make a little improvement through the button jtogglebuttons in the toolbar and the menu item jradiobuttonmenuitems In addition, I want it so that when you select a toolbar button, it deselects the other toolbar buttons, selects the corresponding menu item, and deselects the other menu items Same as selecting one of the menu items
I know I can use buttongroup to group any abstractbuttons, but I'm not sure if this is the right way, because although it handles one group button, I'm not sure it can handle two parallel groups
Without buttongroup, this means that in each of the six event listeners, I will have to manually deselect the other buttons, and each pair will call the same code to set the shape type
I can also make two button groups, one for the menu and one for the toolbar, but I also have to copy the shape type setting code
In either case, I also risk a button of menu setting, set a menu item, set a button, advertising infintum
What is the best way to solve this problem?
(bonus points can solve the problems of NetBeans GUI designers; it's easier)
Solution
As described in how to use actions, the action interface is an effective method "if you have two or more components that perform the same function" In particular, action will allow your buttons and menu items to use the same code
Appendix: the following example shows how JMenu and jtoolbar can share the same action for each of multiple files
import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.io.File; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JToolBar; /** @see https://stackoverflow.com/questions/4038605 */ public class FileMenu { public static void main(String[] args) { EventQueue.invokelater(new Runnable() { public void run() { new FileMenu().create(); } }); } void create() { File userDir = new File(System.getProperty("user.dir")); File[] files = userDir.listFiles(); JMenu menu = new JMenu("Recent Files"); JToolBar toolBar = new JToolBar(JToolBar.VERTICAL); JLabel label = new JLabel(" ",JLabel.CENTER); for (File f : files) { if (f.isFile() && !f.isHidden()) { RecentFile rf = new RecentFile(f,label); menu.add(new JMenuItem(rf.getAction())); toolBar.add(rf.getAction()); } } JMenuBar menuBar = new JMenuBar(); menuBar.add(menu); JFrame f = new JFrame("FileMenu"); f.setJMenuBar(menuBar); f.add(toolBar,BorderLayout.CENTER); f.add(label,BorderLayout.soUTH); f.pack(); f.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); f.setLocationRelativeTo(null); f.setVisible(true); } } class RecentFile extends AbstractAction { private final File file; private final JLabel label; public RecentFile(final File file,final JLabel label) { this.file = file; this.label = label; this.putValue(Action.NAME,file.getName()); this.putValue(Action.SHORT_DESCRIPTION,file.getAbsolutePath()); } public void actionPerformed(ActionEvent e) { label.setText(file.getName()); } public Action getAction() { return this; } }