Example usage for javax.swing JMenuItem setEnabled

List of usage examples for javax.swing JMenuItem setEnabled

Introduction

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

Prototype

@BeanProperty(preferred = true, description = "The enabled state of the component.")
public void setEnabled(boolean b) 

Source Link

Document

Enables or disables the menu item.

Usage

From source file:SwingToolBarSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Action Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Action showAction = new ShowAction(frame);
    JMenuBar menuBar = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic('f');
    JMenuItem newMenuItem = new JMenuItem("New", 'N');
    fileMenu.add(newMenuItem);// ww  w  .ja v a  2  s .  com
    JMenuItem openMenuItem = new JMenuItem("Open", 'O');
    fileMenu.add(openMenuItem);
    JMenuItem closeMenuItem = new JMenuItem("Close", 'C');
    fileMenu.add(closeMenuItem);
    fileMenu.addSeparator();
    JMenuItem saveMenuItem = new JMenuItem("Save", 'S');
    fileMenu.add(saveMenuItem);
    fileMenu.add(showAction);
    fileMenu.addSeparator();
    JMenuItem exitMenuItem = new JMenuItem("Exit", 'X');
    fileMenu.add(exitMenuItem);
    menuBar.add(fileMenu);
    JMenu editMenu = new JMenu("Edit");
    JMenuItem cutMenuItem = new JMenuItem("Cut", 'T');
    KeyStroke ctrlXKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK);
    cutMenuItem.setAccelerator(ctrlXKeyStroke);
    editMenu.add(cutMenuItem);
    JMenuItem copyMenuItem = new JMenuItem("Copy", 'C');
    KeyStroke ctrlCKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK);
    copyMenuItem.setAccelerator(ctrlCKeyStroke);
    editMenu.add(copyMenuItem);
    JMenuItem pasteMenuItem = new JMenuItem("Paste", 'P');
    KeyStroke ctrlVKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK);
    pasteMenuItem.setAccelerator(ctrlVKeyStroke);
    pasteMenuItem.setEnabled(false);
    editMenu.add(pasteMenuItem);
    editMenu.addSeparator();
    JMenuItem findMenuItem = new JMenuItem("Find", 'F');
    KeyStroke f3KeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0);
    findMenuItem.setAccelerator(f3KeyStroke);
    editMenu.add(findMenuItem);
    editMenu.setMnemonic('e');
    editMenu.add(showAction);
    menuBar.add(editMenu);
    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:MediumPopupSample.java

