Example usage for javax.swing JMenu JMenu

List of usage examples for javax.swing JMenu JMenu

Introduction

In this page you can find the example usage for javax.swing JMenu JMenu.

Prototype

public JMenu(Action a) 

Source Link

Document

Constructs a menu whose properties are taken from the Action supplied.

Usage

From source file:MenuTest.java

public MenuTest() {
    super();/*  ww w  .  j  a  v a 2 s .c o m*/

    MenuListener listener = new MenuListener() {
        public void menuCanceled(MenuEvent e) {
            dumpInfo("Canceled", e);
        }

        public void menuDeselected(MenuEvent e) {
            dumpInfo("Deselected", e);
        }

        public void menuSelected(MenuEvent e) {
            dumpInfo("Selected", e);
        }

        private void dumpInfo(String s, MenuEvent e) {
            JMenu menu = (JMenu) e.getSource();
            System.out.println(s + ": " + menu.getText());
        }
    };

    JMenu fileMenu = new JMenu("File");
    fileMenu.addMenuListener(listener);
    fileMenu.add(new JMenuItem("Open"));
    fileMenu.add(new JMenuItem("Close"));
    fileMenu.add(new JMenuItem("Exit"));
    JMenu helpMenu = new JMenu("Help");
    helpMenu.addMenuListener(listener);
    helpMenu.add(new JMenuItem("About MenuTest"));
    helpMenu.add(new JMenuItem("Class Hierarchy"));
    helpMenu.addSeparator();
    helpMenu.add(new JCheckBoxMenuItem("Balloon Help"));
    JMenu subMenu = new JMenu("Categories");
    subMenu.addMenuListener(listener);
    JRadioButtonMenuItem rb;
    ButtonGroup group = new ButtonGroup();
    subMenu.add(rb = new JRadioButtonMenuItem("A Little Help", true));
    group.add(rb);
    subMenu.add(rb = new JRadioButtonMenuItem("A Lot of Help"));
    group.add(rb);
    helpMenu.add(subMenu);
    JMenuBar mb = new JMenuBar();
    mb.add(fileMenu);
    mb.add(helpMenu);
    setJMenuBar(mb);
}

From source file:MainClass.java

MainClass(String title) {
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JToolBar toolBar = new JToolBar();
    Action a = new AbstractAction("Demo") {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Action taken.");
        }//from ww w .j a v  a 2s.c  om
    };

    JButton b = toolBar.add(a);
    b.setText("Demo Button");
    b.setToolTipText("Press me to take action.");

    JMenu mainMenu = new JMenu("Menu");
    JMenuItem mi = mainMenu.add(a);
    mi.getAccessibleContext().setAccessibleName("Menu item");

    JMenuBar mb = new JMenuBar();
    mb.add(mainMenu);
    setJMenuBar(mb);

    JPanel pane = new JPanel();
    pane.setLayout(new BorderLayout());
    pane.setPreferredSize(new Dimension(200, 100));
    pane.add(toolBar, BorderLayout.NORTH);
    setContentPane(pane);

    pack();
    setVisible(true);
}

From source file:IntroExample.java

public IntroExample() {

    JMenu fileMenu = new JMenu("File");
    JMenu editMenu = new JMenu("Edit");
    JMenu otherMenu = new JMenu("Other");
    JMenu subMenu = new JMenu("SubMenu");
    JMenu subMenu2 = new JMenu("SubMenu2");

    //  Assemble the File menus with mnemonics
    ActionListener printListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.out.println("Menu item [" + event.getActionCommand() + "] was pressed.");
        }//from  w  w w .  j av a 2s .  c om
    };
    for (int i = 0; i < fileItems.length; i++) {
        JMenuItem item = new JMenuItem(fileItems[i], fileShortcuts[i]);
        item.addActionListener(printListener);
        fileMenu.add(item);
    }

    //  Assemble the File menus with keyboard accelerators
    for (int i = 0; i < editItems.length; i++) {
        JMenuItem item = new JMenuItem(editItems[i]);
        item.setAccelerator(KeyStroke.getKeyStroke(editShortcuts[i],
                Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false));
        item.addActionListener(printListener);
        editMenu.add(item);
    }

    //  Insert a separator in the Edit Menu in Position 1 after "Undo"
    editMenu.insertSeparator(1);

    //  Assemble the submenus of the Other Menu
    JMenuItem item;
    subMenu2.add(item = new JMenuItem("Extra 2"));
    item.addActionListener(printListener);
    subMenu.add(item = new JMenuItem("Extra 1"));
    item.addActionListener(printListener);
    subMenu.add(subMenu2);

    //  Assemble the Other Menu itself
    otherMenu.add(subMenu);
    otherMenu.add(item = new JCheckBoxMenuItem("Check Me"));
    item.addActionListener(printListener);
    otherMenu.addSeparator();
    ButtonGroup buttonGroup = new ButtonGroup();
    otherMenu.add(item = new JRadioButtonMenuItem("Radio 1"));
    item.addActionListener(printListener);
    buttonGroup.add(item);
    otherMenu.add(item = new JRadioButtonMenuItem("Radio 2"));
    item.addActionListener(printListener);
    buttonGroup.add(item);
    otherMenu.addSeparator();
    otherMenu.add(item = new JMenuItem("Potted Plant", new ImageIcon("image.gif")));
    item.addActionListener(printListener);

    //  Finally, add all the menus to the menu bar
    add(fileMenu);
    add(editMenu);
    add(otherMenu);
}

