Example usage for javax.swing JFrame setJMenuBar

List of usage examples for javax.swing JFrame setJMenuBar

Introduction

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

Prototype

@BeanProperty(bound = false, hidden = true, description = "The menubar for accessing pulldown menus from this frame.")
public void setJMenuBar(final JMenuBar menubar) 

Source Link

Document

Sets the menubar for this frame.

Usage

From source file:Main.java

public static void main(final String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");

    // File->New, N - Mnemonic
    JMenuItem newMenuItem = new JMenuItem("New");
    fileMenu.add(newMenuItem);//from  www.j  a  v a  2  s. c  om

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

    fileMenu.setPopupMenuVisible(true);
}

From source file:DesktopDemo.java

public static void main(String[] args) {

    if (Desktop.isDesktopSupported()) {
        desktop = Desktop.getDesktop();
    } else {/*from  www.j a  v  a 2  s. co  m*/
        System.out.println("Desktop class is not supported");
        System.exit(1);
    }
    JMenuItem openItem = new JMenuItem("Open");
    JMenuItem editItem = new JMenuItem("Edit");
    JMenuItem printItem = new JMenuItem("Print");
    JMenuItem browseToItem = new JMenuItem("Go to www.java2s.com");
    JMenuItem mailToItem = new JMenuItem("Email to a@java.com");
    JMenu fileMenu = new JMenu("File");
    JMenu mailMenu = new JMenu("Email");
    JMenu browseMenu = new JMenu("Browser");

    openItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                try {
                    desktop.open(chooser.getSelectedFile().getAbsoluteFile());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    });
    fileMenu.add(openItem);

    editItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                try {
                    desktop.edit(chooser.getSelectedFile().getAbsoluteFile());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    });
    fileMenu.add(editItem);

    printItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                try {
                    desktop.print(chooser.getSelectedFile().getAbsoluteFile());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    });
    fileMenu.add(printItem);

    browseToItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                URI browseURI = new URI("www.java2s.com");
                desktop.browse(browseURI);
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }
    });
    browseMenu.add(browseToItem);

    mailToItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                URI mailURI = new URI("mailto:support@java.com");
                desktop.mail(mailURI);
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }
    });
    mailMenu.add(mailToItem);

    JMenuBar jMenuBar = new JMenuBar();
    jMenuBar.add(fileMenu);
    jMenuBar.add(browseMenu);
    jMenuBar.add(mailMenu);

    JFrame frame = new JFrame();
    frame.setTitle("Desktop Helper Applications");
    frame.setSize(300, 100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setJMenuBar(jMenuBar);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(final String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");
    fileMenu.setDelay(100);/*from  w  w w.  jav  a 2  s. c o m*/

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

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

From source file:Main.java

public static void main(final String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMenuLocation(10, 10);//from w w w .  j  a v  a  2 s.co m

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

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

From source file:VerticalMenuBar.java

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

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(fileMenu);/*  w w w  . j a  va 2 s  . c  o m*/

    JMenu editMenu = new JMenu("Edit");
    menuBar.add(editMenu);

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

From source file:Main.java

public static void main(final String args[]) {
    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.setAccelerator(KeyStroke.getKeyStroke("F"));

    // File->New, N - Mnemonic
    JMenuItem newMenuItem = new JMenuItem("New");
    fileMenu.add(newMenuItem);/*from ww  w.  j a  v a  2 s  .  co  m*/

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

}

From source file:Main.java

public static void main(String[] a) {
    final JFrame jf = new JFrame("JIFrameDemo Main Window");

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    screenSize.width -= 42;/*from w  w w.j  a v a2  s  .c o  m*/
    screenSize.height -= 42;
    jf.setSize(screenSize);
    jf.setLocation(20, 20);

    JMenuBar mb = new JMenuBar();
    jf.setJMenuBar(mb);
    JMenu fm = new JMenu("File");
    mb.add(fm);
    JMenuItem mi;
    fm.add(mi = new JMenuItem("Exit"));
    mi.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });

    JDesktopPane dtp = new JDesktopPane();
    //dtp.setBackground(Color.GREEN);
    jf.setContentPane(dtp);

    JInternalFrame mboxFrame = new JInternalFrame("Mail Reader", true, true, true, true);
    JLabel reader = new JLabel("Mail Reader Would Be Here");
    mboxFrame.setContentPane(reader);
    mboxFrame.setSize(400, 300);
    mboxFrame.setLocation(50, 50);
    mboxFrame.setVisible(true);
    dtp.add(mboxFrame);

    JInternalFrame compFrame = new JInternalFrame("Compose Mail", true, true, true, true);
    JLabel composer = new JLabel("Mail Compose Would Be Here");
    compFrame.setContentPane(composer);
    compFrame.setSize(300, 200);
    compFrame.setLocation(200, 200);
    compFrame.setVisible(true);
    dtp.add(compFrame);

    JInternalFrame listFrame = new JInternalFrame("Users", true, true, true, true);
    JLabel list = new JLabel("List of Users Would Be Here");
    listFrame.setContentPane(list);
    listFrame.setLocation(400, 400);
    listFrame.setSize(500, 200);
    listFrame.setVisible(true);
    dtp.add(listFrame);

    jf.setVisible(true);
    jf.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            jf.setVisible(false);
            jf.dispose();
            System.exit(0);
        }
    });
}

From source file:Main.java

public static void main(final String args[]) {
    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.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

    // File->New, N - Mnemonic
    JMenuItem newMenuItem = new JMenuItem("New");
    fileMenu.add(newMenuItem);// w w  w  .  j  av  a 2 s .  c  o  m

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

}

From source file:ActionsMenuBar.java

public static void main(String args[]) {
    final JFrame frame = new JFrame("TextAction Usage");
    Container contentPane = frame.getContentPane();
    final JScrollPane scrollPane = new JScrollPane();
    contentPane.add(scrollPane, BorderLayout.CENTER);

    final JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);

    ActionListener actionListener = new ActionListener() {
        JTextComponent component;

        public void actionPerformed(ActionEvent actionEvent) {
            // Determine which component selected
            String command = actionEvent.getActionCommand();
            if (command.equals("JTextField")) {
                component = new JTextField();
            } else if (command.equals("JPasswordField")) {
                component = new JPasswordField();
            } else if (command.equals("JTextArea")) {
                component = new JTextArea();
            } else if (command.equals("JTextPane")) {
                component = new JTextPane();
            } else {
                component = new JEditorPane();
            }/*from ww  w .  j  a  v  a 2s  .c  om*/
            scrollPane.setViewportView(component);
            // Process action list
            Action actions[] = component.getActions();
            menuBar.removeAll();
            menuBar.revalidate();
            JMenu menu = null;
            for (int i = 0, n = actions.length; i < n; i++) {
                if ((i % 10) == 0) {
                    menu = new JMenu("From " + i);
                    menuBar.add(menu);
                }
                menu.add(actions[i]);
            }
            menuBar.revalidate();
        }
    };

    String components[] = { "JTextField", "JPasswordField", "JTextArea", "JTextPane", "JEditorPane" };
    final Container componentsContainer = RadioButtonUtils.createRadioButtonGrouping(components,
            "Pick to List Actions", actionListener);
    contentPane.add(componentsContainer, BorderLayout.WEST);

    frame.setSize(400, 300);
    frame.setVisible(true);
}

From source file:AddingRemovingJMenu.java

public static void main(final String args[]) {
    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 w  w  .j a  v a 2  s  . co  m*/

    JMenu editMenu = new JMenu("Edit");
    menuBar.add(editMenu);

    menuBar.remove(0);

    menuBar.revalidate();

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