public static void main(String args[]) {
    ActionListener menuListener = new MenuActionListener();
    JFrame frame = new JFrame("MenuSample Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(fileMenu);//from w  ww  .j  a  v  a2  s  .c om

    // File->New, N - Mnemonic
    JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
    newMenuItem.addActionListener(menuListener);
    fileMenu.add(newMenuItem);

    // File->Open, O - Mnemonic
    JMenuItem openMenuItem = new JMenuItem("Open", KeyEvent.VK_O);
    openMenuItem.addActionListener(menuListener);
    fileMenu.add(openMenuItem);

    // File->Close, C - Mnemonic
    JMenuItem closeMenuItem = new JMenuItem("Close", KeyEvent.VK_C);
    closeMenuItem.addActionListener(menuListener);
    fileMenu.add(closeMenuItem);

    // Separator
    fileMenu.addSeparator();

    // File->Save, S - Mnemonic
    JMenuItem saveMenuItem = new JMenuItem("Save", KeyEvent.VK_S);
    saveMenuItem.addActionListener(menuListener);
    fileMenu.add(saveMenuItem);

    // Separator
    fileMenu.addSeparator();

    // File->Exit, X - Mnemonic
    JMenuItem exitMenuItem = new JMenuItem("Exit", KeyEvent.VK_X);
    exitMenuItem.addActionListener(menuListener);
    fileMenu.add(exitMenuItem);

    // Edit Menu, E - Mnemonic
    JMenu editMenu = new JMenu("Edit");
    editMenu.setMnemonic(KeyEvent.VK_E);
    menuBar.add(editMenu);

    // Edit->Cut, T - Mnemonic, CTRL-X - Accelerator
    JMenuItem cutMenuItem = new JMenuItem("Cut", KeyEvent.VK_T);
    cutMenuItem.addActionListener(menuListener);
    KeyStroke ctrlXKeyStroke = KeyStroke.getKeyStroke("control X");
    cutMenuItem.setAccelerator(ctrlXKeyStroke);
    editMenu.add(cutMenuItem);

    // Edit->Copy, C - Mnemonic, CTRL-C - Accelerator
    JMenuItem copyMenuItem = new JMenuItem("Copy", KeyEvent.VK_C);
    copyMenuItem.addActionListener(menuListener);
    KeyStroke ctrlCKeyStroke = KeyStroke.getKeyStroke("control C");
    copyMenuItem.setAccelerator(ctrlCKeyStroke);
    editMenu.add(copyMenuItem);

    // Edit->Paste, P - Mnemonic, CTRL-V - Accelerator, Disabled
    JMenuItem pasteMenuItem = new JMenuItem("Paste", KeyEvent.VK_P);
    pasteMenuItem.addActionListener(menuListener);
    KeyStroke ctrlVKeyStroke = KeyStroke.getKeyStroke("control V");
    pasteMenuItem.setAccelerator(ctrlVKeyStroke);
    pasteMenuItem.setEnabled(false);
    editMenu.add(pasteMenuItem);

    // Separator
    editMenu.addSeparator();

    // Edit->Find, F - Mnemonic, F3 - Accelerator
    JMenuItem findMenuItem = new JMenuItem("Find", KeyEvent.VK_F);
    findMenuItem.addActionListener(menuListener);
    KeyStroke f3KeyStroke = KeyStroke.getKeyStroke("F3");
    findMenuItem.setAccelerator(f3KeyStroke);
    editMenu.add(findMenuItem);

    // Edit->Options Submenu, O - Mnemonic, at.gif - Icon Image File
    JMenu findOptionsMenu = new JMenu("Options");
    Icon atIcon = new ImageIcon("at.gif");
    findOptionsMenu.setIcon(atIcon);
    findOptionsMenu.setMnemonic(KeyEvent.VK_O);

    // ButtonGroup for radio buttons
    ButtonGroup directionGroup = new ButtonGroup();

    // Edit->Options->Forward, F - Mnemonic, in group
    JRadioButtonMenuItem forwardMenuItem = new JRadioButtonMenuItem("Forward", true);
    forwardMenuItem.addActionListener(menuListener);
    forwardMenuItem.setMnemonic(KeyEvent.VK_F);
    findOptionsMenu.add(forwardMenuItem);
    directionGroup.add(forwardMenuItem);

    // Edit->Options->Backward, B - Mnemonic, in group
    JRadioButtonMenuItem backwardMenuItem = new JRadioButtonMenuItem("Backward");
    backwardMenuItem.addActionListener(menuListener);
    backwardMenuItem.setMnemonic(KeyEvent.VK_B);
    findOptionsMenu.add(backwardMenuItem);
    directionGroup.add(backwardMenuItem);

    // Separator
    findOptionsMenu.addSeparator();

    // Edit->Options->Case Sensitive, C - Mnemonic
    JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Case Sensitive");
    caseMenuItem.addActionListener(menuListener);
    caseMenuItem.setMnemonic(KeyEvent.VK_C);
    findOptionsMenu.add(caseMenuItem);
    editMenu.add(findOptionsMenu);

    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:MenuX.java

public static void main(String args[]) {
    ActionListener actionListener = new MenuActionListener();
    MenuKeyListener menuKeyListener = new MyMenuKeyListener();
    ChangeListener cListener = new MyChangeListener();
    MenuListener menuListener = new MyMenuListener();
    MenuSelectionManager manager = MenuSelectionManager.defaultManager();
    manager.addChangeListener(cListener);
    JFrame frame = new JFrame("MenuSample Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar bar = new VerticalMenuBar();
    //    JMenuBar bar = new JMenuBar();

    // File Menu, F - Mnemonic
    JMenu file = new JMenu("File");
    file.setMnemonic(KeyEvent.VK_F);
    file.addChangeListener(cListener);/*from w ww .ja v  a 2 s. c  o m*/
    file.addMenuListener(menuListener);
    file.addMenuKeyListener(menuKeyListener);
    JPopupMenu popupMenu = file.getPopupMenu();
    popupMenu.setLayout(new GridLayout(3, 3));
    bar.add(file);

    // File->New, N - Mnemonic
    JMenuItem newItem = new JMenuItem("New", KeyEvent.VK_N);
    newItem.addActionListener(actionListener);
    newItem.addChangeListener(cListener);
    newItem.addMenuKeyListener(menuKeyListener);
    file.add(newItem);

    // File->Open, O - Mnemonic
    JMenuItem openItem = new JMenuItem("Open", KeyEvent.VK_O);
    openItem.addActionListener(actionListener);
    openItem.addChangeListener(cListener);
    openItem.addMenuKeyListener(menuKeyListener);
    file.add(openItem);

    // File->Close, C - Mnemonic
    JMenuItem closeItem = new JMenuItem("Close", KeyEvent.VK_C);
    closeItem.addActionListener(actionListener);
    closeItem.addChangeListener(cListener);
    closeItem.addMenuKeyListener(menuKeyListener);
    file.add(closeItem);

    // Separator
    file.addSeparator();

    // File->Save, S - Mnemonic
    JMenuItem saveItem = new JMenuItem("Save", KeyEvent.VK_S);
    saveItem.addActionListener(actionListener);
    saveItem.addChangeListener(cListener);
    saveItem.addMenuKeyListener(menuKeyListener);
    file.add(saveItem);

    // Separator
    file.addSeparator();

    // File->Exit, X - Mnemonic
    JMenuItem exitItem = new JMenuItem("Exit", KeyEvent.VK_X);
    exitItem.addActionListener(actionListener);
    exitItem.addChangeListener(cListener);
    exitItem.addMenuKeyListener(menuKeyListener);
    file.add(exitItem);

    // Edit Menu, E - Mnemonic
    JMenu edit = new JMenu("Edit");
    edit.setMnemonic(KeyEvent.VK_E);
    edit.addChangeListener(cListener);
    edit.addMenuListener(menuListener);
    edit.addMenuKeyListener(menuKeyListener);
    bar.add(edit);

    // Edit->Cut, T - Mnemonic, CTRL-X - Accelerator
    JMenuItem cutItem = new JMenuItem("Cut", KeyEvent.VK_T);
    cutItem.addActionListener(actionListener);
    cutItem.addChangeListener(cListener);
    cutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK));
    cutItem.addMenuKeyListener(menuKeyListener);
    edit.add(cutItem);

    // Edit->Copy, C - Mnemonic, CTRL-C - Accelerator
    JMenuItem copyItem = new JMenuItem("Copy", KeyEvent.VK_C);
    copyItem.addActionListener(actionListener);
    copyItem.addChangeListener(cListener);
    copyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK));
    copyItem.addMenuKeyListener(menuKeyListener);
    copyItem.setEnabled(false);
    edit.add(copyItem);

    // Edit->Paste, P - Mnemonic, CTRL-V - Accelerator, Disabled
    JMenuItem pasteItem = new JMenuItem("Paste", KeyEvent.VK_P);
    pasteItem.addActionListener(actionListener);
    pasteItem.addChangeListener(cListener);
    pasteItem.addMenuKeyListener(menuKeyListener);
    pasteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK));
    pasteItem.setEnabled(false);
    edit.add(pasteItem);

    // Separator
    edit.addSeparator();

    // Edit->Find, F - Mnemonic, F3 - Accelerator
    JMenuItem findItem = new JMenuItem("Find", KeyEvent.VK_F);
    findItem.addActionListener(actionListener);
    findItem.addChangeListener(cListener);
    findItem.addMenuKeyListener(menuKeyListener);
    findItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0));
    edit.add(findItem);

    // Edit->Options Submenu, O - Mnemonic, at.gif - Icon Image File
    JMenu findOptions = new JMenu("Options");
    findOptions.addChangeListener(cListener);
    findOptions.addMenuListener(menuListener);
    findOptions.addMenuKeyListener(menuKeyListener);
    Icon atIcon = new ImageIcon("at.gif");
    findOptions.setIcon(atIcon);
    findOptions.setMnemonic(KeyEvent.VK_O);

    // ButtonGrou for radio buttons
    ButtonGroup directionGroup = new ButtonGroup();

    // Edit->Options->Forward, F - Mnemonic, in group
    JRadioButtonMenuItem forward = new JRadioButtonMenuItem("Forward", true);
    forward.addActionListener(actionListener);
    forward.addChangeListener(cListener);
    forward.addMenuKeyListener(menuKeyListener);
    forward.setMnemonic(KeyEvent.VK_F);
    findOptions.add(forward);
    directionGroup.add(forward);

    // Edit->Options->Backward, B - Mnemonic, in group
    JRadioButtonMenuItem backward = new JRadioButtonMenuItem("Backward");
    backward.addActionListener(actionListener);
    backward.addChangeListener(cListener);
    backward.addMenuKeyListener(menuKeyListener);
    backward.setMnemonic(KeyEvent.VK_B);
    findOptions.add(backward);
    directionGroup.add(backward);

    // Separator
    findOptions.addSeparator();

    // Edit->Options->Case Sensitive, C - Mnemonic
    JCheckBoxMenuItem caseItem = new JCheckBoxMenuItem("Case Sensitive");
    caseItem.addActionListener(actionListener);
    caseItem.addChangeListener(cListener);
    caseItem.addMenuKeyListener(menuKeyListener);
    caseItem.setMnemonic(KeyEvent.VK_C);
    findOptions.add(caseItem);
    edit.add(findOptions);

    frame.setJMenuBar(bar);
    //    frame.getContentPane().add(bar, BorderLayout.EAST);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:MenuY.java

