Example usage for javax.swing JMenu addSeparator

List of usage examples for javax.swing JMenu addSeparator

Introduction

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

Prototype

public void addSeparator() 

Source Link

Document

Appends a new separator to the end of the menu.

Usage

From source file:edu.ku.brc.specify.utilapps.sp5utils.Sp5Forms.java

/**
 * @param args/*from  w w w . j  a  va 2s . c  o  m*/
 */
public static void main(String[] args) {

    // Set App Name, MUST be done very first thing!
    UIRegistry.setAppName("Specify"); //$NON-NLS-1$

    log.debug("********* Current [" + (new File(".").getAbsolutePath()) + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

    UIRegistry.setEmbeddedDBPath(UIRegistry.getDefaultEmbeddedDBPath()); // on the local machine

    for (String s : args) {
        String[] pairs = s.split("="); //$NON-NLS-1$
        if (pairs.length == 2) {
            if (pairs[0].startsWith("-D")) //$NON-NLS-1$
            {
                //System.err.println("["+pairs[0].substring(2, pairs[0].length())+"]["+pairs[1]+"]");
                System.setProperty(pairs[0].substring(2, pairs[0].length()), pairs[1]);
            }
        } else {
            String symbol = pairs[0].substring(2, pairs[0].length());
            //System.err.println("["+symbol+"]");
            System.setProperty(symbol, symbol);
        }
    }

    // Now check the System Properties
    String appDir = System.getProperty("appdir");
    if (StringUtils.isNotEmpty(appDir)) {
        UIRegistry.setDefaultWorkingPath(appDir);
    }

    String appdatadir = System.getProperty("appdatadir");
    if (StringUtils.isNotEmpty(appdatadir)) {
        UIRegistry.setBaseAppDataDir(appdatadir);
    }

    Logger logger = LogManager.getLogger("edu.ku.brc");
    if (logger != null) {
        logger.setLevel(Level.ALL);
        System.out.println("Setting " + logger.getName() + " to " + logger.getLevel());
    }

    logger = LogManager.getLogger(edu.ku.brc.dbsupport.HibernateUtil.class);
    if (logger != null) {
        logger.setLevel(Level.INFO);
        System.out.println("Setting " + logger.getName() + " to " + logger.getLevel());
    }

    // Create Specify Application
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            try {
                if (!System.getProperty("os.name").equals("Mac OS X")) {
                    UIManager.setLookAndFeel(new Plastic3DLookAndFeel());
                    PlasticLookAndFeel.setPlasticTheme(new DesertBlue());
                }

                final Sp5Forms frame = new Sp5Forms();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setBounds(0, 0, 800, 800);

                new MacOSAppHandler(frame);

                frame.startup();

                frame.createUI();
                JMenuBar mb = new JMenuBar();
                JMenu fileMenu = new JMenu("File");

                JMenuItem openMI = new JMenuItem("Open XML File");
                fileMenu.add(openMI);
                openMI.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        frame.openXML();
                    }
                });

                JMenuItem openDBMI = new JMenuItem("Open Database");
                fileMenu.add(openDBMI);
                openDBMI.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        frame.openDB();
                    }
                });

                JMenuItem saveMI = new JMenuItem("Save");
                fileMenu.add(saveMI);
                saveMI.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        frame.saveXML();
                    }
                });

                if (!UIHelper.isMacOS()) {
                    fileMenu.addSeparator();
                    JMenuItem mi = new JMenuItem("Exit");
                    fileMenu.add(mi);

                    mi.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            System.exit(0);
                        }
                    });
                }

                mb.add(fileMenu);
                frame.setJMenuBar(mb);

                centerAndShow(frame);
            } catch (Exception e) {
                log.error("Can't change L&F: ", e);
            }
        }
    });
}

From source file:Main.java

public static JMenu menu(String name, JMenuItem... items) {
    final JMenu menu = new JMenu(name);
    for (JMenuItem item : items) {
        if (item != null) {
            menu.add(item);//from www  . j  a  v a 2  s  .  com
        } else {
            menu.addSeparator();
        }
    }
    return menu;
}

From source file:Main.java

/**
 * Populates a {@link JMenuBar} from the given list and {@link ActionMap}.
 * //from w  w w .ja va2  s  .co m
 * @param menubar
 * @param actionMap
 * @param list
 * @return
 */
public static void populate(JMenuBar menubar, ActionMap actionMap, List<?> list) {
    JMenu menu = null;

    for (Object element : list) {
        if (element == null) {
            if (menu != null) {
                menu.addSeparator();
            }
        } else if (element instanceof List<?>) {
            menu = createMenu(actionMap, (List<?>) element);
            if (menu != null) {
                menubar.add(menu);
            }
        } else if (element.getClass().isArray()) {
            menu = createMenu(actionMap, (Object[]) element);
            if (menu != null) {
                menubar.add(menu);
            }
        } else {
            if (menu != null) {
                menu.add(actionMap.get(element));
            }
        }
    }
}

