Example usage for javax.swing JMenuBar add

List of usage examples for javax.swing JMenuBar add

Introduction

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

Prototype

public JMenu add(JMenu c) 

Source Link

Document

Appends the specified menu to the end of the menu bar.

Usage

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

/**
 * @param args//w w w . j av  a2s.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:QandE.MyDemo2.java

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread./*from w  w w. j  a  va2  s.c  om*/
 */
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:Main.java

static public JMenu createJMenu(JMenuBar pMenuBar, String pMenuText, char pMnemonic) {
    final JMenu menu = new JMenu(pMenuText);
    menu.setMnemonic(pMnemonic);//from  w w w  . j a  va 2s  .  c  o  m

    if (defaultFont != null)
        menu.setFont(defaultFont);

    pMenuBar.add(menu);
    return menu;
}

From source file:Main.java

/**
 * Creates a copy of this menu bar, whose contents update automatically
 * whenever the original menu bar changes.
 *///from w  ww  .ja  va  2 s .c o m
public static JMenuBar cloneMenuBar(final JMenuBar menubar) {
    if (menubar == null)
        return null;
    final JMenuBar jmb = new JMenuBar();
    final int count = menubar.getMenuCount();
    for (int i = 0; i < count; i++)
        jmb.add(cloneMenuItem(menubar.getMenu(i)));
    return jmb;
}

From source file:jgraph.JShow.java

/**
 * a driver for this demo//w  ww. j a v  a2  s  . co  m
 */
@SuppressWarnings("serial")
public static void showtest(DirectedOrderedSparseMultigraph<Object, Object> graph) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JShow demo = new JShow(graph);

    JMenu menu = new JMenu("Snapshot");
    menu.add(new AbstractAction("To JPEG") {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            int option = chooser.showSaveDialog(demo);
            if (option == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                demo.writeJPEGImage(file);
            }
        }
    });
    menu.add(new AbstractAction("Print") {
        public void actionPerformed(ActionEvent e) {
            PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.setPrintable(demo);
            if (printJob.printDialog()) {
                try {
                    printJob.print();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    });
    JPopupMenu.setDefaultLightWeightPopupEnabled(false);
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(menu);
    frame.setJMenuBar(menuBar);
    frame.getContentPane().add(demo);
    frame.pack();
    frame.setVisible(true);
}

From source file:components.GlassPaneDemo.java

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread./* w  w w  .j a v a  2 s . co  m*/
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("GlassPaneDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Start creating and adding components.
    JCheckBox changeButton = new JCheckBox("Glass pane \"visible\"");
    changeButton.setSelected(false);

    //Set up the content pane, where the "main GUI" lives.
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new FlowLayout());
    contentPane.add(changeButton);
    contentPane.add(new JButton("Button 1"));
    contentPane.add(new JButton("Button 2"));

    //Set up the menu bar, which appears above the content pane.
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Menu");
    menu.add(new JMenuItem("Do nothing"));
    menuBar.add(menu);
    frame.setJMenuBar(menuBar);

    //Set up the glass pane, which appears over both menu bar
    //and content pane and is an item listener on the change
    //button.
    myGlassPane = new MyGlassPane(changeButton, menuBar, frame.getContentPane());
    changeButton.addItemListener(myGlassPane);
    frame.setGlassPane(myGlassPane);

    //Show the window.
    frame.pack();
    frame.setVisible(true);
}

From source file:GlassPaneDemo.java

/**
 * Create the GUI and show it. For thread safety, this method should be
 * invoked from the event-dispatching thread.
 *///from  w  ww.  j  a v  a2s  .c o  m
private static void createAndShowGUI() {
    // Create and set up the window.
    JFrame frame = new JFrame("GlassPaneDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Start creating and adding components.
    JCheckBox changeButton = new JCheckBox("Glass pane \"visible\"");
    changeButton.setSelected(false);

    // Set up the content pane, where the "main GUI" lives.
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new FlowLayout());
    contentPane.add(changeButton);
    contentPane.add(new JButton("Button 1"));
    contentPane.add(new JButton("Button 2"));

    // Set up the menu bar, which appears above the content pane.
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Menu");
    menu.add(new JMenuItem("Do nothing"));
    menuBar.add(menu);
    frame.setJMenuBar(menuBar);

    // Set up the glass pane, which appears over both menu bar
    // and content pane and is an item listener on the change
    // button.
    myGlassPane = new MyGlassPane(changeButton, menuBar, frame.getContentPane());
    changeButton.addItemListener(myGlassPane);
    frame.setGlassPane(myGlassPane);

    // Show the window.
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

/**
 * @param newMenu/*w  ww  .ja v  a  2s . c o  m*/
 * @param menuBar
 * @param index
 * @return The same JMenuBar, for cascading.
 *         TODO See if the same thing can be done with Container.add( component, index )
 */
public static JMenuBar addMenuAt(JMenu newMenu, JMenuBar menuBar, int index) {

    ArrayList menuList = new ArrayList();
    for (int i = 0; i < menuBar.getMenuCount(); i++) {
        if (i == index) {
            menuList.add(newMenu);
        }
        menuList.add(menuBar.getMenu(i));
    }
    menuBar.removeAll();
    //        menuBar = new JMenuBar();
    for (int i = 0; i < menuList.size(); i++) {
        JMenu menu = (JMenu) menuList.get(i);
        menuBar.add(menu);
    }
    return menuBar;
}

From source file:edu.ku.brc.specify.toycode.UpdatesApp.java

public static JMenu createMenu(final JMenuBar menuBar, final String label, final String mneu) {
    JMenu menu = null;/*from w  w  w .j  av  a  2  s  .  c  om*/
    try {
        menu = menuBar.add(new JMenu(label));
        //if (oSType != OSTYPE.MacOSX)
        {
            setMnemonic(menu, mneu);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return menu;
}

From source file:Main.java

/**
 * Populates a {@link JMenuBar} from the given list and {@link ActionMap}.
 * /* w  w  w . j  a v a 2 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));
            }
        }
    }
}