From source file:MenuActionExample.java

public MenuActionExample() {
    super(true);/*from  w  w  w. j  a va2s . c  o m*/

    // 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");
    menuBar.add(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);
    menu.add(exampleItem);
    toolBar.add(exampleButton);
}

From source file:Main.java

public JMenuBar createMenuBar() {
    JMenuBar top_menu_bar = new JMenuBar();
    JMenu main_menu = new JMenu("Menu");
    main_menu.setMnemonic(KeyEvent.VK_M);
    top_menu_bar.add(main_menu);/*from   www.j a v  a2  s  .c  om*/
    JMenuItem menu_item;

    menu_item = new JMenuItem("Add New");
    menu_item.setMnemonic(KeyEvent.VK_N);
    menu_item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK));
    menu_item.setActionCommand("new");
    menu_item.addActionListener(e -> createThumb());
    main_menu.add(menu_item);
    return top_menu_bar;
}

From source file:QandE.MyDemo2.java

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.// www . jav a 2s.c  o m
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("MyDemo2");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenu menu = new JMenu("Menu");
    JMenuBar mb = new JMenuBar();
    mb.add(menu);
    frame.setJMenuBar(mb);

    //Add a label with bold italic font.
    JLabel label = new JLabel("My Demo");
    frame.getContentPane().add(BorderLayout.CENTER, label);
    if (true) {
        label.setFont(label.getFont().deriveFont(Font.ITALIC | Font.BOLD));
    } else {
        //another way of doing it, but not as appropriate since 
        //setFont is faster than using HTML.
        label.setText("<html><i>My Demo</i></html>");
    }
    label.setHorizontalAlignment(JLabel.CENTER);

    //Display the window, making it a little bigger than it really needs to be.
    frame.pack();
    frame.setSize(frame.getWidth() + 100, frame.getHeight() + 50);
    frame.setVisible(true);
}

From source file:LookAndFeelPrefs.java

/**
 * Create a menu of radio buttons listing the available Look and Feels. When
 * the user selects one, change the component hierarchy under frame to the new
 * LAF, and store the new selection as the current preference for the package
 * containing class c./*w w w .ja v  a  2 s.c  om*/
 */
public static JMenu createLookAndFeelMenu(final Class prefsClass, final ActionListener listener) {
    // Create the menu
    final JMenu plafmenu = new JMenu("Look and Feel");

    // Create an object used for radio button mutual exclusion
    ButtonGroup radiogroup = new ButtonGroup();

    // Look up the available look and feels
    UIManager.LookAndFeelInfo[] plafs = UIManager.getInstalledLookAndFeels();

    // Find out which one is currently used
    String currentLAFName = UIManager.getLookAndFeel().getClass().getName();

    // Loop through the plafs, and add a menu item for each one
    for (int i = 0; i < plafs.length; i++) {
        String plafName = plafs[i].getName();
        final String plafClassName = plafs[i].getClassName();

        // Create the menu item
        final JMenuItem item = plafmenu.add(new JRadioButtonMenuItem(plafName));
        item.setSelected(plafClassName.equals(currentLAFName));

        // Tell the menu item what to do when it is selected
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                // Set the new look and feel
                try {
                    UIManager.setLookAndFeel(plafClassName);
                } catch (UnsupportedLookAndFeelException e) {
                    // Sometimes a Look-and-Feel is installed but not
                    // supported, as in the Windows LaF on Linux platforms.
                    JOptionPane.showMessageDialog(plafmenu,
                            "The selected Look-and-Feel is " + "not supported on this platform.",
                            "Unsupported Look And Feel", JOptionPane.ERROR_MESSAGE);
                    item.setEnabled(false);
                } catch (Exception e) { // ClassNotFound or Instantiation
                    item.setEnabled(false); // shouldn't happen
                }

                // Make the selection persistent by storing it in prefs.
                Preferences p = Preferences.userNodeForPackage(prefsClass);
                p.put(PREF_NAME, plafClassName);

                // Invoke the supplied action listener so the calling
                // application can update its components to the new LAF
                // Reuse the event that was passed here.
                listener.actionPerformed(event);
            }
        });

        // Only allow one menu item to be selected at once
        radiogroup.add(item);
    }

    return plafmenu;
}