public static void main(String args[]) {
    ActionListener actionListener = new MenuActionListener();
    MenuKeyListener menuKeyListener = new MyMenuKeyListener();
    ChangeListener cListener = new MyChangeListener();
    MenuListener menuListener = new MyMenuListener();
    MenuSelectionManager manager = MenuSelectionManager.defaultManager();
    manager.addChangeListener(cListener);
    JFrame frame = new JFrame("MenuSample Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar bar = new VerticalMenuBar();
    //    JMenuBar bar = new JMenuBar();

    // File Menu, F - Mnemonic
    JMenu file = new JMenu("File");
    file.setMnemonic(KeyEvent.VK_F);
    file.addChangeListener(cListener);/*from   www.j  av a2s .  c o m*/
    file.addMenuListener(menuListener);
    file.addMenuKeyListener(menuKeyListener);
    JPopupMenu popupMenu = file.getPopupMenu();
    popupMenu.setLayout(new GridLayout(3, 3));
    bar.add(file);

    // File->New, N - Mnemonic
    JMenuItem newItem = new JMenuItem("New", KeyEvent.VK_N);
    newItem.addActionListener(actionListener);
    newItem.addChangeListener(cListener);
    newItem.addMenuKeyListener(menuKeyListener);
    file.add(newItem);

    // File->Open, O - Mnemonic
    JMenuItem openItem = new JMenuItem("Open", KeyEvent.VK_O);
    openItem.addActionListener(actionListener);
    openItem.addChangeListener(cListener);
    openItem.addMenuKeyListener(menuKeyListener);
    file.add(openItem);

    // File->Close, C - Mnemonic
    JMenuItem closeItem = new JMenuItem("Close", KeyEvent.VK_C);
    closeItem.addActionListener(actionListener);
    closeItem.addChangeListener(cListener);
    closeItem.addMenuKeyListener(menuKeyListener);
    file.add(closeItem);

    // Separator
    file.addSeparator();

    // File->Save, S - Mnemonic
    JMenuItem saveItem = new JMenuItem("Save", KeyEvent.VK_S);
    saveItem.addActionListener(actionListener);
    saveItem.addChangeListener(cListener);
    saveItem.addMenuKeyListener(menuKeyListener);
    file.add(saveItem);

    // Separator
    file.addSeparator();

    // File->Exit, X - Mnemonic
    JMenuItem exitItem = new JMenuItem("Exit", KeyEvent.VK_X);
    exitItem.addActionListener(actionListener);
    exitItem.addChangeListener(cListener);
    exitItem.addMenuKeyListener(menuKeyListener);
    file.add(exitItem);

    // Edit Menu, E - Mnemonic
    JMenu edit = new JMenu("Edit");
    edit.setMnemonic(KeyEvent.VK_E);
    edit.addChangeListener(cListener);
    edit.addMenuListener(menuListener);
    edit.addMenuKeyListener(menuKeyListener);
    bar.add(edit);

    // Edit->Cut, T - Mnemonic, CTRL-X - Accelerator
    JMenuItem cutItem = new JMenuItem("Cut", KeyEvent.VK_T);
    cutItem.addActionListener(actionListener);
    cutItem.addChangeListener(cListener);
    cutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK));
    cutItem.addMenuKeyListener(menuKeyListener);
    edit.add(cutItem);

    // Edit->Copy, C - Mnemonic, CTRL-C - Accelerator
    JMenuItem copyItem = new JMenuItem("Copy", KeyEvent.VK_C);
    copyItem.addActionListener(actionListener);
    copyItem.addChangeListener(cListener);
    copyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK));
    copyItem.addMenuKeyListener(menuKeyListener);
    copyItem.setEnabled(false);
    edit.add(copyItem);

    // Edit->Paste, P - Mnemonic, CTRL-V - Accelerator, Disabled
    JMenuItem pasteItem = new JMenuItem("Paste", KeyEvent.VK_P);
    pasteItem.addActionListener(actionListener);
    pasteItem.addChangeListener(cListener);
    pasteItem.addMenuKeyListener(menuKeyListener);
    pasteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK));
    pasteItem.setEnabled(false);
    edit.add(pasteItem);

    // Separator
    edit.addSeparator();

    // Edit->Find, F - Mnemonic, F3 - Accelerator
    JMenuItem findItem = new JMenuItem("Find", KeyEvent.VK_F);
    findItem.addActionListener(actionListener);
    findItem.addChangeListener(cListener);
    findItem.addMenuKeyListener(menuKeyListener);
    findItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0));
    edit.add(findItem);

    // Edit->Options Submenu, O - Mnemonic, at.gif - Icon Image File
    Icon atIcon = new ImageIcon("at.gif");
    Action findAction = new AbstractAction("Options", atIcon) {
        ActionListener actionListener = new MenuActionListener();

        public void actionPerformed(ActionEvent e) {
            actionListener.actionPerformed(e);
        }
    };
    findAction.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_O));
    JMenuItem jMenuItem = new JMenuItem(findAction);

    JMenu findOptions = new JMenu(findAction);
    findOptions.addChangeListener(cListener);
    findOptions.addMenuListener(menuListener);
    findOptions.addMenuKeyListener(menuKeyListener);

    // ButtonGrou for radio buttons
    ButtonGroup directionGroup = new ButtonGroup();

    // Edit->Options->Forward, F - Mnemonic, in group
    JRadioButtonMenuItem forward = new JRadioButtonMenuItem("Forward", true);
    forward.addActionListener(actionListener);
    forward.addChangeListener(cListener);
    forward.addMenuKeyListener(menuKeyListener);
    forward.setMnemonic(KeyEvent.VK_F);
    findOptions.add(forward);
    directionGroup.add(forward);

    // Edit->Options->Backward, B - Mnemonic, in group
    JRadioButtonMenuItem backward = new JRadioButtonMenuItem("Backward");
    backward.addActionListener(actionListener);
    backward.addChangeListener(cListener);
    backward.addMenuKeyListener(menuKeyListener);
    backward.setMnemonic(KeyEvent.VK_B);
    findOptions.add(backward);
    directionGroup.add(backward);

    // Separator
    findOptions.addSeparator();

    // Edit->Options->Case Sensitive, C - Mnemonic
    JCheckBoxMenuItem caseItem = new JCheckBoxMenuItem("Case Sensitive");
    caseItem.addActionListener(actionListener);
    caseItem.addChangeListener(cListener);
    caseItem.addMenuKeyListener(menuKeyListener);
    caseItem.setMnemonic(KeyEvent.VK_C);
    findOptions.add(caseItem);
    edit.add(findOptions);

    frame.setJMenuBar(bar);
    //    frame.getContentPane().add(bar, BorderLayout.EAST);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:Main.java

