Using an Action

This example creates an Action for both a menu item and a toolbar, displaying both components and allowing the user to click on either one. When the components are clicked, the actionPerformed( ) method of the action is called. Don’t worry if you don’t understand all the methods behind the toolbar or the menu; these classes are discussed later. For now, it is important to see that selecting either one performs the action.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class ActionExample extends JPanel {

	public JMenuBar menuBar;
	public JToolBar toolBar;

	public ActionExample() {

		// Create a menu bar and give it a bevel border.
		menuBar = new JMenuBar();
		menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));

		// Create a menu and add it to the menu bar.
		JMenu menu = new JMenu("Menu");

		// Create a toolbar and give it an etched border.
		toolBar = new JToolBar();
		toolBar.setBorder(new EtchedBorder());

		// Instantiate a sample action with the NAME property of "Download" and
		// the
		// appropriate SMALL_ICON property.
		SampleAction exampleAction = new SampleAction("Download",
				new ImageIcon("action.gif"));

		// Finally, add the sample action to the menu and the toolbar. These
		// methods
		// are no longer preferred:
		// menu.add(exampleAction);
		// toolBar.add(exampleAction);
		// Instead, you should create actual menu items and buttons:
		JMenuItem exampleItem = new JMenuItem(exampleAction);
		JButton exampleButton = new JButton(exampleAction);

	class SampleAction extends AbstractAction {
		// This is our sample action. It must have an actionPerformed( ) method,
		// which
		// is called when the action should be invoked.
		public SampleAction(String text, Icon icon) {
			super(text, icon);

		public void actionPerformed(ActionEvent e) {
			System.out.println("Action [" + e.getActionCommand()
					+ "] performed!");

	public static void main(String s[]) {
		ActionExample example = new ActionExample();
		JFrame frame = new JFrame("Action Example");
		frame.getContentPane().add(example.toolBar, BorderLayout.NORTH);
		frame.setSize(200, 200);

You may also like...