import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class ActionExample extends JFrame { public JMenuBar menuBar; public JToolBar toolBar; private SampleAction exampleAction; public ActionExample() { // Create a menu bar and give it a bevel border menuBar = new JMenuBar(); // Create a menu and add it to the menu bar JMenu menu = new JMenu("Menu"); menuBar.add(menu); // Create a toolbar and give it an etched border toolBar = new JToolBar(); // Instantiate a sample action with the NAME property of // "Download" and the appropriate SMALL_ICON property. exampleAction = new SampleAction("Download", new ImageIcon("action.gif")); // Finally, add the sample action to the menu and the toolbar. menu.add(new JMenuItem(exampleAction)); toolBar.add(new JButton(exampleAction)); //Create a button that disables the action. JButton b = new JButton("disable"); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { exampleAction.setEnabled(false); } }); toolBar.addSeparator(); toolBar.add(b); //Assemble the frame and display it. addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setJMenuBar(menuBar); getContentPane().add(toolBar, BorderLayout.NORTH); setSize(200,200); setVisible(true); } 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[]) { new ActionExample(); } }