/** Brings the destination menu item into sync with the source item. */
protected static void syncMenuItem(final JMenuItem source, final JMenuItem dest) {
    final boolean enabled = source.isEnabled();
    if (dest.isEnabled() != enabled)
        dest.setEnabled(enabled);
    final int mnemonic = source.getMnemonic();
    if (dest.getMnemonic() != mnemonic)
        dest.setMnemonic(mnemonic);/*from   w ww .ja v a 2  s  .  co m*/
    final String text = source.getText();
    if (dest.getText() != text)
        dest.setText(text);
    final KeyStroke accel = source.getAccelerator();
    if (dest.getAccelerator() != accel)
        dest.setAccelerator(accel);
}

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.//from   ww  w .  j  a v a2  s. co  m
 */
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:Main.java

protected void showMenu(int x, int y) {
    JPopupMenu popup = new JPopupMenu();
    JMenuItem mi = new JMenuItem("Delete");
    TreePath path = tree.getSelectionPath();
    Object node = path.getLastPathComponent();
    if (node == tree.getModel().getRoot()) {
        mi.setEnabled(false);
    }/*from ww  w.j a v a  2s .  c  o  m*/
    popup.add(mi);
    mi.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            deleteSelectedItems();
        }
    });
    popup.show(tree, x, y);
}

