Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to use Actions to create menus, toolbars, and other components in Java

The naive way of writing building a menu in a Java Swing app is to do something like:

JMenu fileMenu = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open...");
openItem.addActionListener(new ActionListener() { /* action listener stuff */ } )
fileMenu.addMenuItem(openItem);

A more experienced developer will recognize that actions can be accessed through a variety of mechanisms - menus, toolbar buttons, maybe even other workflows in the system. That person is more likely to write:

Action openAction = new AbstractAction();
openAction.setName("Open...");
openAction.addActionListener(new ActionListener() { /* action listener stuff */ } )
...
JMenuItem openItem = new JMenuItem(openAction);

My question is, what is the best way to manage these Action objects so they can be used across menus, toolbars, etc?

  • Create a factory class that returns specific actions?
  • Declare all of the actions as private static final Action in some utility class?
  • Take advantage of a Java application framework?
  • Something else?
like image 253
David Koelle Avatar asked Jan 23 '09 16:01

David Koelle


People also ask

How do I create a toolbar in Java?

In order to create a toolbar in Java Swing, you use JToolBar class. The JToolbar class supports two orientations: vertical and horizontal. You use the orientation attribute to maintain the current orientation of the toolbar. You can add any component to the toolbar including a button, combobox, and menu.

What are the menus and toolbars in Java?

Menus group commands that we can use in an application. Toolbars provide a quick access to the most frequently used commands. In Java Swing, the JToolBar class creates a toolbar in an application.


2 Answers

Applications that I have developed that need to use that same actions across menus, toolbars, and other buttons have been done using Swing Application Framework.

Swing Application Framework

This framework will allow you to have a resource file where you can define all menu text, tooltips, and ICONS. I think the icons are the key, you do not have to load them yourself. Also, if you have any actions that you need to enable/disable you can override the method to control its state.

The website is worth the read.

like image 163
ShawnD Avatar answered Oct 06 '22 00:10

ShawnD


You can group all your abstractAction using the dedicated Map javax.swing.actionmap . See http://java.sun.com/javase/6/docs/api/javax/swing/ActionMap.html

Moreover each JComponent has an internal actionMap (getActionMap()).

class MyComponent
extends JPanel
{
public static final String ACTION_NAME1="my.action.1";

public MyComponent()
 {
 AbstractAction action= new AbstractAction() { ... }
 getActionMap().put(ACTION_NAME1,action);
...

 menu.add(getActionMap().get(ACTION_NAME1));
 }

}

Hope it helps

like image 38
Pierre Avatar answered Oct 06 '22 01:10

Pierre