Example usage for javax.swing JMenu add

List of usage examples for javax.swing JMenu add

Introduction

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

Prototype

public JMenuItem add(Action a) 

Source Link

Document

Creates a new menu item attached to the specified Action object and appends it to the end of this menu.

Usage

From source file:maltcms.ui.nb.pipelineRunner.actions.RunMaltcmsPipelinesAction.java

protected static JMenu actionsToMenu(String menuName, Action[] actions, Lookup context) {
    //code from Utilities.actionsToPopup
    // keeps actions for which was menu item created already (do not add them twice)
    Set<Action> counted = new HashSet<>();
    // components to be added (separators are null)
    List<Component> components = new ArrayList<>();

    for (Action action : actions) {
        if (action != null && counted.add(action)) {
            // switch to replacement action if there is some
            if (action instanceof ContextAwareAction) {
                //               System.out.println("Context aware action");
                Action contextAwareAction = ((ContextAwareAction) action).createContextAwareInstance(context);
                if (contextAwareAction == null) {
                    Logger.getLogger(RunMaltcmsPipelinesAction.class.getName()).log(Level.WARNING,
                            "ContextAwareAction.createContextAwareInstance(context) returns null. That is illegal!"
                                    + " action={0}, context={1}",
                            new Object[] { action, context });
                } else {
                    action = contextAwareAction;
                }/*w  w w  .  j a  v a2 s  .c o  m*/
            }

            JMenuItem item;
            if (action instanceof JMenuItem || action instanceof JMenu) {
                item = (JMenuItem) action;
            } else if (action instanceof Presenter.Popup) {
                //               System.out.println("Popup menu");
                item = ((Presenter.Popup) action).getPopupPresenter();
                if (item == null) {
                    Logger.getLogger(RunMaltcmsPipelinesAction.class.getName()).log(Level.WARNING,
                            "findContextMenuImpl, getPopupPresenter returning null for {0}", action);
                    continue;
                }
            } else if (action instanceof DynamicMenuContent) {
                //               System.out.println("Dynamic content menu");
                DynamicMenuContent dmc = (DynamicMenuContent) action;
                JComponent[] presenters = dmc.getMenuPresenters();
                String name = action.getValue("name").toString();
                item = new JMenuItem(name);
                for (JComponent jc : presenters) {
                    item.add(jc);
                }
            } else {
                //               System.out.println("Plain menu action");
                // We need to correctly handle mnemonics with '&' etc.
                item = ActionPresenterProvider.getDefault().createPopupPresenter(action);
            }

            for (Component c : ActionPresenterProvider.getDefault().convertComponents(item)) {
                if (c instanceof JSeparator) {
                    components.add(null);
                } else {
                    components.add(c);
                }
            }
        } else {
            components.add(null);
        }
    }

    // Now create actual menu. Strip adjacent, leading, and trailing separators.
    JMenu menu = new JMenu(menuName);
    boolean nonempty = false; // has anything been added yet?
    boolean pendingSep = false; // should there be a separator before any following item?
    for (Component c : components) {
        try {
            if (c == null) {
                pendingSep = nonempty;
            } else {
                nonempty = true;
                if (pendingSep) {
                    pendingSep = false;
                    menu.addSeparator();
                }
                menu.add(c);
            }
        } catch (RuntimeException ex) {
            Exceptions.attachMessage(ex, "Current component: " + c); // NOI18N
            Exceptions.attachMessage(ex, "List of components: " + components); // NOI18N
            Exceptions.attachMessage(ex, "List of actions: " + Arrays.asList(actions)); // NOI18N
            Exceptions.printStackTrace(ex);
        }
    }
    return menu;
}

From source file:Main.java

public JMenu createMenu(String title) {
    JMenu m = new JMenu(title);
    m.add("Menu item #1 in " + title);
    m.add("Menu item #2 in " + title);
    m.add("Menu item #3 in " + title);
    return m;/*  ww  w.j  a  v  a 2 s .  c om*/
}

From source file:RootExample3.java

