MenuBar, menu item, action, icon accelerator : Menu « Swing JFC « Java






MenuBar, menu item, action, icon accelerator

MenuBar, menu item, action, icon accelerator
 
/*
   This program is a part of the companion code for Core Java 8th ed.
   (http://horstmann.com/corejava)

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;

/**
 * @version 1.23 2007-05-30
 * @author Cay Horstmann
 */
public class MenuTest
{
   public static void main(String[] args)
   {
      EventQueue.invokeLater(new Runnable()
         {
            public void run()
            {
               MenuFrame frame = new MenuFrame();
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               frame.setVisible(true);
            }
         });
   }
}

/**
 * A frame with a sample menu bar.
 */
class MenuFrame extends JFrame
{
   public MenuFrame()
   {
      setTitle("MenuTest");
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

      JMenu fileMenu = new JMenu("File");
      fileMenu.add(new TestAction("New"));

      // demonstrate accelerators

      JMenuItem openItem = fileMenu.add(new TestAction("Open"));
      openItem.setAccelerator(KeyStroke.getKeyStroke("ctrl O"));

      fileMenu.addSeparator();

      saveAction = new TestAction("Save");
      JMenuItem saveItem = fileMenu.add(saveAction);
      saveItem.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));

      saveAsAction = new TestAction("Save As");
      fileMenu.add(saveAsAction);
      fileMenu.addSeparator();

      fileMenu.add(new AbstractAction("Exit")
         {
            public void actionPerformed(ActionEvent event)
            {
               System.exit(0);
            }
         });

      // demonstrate check box and radio button menus

      readonlyItem = new JCheckBoxMenuItem("Read-only");
      readonlyItem.addActionListener(new ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               boolean saveOk = !readonlyItem.isSelected();
               saveAction.setEnabled(saveOk);
               saveAsAction.setEnabled(saveOk);
            }
         });

      ButtonGroup group = new ButtonGroup();

      JRadioButtonMenuItem insertItem = new JRadioButtonMenuItem("Insert");
      insertItem.setSelected(true);
      JRadioButtonMenuItem overtypeItem = new JRadioButtonMenuItem("Overtype");

      group.add(insertItem);
      group.add(overtypeItem);

      // demonstrate icons

      Action cutAction = new TestAction("Cut");
      cutAction.putValue(Action.SMALL_ICON, new ImageIcon("cut.gif"));
      Action copyAction = new TestAction("Copy");
      copyAction.putValue(Action.SMALL_ICON, new ImageIcon("copy.gif"));
      Action pasteAction = new TestAction("Paste");
      pasteAction.putValue(Action.SMALL_ICON, new ImageIcon("paste.gif"));

      JMenu editMenu = new JMenu("Edit");
      editMenu.add(cutAction);
      editMenu.add(copyAction);
      editMenu.add(pasteAction);

      // demonstrate nested menus

      JMenu optionMenu = new JMenu("Options");

      optionMenu.add(readonlyItem);
      optionMenu.addSeparator();
      optionMenu.add(insertItem);
      optionMenu.add(overtypeItem);

      editMenu.addSeparator();
      editMenu.add(optionMenu);

      // demonstrate mnemonics

      JMenu helpMenu = new JMenu("Help");
      helpMenu.setMnemonic('H');

      JMenuItem indexItem = new JMenuItem("Index");
      indexItem.setMnemonic('I');
      helpMenu.add(indexItem);

      // you can also add the mnemonic key to an action
      Action aboutAction = new TestAction("About");
      aboutAction.putValue(Action.MNEMONIC_KEY, new Integer('A'));
      helpMenu.add(aboutAction);

      // add all top-level menus to menu bar

      JMenuBar menuBar = new JMenuBar();
      setJMenuBar(menuBar);

      menuBar.add(fileMenu);
      menuBar.add(editMenu);
      menuBar.add(helpMenu);

      // demonstrate pop-ups

      popup = new JPopupMenu();
      popup.add(cutAction);
      popup.add(copyAction);
      popup.add(pasteAction);

      JPanel panel = new JPanel();
      panel.setComponentPopupMenu(popup);
      add(panel);

      // the following line is a workaround for bug 4966109
      panel.addMouseListener(new MouseAdapter()
         {
         });
   }

   public static final int DEFAULT_WIDTH = 300;
   public static final int DEFAULT_HEIGHT = 200;

   private Action saveAction;
   private Action saveAsAction;
   private JCheckBoxMenuItem readonlyItem;
   private JPopupMenu popup;
}