From source file:Main.java

/**
 * Creates a {@link JMenu} from the given list and action map.
 * //from   w  ww .  j a  v a 2  s.  c  o  m
 * @param actionMap
 * @param list
 * @return
 */
public static JMenu createMenu(ActionMap actionMap, List<?> list) {
    // The first item will be the action for the JMenu
    final Action titleAction = actionMap.get(list.get(0));
    if (titleAction == null) {
        return null;
    }
    final JMenu menu = new JMenu();
    menu.setAction(titleAction);

    // The rest of the items represent the menu items.
    for (Object element : list.subList(1, list.size())) {
        if (element == null) {
            menu.addSeparator();
        } else if (element instanceof List<?>) {
            JMenu newMenu = createMenu(actionMap, (List<?>) element);
            if (newMenu != null) {
                menu.add(newMenu);
            }
        } else if (element.getClass().isArray()) {
            JMenu newMenu = createMenu(actionMap, (Object[]) element);
            if (newMenu != null) {
                menu.add(newMenu);
            }
        } else {
            final Action action = actionMap.get(element);
            if (action == null) {
                continue;
            } else {
                menu.add(createMenuItem(action));
            }
        }
    }
    return menu;
}

From source file:Main.java

/**
 * Creates a copy of this menu item, whose contents update automatically
 * whenever the original menu item changes.
 *///from  w ww  .  j ava 2 s  . c  o  m
public static JMenuItem cloneMenuItem(final JMenuItem item) {
    if (item == null)
        return null;
    JMenuItem jmi;
    if (item instanceof JMenu) {
        final JMenu menu = (JMenu) item;
        final JMenu jm = new JMenu();
        final int count = menu.getItemCount();
        for (int i = 0; i < count; i++) {
            final JMenuItem ijmi = cloneMenuItem(menu.getItem(i));
            if (ijmi == null)
                jm.addSeparator();
            else
                jm.add(ijmi);
        }
        jmi = jm;
    } else
        jmi = new JMenuItem();
    final ActionListener[] l = item.getActionListeners();
    for (int i = 0; i < l.length; i++)
        jmi.addActionListener(l[i]);
    jmi.setActionCommand(item.getActionCommand());
    syncMenuItem(item, jmi);
    linkMenuItem(item, jmi);
    return jmi;
}

From source file:com.googlecode.blaisemath.app.MenuConfig.java

/**
 * Build a menu from a given map entry with key the menu name, and value a list
 * or map that determines the menu items.
 * @param name menu name/*from   w  w  w . j  a va 2  s  .c  o  m*/
 * @param menuContent content of menu
 * @param am string/action mapping
 * @return menu
 */
static JMenu createMenu(String name, List menuContent, Map<String, Action> am) {
    JMenu menu = new JMenu(name);
    for (JMenuItem el : createMenuItemList(menuContent, am)) {
        if (el == null) {
            menu.addSeparator();
        } else {
            menu.add(el);
        }
    }
    return menu;
}

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;
                }/* ww w  .  j a  v a  2 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:TabPanelwithImageIconCustom.java

public TabPanelwithImageIconCustom() {
    setSize(450, 350);// w w w. ja  v  a 2s  .  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() {
    setSize(450, 350);/* ww w  .java  2  s . c  o 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:AddingDropDownMenus.java

public AddingDropDownMenus() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setJMenuBar(menuBar);/* w ww  . j a  va  2 s .  c o  m*/
    JMenu fileMenu = new JMenu("File");
    JMenu elementMenu = new JMenu("Elements");
    newItem = fileMenu.add("New");
    openItem = fileMenu.add("Open");
    closeItem = fileMenu.add("Close");
    fileMenu.addSeparator();
    saveItem = fileMenu.add("Save");
    saveAsItem = fileMenu.add("Save As...");
    fileMenu.addSeparator();
    printItem = fileMenu.add("Print");
    elementMenu.add(lineItem = new JRadioButtonMenuItem("Line", true));
    elementMenu.add(rectangleItem = new JRadioButtonMenuItem("Rectangle", false));
    elementMenu.add(circleItem = new JRadioButtonMenuItem("Circle", false));
    ButtonGroup types = new ButtonGroup();
    types.add(lineItem);
    types.add(rectangleItem);
    types.add(circleItem);
    elementMenu.addSeparator();
    elementMenu.add(redItem = new JCheckBoxMenuItem("Red", false));
    elementMenu.add(yellowItem = new JCheckBoxMenuItem("Yellow", false));
    menuBar.add(fileMenu);
    menuBar.add(elementMenu);
}