From source file:ScreenCapture.java

public ScreenCapture(String title) {
    super(title);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JMenuBar mb = new JMenuBar();
    JMenu menu = new JMenu("File");
    ActionListener al;/*from w w  w .j a v  a  2 s.  com*/

    JMenuItem mi = new JMenuItem("Save");
    al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            save();
        }
    };

    mi.addActionListener(al);
    menu.add(mi);
    mb.add(menu);

    menu = new JMenu("Capture");

    mi = new JMenuItem("Capture");
    al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            setVisible(false);

            BufferedImage biScreen = robot.createScreenCapture(rectScreenSize);
            setVisible(true);

            ia.setImage(biScreen);

            jsp.getHorizontalScrollBar().setValue(0);
            jsp.getVerticalScrollBar().setValue(0);
        }
    };

    mi.addActionListener(al);
    menu.add(mi);

    mb.add(menu);

    mi = new JMenuItem("Crop");
    al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (ia.crop()) {
                jsp.getHorizontalScrollBar().setValue(0);
                jsp.getVerticalScrollBar().setValue(0);
            }
        }
    };

    mi.addActionListener(al);
    menu.add(mi);

    mb.add(menu);
    setJMenuBar(mb);
    getContentPane().add(jsp = new JScrollPane(ia));
    setVisible(true);
}

From source file:SimpleMenu.java

/** The convenience method that creates menu panes */
public static JMenu create(ResourceBundle bundle, String menuname, String[] itemnames,
        ActionListener listener) {
    // Get the menu title from the bundle. Use name as default label.
    String menulabel;/*from  ww  w. j a  v  a  2s.com*/
    try {
        menulabel = bundle.getString(menuname + ".label");
    } catch (MissingResourceException e) {
        menulabel = menuname;
    }

    // Create the menu pane.
    JMenu menu = new JMenu(menulabel);

    // For each named item in the menu.
    for (int i = 0; i < itemnames.length; i++) {
        // Look up the label for the item, using name as default.
        String itemlabel;
        try {
            itemlabel = bundle.getString(menuname + "." + itemnames[i] + ".label");
        } catch (MissingResourceException e) {
            itemlabel = itemnames[i];
        }

        JMenuItem item = new JMenuItem(itemlabel);

        // Look up an accelerator for the menu item
        try {
            String acceleratorText = bundle.getString(menuname + "." + itemnames[i] + ".accelerator");
            item.setAccelerator(KeyStroke.getKeyStroke(acceleratorText));
        } catch (MissingResourceException e) {
        }

        // Register an action listener and command for the item.
        if (listener != null) {
            item.addActionListener(listener);
            item.setActionCommand(itemnames[i]);
        }

        // Add the item to the menu.
        menu.add(item);
    }

    // Return the automatically created localized menu.
    return menu;
}

From source file:fr.crnan.videso3d.ihm.TrajectoryProjectionGUI.java

public TrajectoryProjectionGUI(List<VidesoTrack> tracks, Globe globe) {

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);// w  w w. java2s . c  o  m

    JMenu mnParamtres = new JMenu("Paramtres");
    menuBar.add(mnParamtres);

    JMenuItem mntmNewMenuItem = new JMenuItem("New menu item");
    mnParamtres.add(mntmNewMenuItem);

    double ref = TracksStatsProducer.computeReferenceAltitude(tracks);
    XYSeriesCollection dataset = new XYSeriesCollection();

    for (VidesoTrack t : tracks) {
        dataset.addSeries(TracksStatsProducer.computeDevelopedPath(t, ref, false, globe));
    }

    JFreeChart chart = ChartFactory.createXYLineChart("Projection", "Distance (NM)", "Altitude (FL)", dataset,
            PlotOrientation.VERTICAL, false, true, false);

    ChartPanel panel = new ChartPanel(chart);
    setContentPane(panel);
    pack();
}