/**
 * A sample action that prints the action name to System.out
 */
class TestAction extends AbstractAction
{
   public TestAction(String name)
   {
      super(name);
   }

   public void actionPerformed(ActionEvent event)
   {
      System.out.println(getValue(Action.NAME) + " selected.");
   }
}

   
  








Related examples in the same category

1.Simple MenusSimple Menus
2.Creating popup menus with SwingCreating popup menus with Swing
3.Create a main menu.
4.Creating a menubar
5.Creating a JMenuBar, JMenu, and JMenuItem Component
6.Submenus, checkbox menu items, swapping menus,mnemonics (shortcuts) and action commandsSubmenus, checkbox menu items, swapping menus,mnemonics (shortcuts) and action commands
7.Separating Menu Items in a Menu
8.This example create a menubar and toolbar both populated with ActionThis example create a menubar and toolbar both populated with Action
9.A simple example of JPopupMenuA simple example of JPopupMenu
10.A quick demonstration of checkbox menu itemsA quick demonstration of checkbox menu items
11.Building menus and menu items: Accelerators and mnemonicsBuilding menus and menu items: Accelerators and mnemonics
12.An example of the JPopupMenu in actionAn example of the JPopupMenu in action
13.A simple example of constructing and using menus.A simple example of constructing and using menus.
14.PopupMenu and Mouse EventPopupMenu and Mouse Event
15.Menu YMenu Y
16.Menu XMenu X
17.Action MenuAction Menu
18.Menu Action Screen Dump DemoMenu Action Screen Dump Demo
19.Toggle Menu ItemToggle Menu Item
20.CheckBox Menu SampleCheckBox Menu Sample
21.Menu Demo 4Menu Demo 4
22.Comprehensive Menu DemoComprehensive Menu Demo
23.Menu Sample 3Menu Sample 3
24.How to create customized menuHow to create customized menu
25.Radio menu itemRadio menu item
26.React to menu action and checkbox menuReact to menu action and checkbox menu
27.Menu created from property file
28.Simple Menu and Window interface - not InternationalizedSimple Menu and Window interface - not Internationalized
29.Provide a pop-up menu using a FrameProvide a pop-up menu using a Frame
30.Demonstrate Menus and the MenuBar classDemonstrate Menus and the MenuBar class
31.Demonstrate JMenu shortcuts and accelerators
32.Demonstrate Cascading MenusDemonstrate Cascading Menus
33.Popup Menu DemoPopup Menu Demo
34.Menu DemoMenu Demo
35.Menu Layout DemoMenu Layout Demo
36.Actions MenuBarActions MenuBar
37.UseActions: MenuUseActions: Menu
38.PopupMenu SamplePopupMenu Sample
39.RadioButton Menu SampleRadioButton Menu Sample
40.Menu Look Demo, except the menu items actually doMenu Look Demo, except the menu items actually do
41.Menu Glue DemoMenu Glue Demo
42.Add PopupMenuAdd PopupMenu
43.Listening for Changes to the Currently Selected Menu or Menu Item
44.Menu item that can be selected or deselected
45.Place commands that hide/show various toolbars
46.Getting the Currently Selected Menu or Menu Item
47.Creating a Menu Item That Listens for Changes to Its Selection Status
48.Create a change listener and register with the menu selection manager
49.Menu Selection Manager DemoMenu Selection Manager Demo
50.Color Menu