From source file:EditorPaneExample20.java

public static JMenu buildMenu(String name, MenuSpec[] menuSpecs, Hashtable actions) {
    int count = menuSpecs.length;

    JMenu menu = new JMenu(name);
    for (int i = 0; i < count; i++) {
        MenuSpec spec = menuSpecs[i];/*w w  w  . ja  v  a 2  s .c o m*/
        if (spec.isSubMenu()) {
            // Recurse to handle a sub menu
            JMenu subMenu = buildMenu(spec.getName(), spec.getSubMenus(), actions);
            if (subMenu != null) {
                menu.add(subMenu);
            }
        } else if (spec.isAction()) {
            // It's an Action - add it directly to the menu
            menu.add(spec.getAction());
        } else {
            // It's an action name - add it if possible
            String actionName = spec.getActionName();
            Action targetAction = (Action) actions.get(actionName);

            // Create the menu item
            JMenuItem menuItem = menu.add(spec.getName());
            if (targetAction != null) {
                // The editor kit knows the action
                menuItem.addActionListener(targetAction);
            } else {
                // Action not known - disable the menu item
                menuItem.setEnabled(false);
            }
        }
    }

    // Return null if nothing was added to the menu.
    if (menu.getMenuComponentCount() == 0) {
        menu = null;
    }

    return menu;
}