public RootExample3() {
    super("RootPane Menu Demo");
    setSize(220, 100);//from w  w  w . j a v a2  s  . c om
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JMenuBar bar = new JMenuBar();
    JMenu menu = new JMenu("File");
    bar.add(menu);
    menu.add("Open");
    menu.add("Close");
    setJMenuBar(bar);

    getContentPane().add(new JButton("Hello World"));
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar bar = new JMenuBar();
    JMenu menu = new JMenu("File");
    bar.add(menu);/*from  w  w w .  jav  a 2  s . c o m*/

    menu.add(new JMenuItem("Close"));
    menu.add(new JSeparator()); // SEPARATOR
    menu.add(new JMenuItem("Exit"), 0);

    setJMenuBar(bar);
    add(new JLabel("A placeholder"));

    pack();
    setSize(300, 300);
    setVisible(true);
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar bar = new JMenuBar();
    JMenu menu = new JMenu("File");
    bar.add(menu);/* w w  w. j a  v a2  s  . c  o m*/

    menu.add(new JMenuItem("Close"));
    menu.add(new JSeparator()); // SEPARATOR
    menu.add(new JMenuItem("Exit"));

    setJMenuBar(bar);
    add(new JLabel("A placeholder"));

    pack();
    setSize(300, 300);
    setVisible(true);
}

From source file:MenuSeparator.java

public MenuSeparator() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar bar = new JMenuBar();
    JMenu menu = new JMenu("File");
    bar.add(menu);/*from www .  j av a  2s  .co m*/

    menu.add(new JMenuItem("Close"));
    menu.add(new JSeparator()); // SEPARATOR
    menu.add(new JMenuItem("Exit"));

    setJMenuBar(bar);
    getContentPane().add(new JLabel("A placeholder"));

    pack();
    setSize(300, 300);
    setVisible(true);
}

From source file:RootExample3.java

public RootExample3() {
    super("RootPane Menu Demo");
    setSize(220, 100);/*  ww  w .  ja  v  a 2  s  .c  o m*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    // Create a menu bar
    JMenuBar bar = new JMenuBar();
    JMenu menu = new JMenu("File");
    bar.add(menu);
    menu.add("Open");
    menu.add("Close");
    setJMenuBar(bar);

    // Add a button to the content pane
    getContentPane().add(new JButton("Hello World"));
}

From source file:TabPanelwithImageIconCustom.java

public TabPanelwithImageIconCustom() {
    setSize(450, 350);//from w  w w  .j  av a 2  s  .  co m
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().add(textfield, BorderLayout.SOUTH);

    JMenuBar mbar = new JMenuBar();
    JMenu menu = new JMenu("File");
    menu.add(new JCheckBoxMenuItem("Check Me"));
    menu.addSeparator();
    JMenuItem item = new JMenuItem("Exit");
    item.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    menu.add(item);
    mbar.add(menu);
    setJMenuBar(mbar);

    JTabbedPane tabbedPane = new JTabbedPane();

    tabbedPane.addTab("Button", new TabIcon(), new JButton(""), "Click here for Button demo");
}

From source file:Main.java

public Main() {
    setSize(450, 350);/*from   ww w. j a va2 s .  c om*/
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().add(textfield, BorderLayout.SOUTH);

    JMenuBar mbar = new JMenuBar();
    JMenu menu = new JMenu("File");
    menu.add(new JCheckBoxMenuItem("Check Me"));
    menu.addSeparator();
    JMenuItem item = new JMenuItem("Exit");
    item.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    menu.add(item);
    mbar.add(menu);
    setJMenuBar(mbar);

    JTabbedPane tabbedPane = new JTabbedPane();

    tabbedPane.addTab("Button", new TabIcon(), new JButton(""), "Click here for Button demo");
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JMenuBar bar = new JMenuBar();
    JMenu menu = new JMenu("Checkable");
    bar.add(menu);//from w w w  .  j a  v a  2 s  . c o  m
    menu.add(check);

    check.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            System.out.println("Checked? " + check.isSelected());
        }
    });

    setJMenuBar(bar);
    getContentPane().add(new JLabel("A placeholder"));

    pack();
    setSize(300, 300);
    setVisible(true);
}