From source file:net.sf.firemox.AbstractMainForm.java

/**
 * Initialize the components of this form.
 *//*from   w ww .j  a va  2  s.  c om*/
protected void initAbstractMenu() {
    // "TBS" menu
    tbsMenu = UIHelper.buildMenu("menu_options_tbs");
    ButtonGroup group4 = new ButtonGroup();
    final MdbListener mdbListener = new MdbListener(this);
    final File[] mdbs = MToolKit.getFile(IdConst.TBS_DIR)
            .listFiles((FileFilter) FileFilterUtils.suffixFileFilter("xml"));
    String defaultTbs = MToolKit.tbsName;
    for (File mdb : mdbs) {
        String mdbName = FilenameUtils.getBaseName(mdb.getName());
        JRadioButtonMenuItem itemChck = new JRadioButtonMenuItem();
        MToolKit.tbsName = mdbName;
        updateMdbMenu(mdbName, itemChck);
        itemChck.setActionCommand(mdbName);
        itemChck.setFont(MToolKit.defaultFont);
        itemChck.addActionListener(mdbListener);
        group4.add(itemChck);
        tbsMenu.add(itemChck);
        if (mdbName.equals(defaultTbs)) {
            itemChck.setSelected(true);
        }
    }
    MToolKit.tbsName = defaultTbs;
    tbsMenu.add(new JSeparator());

    // "More TBS" menu item
    tbsMenu.add(UIHelper.buildMenu("menu_options_tbs_more", mdbListener));
    tbsMenu.add(new JSeparator());
    final JMenuItem updateMdbMenu = UIHelper.buildMenu("menu_options_tbs_update", mdbListener);
    updateMdbMenu.setEnabled(false);
    tbsMenu.add(updateMdbMenu);
    tbsMenu.add(UIHelper.buildMenu("menu_options_tbs_rebuild", mdbListener));
    optionMenu.add(new JSeparator());
    optionMenu.add(tbsMenu);
}

From source file:TopLevelTransferHandlerDemo.java

private JMenu createDummyMenu(String str) {
    JMenu menu = new JMenu(str);
    JMenuItem item = new JMenuItem("[Empty]");
    item.setEnabled(false);
    menu.add(item);//  w  w w. ja  v  a  2s. c o  m
    return menu;
}