Example usage for javax.swing JMenu JMenu

List of usage examples for javax.swing JMenu JMenu

Introduction

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

Prototype

public JMenu(Action a) 

Source Link

Document

Constructs a menu whose properties are taken from the Action supplied.

Usage

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar bar = new JMenuBar();
    JMenu menu = new JMenu("File");
    ComponentOrientation ori = ComponentOrientation.LEFT_TO_RIGHT;

    menu.applyComponentOrientation(ori);
    bar.add(menu);/*w w w. j  a v a 2 s  . co  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);

    menu.insert(new AbstractAction("Action") {

        @Override
        public void actionPerformed(ActionEvent arg0) {

        }

    }, 0);
}

From source file:Main.java

Main() {
    JFrame f = new JFrame("Menu Demo");
    f.setSize(220, 200);/*www.j a  v  a 2s  . c om*/

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar jmb = new JMenuBar();

    JMenu jmFile = new JMenu("File");
    JMenuItem jmiOpen = new JMenuItem("Open");
    JMenuItem jmiClose = new JMenuItem("Close");
    JMenuItem jmiSave = new JMenuItem("Save");
    JMenuItem jmiExit = new JMenuItem("Exit");
    jmFile.add(jmiOpen);
    jmFile.add(jmiClose);
    jmFile.add(jmiSave);
    jmFile.addSeparator();
    jmFile.add(jmiExit);
    jmb.add(jmFile);

    JMenu jmOptions = new JMenu("Options");
    JMenu a = new JMenu("A");
    JMenuItem b = new JMenuItem("B");
    JMenuItem c = new JMenuItem("C");
    JMenuItem d = new JMenuItem("D");
    a.add(b);
    a.add(c);
    a.add(d);
    jmOptions.add(a);

    JMenu e = new JMenu("E");
    e.add(new JMenuItem("F"));
    e.add(new JMenuItem("G"));
    jmOptions.add(e);

    jmb.add(jmOptions);

    JMenu jmHelp = new JMenu("Help");
    JMenuItem jmiAbout = new JMenuItem("About");
    jmHelp.add(jmiAbout);
    jmb.add(jmHelp);

    jmiOpen.addActionListener(this);
    jmiClose.addActionListener(this);
    jmiSave.addActionListener(this);
    jmiExit.addActionListener(this);
    b.addActionListener(this);
    c.addActionListener(this);
    d.addActionListener(this);
    jmiAbout.addActionListener(this);

    f.setJMenuBar(jmb);
    f.setVisible(true);
}

From source file:Main.java

public Main() {
    JMenuBar bar = new JMenuBar();
    JMenu addMenu = new JMenu("Add");
    JMenuItem newFrame = new JMenuItem("Internal Frame");
    addMenu.add(newFrame);//from w ww.  j a  v a 2 s  .  co m
    bar.add(addMenu);
    setJMenuBar(bar);

    final JDesktopPane theDesktop = new JDesktopPane();
    getContentPane().add(theDesktop);

    newFrame.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JInternalFrame frame = new JInternalFrame("Internal Frame", true, true, true, true);

            Container c = frame.getContentPane();
            MyJPanel panel = new MyJPanel();

            c.add(panel, BorderLayout.CENTER);
            frame.setSize(200, 200);
            frame.setOpaque(true);
            theDesktop.add(frame);
        }
    });

    setSize(500, 400);
    setVisible(true);
}

From source file:Main.java

public MainFrame() {

    setDefaultCloseOperation(EXIT_ON_CLOSE);

    setJMenuBar(menuBar); // Add the menu bar to the window

    JMenu fileMenu = new JMenu("File"); // Create File menu
    JMenu elementMenu = new JMenu("Elements"); // Create Elements menu
    menuBar.add(fileMenu); // Add the file menu
    menuBar.add(elementMenu); // Add the element menu
}

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 av a2  s . co  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);
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    setJMenuBar(menuBar);/*w  ww .  ja  v  a 2  s . c o m*/

    JMenu fileMenu = new JMenu("File");
    JMenu elementMenu = new JMenu("Elements");
    fileMenu.setMnemonic('F');
    elementMenu.setMnemonic('E');

    // Construct the file pull down menu
    newItem = fileMenu.add("New");
    openItem = fileMenu.add("Open");
    closeItem = fileMenu.add("Close");
    fileMenu.addSeparator();
    saveItem = fileMenu.add("Save");
    saveAsItem = fileMenu.add("Save As...");

    JMenu colorMenu = new JMenu("Color");

    fileMenu.addSeparator();
    printItem = fileMenu.add("Print");

    newItem.setAccelerator(KeyStroke.getKeyStroke('N', Event.CTRL_MASK));
    openItem.setAccelerator(KeyStroke.getKeyStroke('O', Event.CTRL_MASK));
    saveItem.setAccelerator(KeyStroke.getKeyStroke('S', Event.CTRL_MASK));
    printItem.setAccelerator(KeyStroke.getKeyStroke('P', Event.CTRL_MASK));

    elementMenu.add(lineItem = new JRadioButtonMenuItem("Line", true));
    elementMenu.add(rectangleItem = new JRadioButtonMenuItem("Rectangle", false));
    elementMenu.add(circleItem = new JRadioButtonMenuItem("Circle", false));
    elementMenu.add(curveItem = new JRadioButtonMenuItem("Curve", false));
    ButtonGroup types = new ButtonGroup();
    types.add(lineItem);
    types.add(rectangleItem);
    types.add(circleItem);
    types.add(curveItem);

    lineItem.setAccelerator(KeyStroke.getKeyStroke('L', Event.CTRL_MASK));
    rectangleItem.setAccelerator(KeyStroke.getKeyStroke('E', Event.CTRL_MASK));
    circleItem.setAccelerator(KeyStroke.getKeyStroke('I', Event.CTRL_MASK));
    curveItem.setAccelerator(KeyStroke.getKeyStroke('V', Event.CTRL_MASK));

    elementMenu.addSeparator();

    elementMenu.add(colorMenu); // Add the sub-menu
    colorMenu.add(redItem = new JCheckBoxMenuItem("Red", false));
    colorMenu.add(yellowItem = new JCheckBoxMenuItem("Yellow", false));
    colorMenu.add(greenItem = new JCheckBoxMenuItem("Green", false));
    colorMenu.add(blueItem = new JCheckBoxMenuItem("Blue", true));

    // Add element color accelerators
    redItem.setAccelerator(KeyStroke.getKeyStroke('R', Event.CTRL_MASK));
    yellowItem.setAccelerator(KeyStroke.getKeyStroke('Y', Event.CTRL_MASK));
    greenItem.setAccelerator(KeyStroke.getKeyStroke('G', Event.CTRL_MASK));
    blueItem.setAccelerator(KeyStroke.getKeyStroke('B', Event.CTRL_MASK));

    menuBar.add(fileMenu);
    menuBar.add(elementMenu);
}

From source file:MainClass.java

public MainClass() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    setJMenuBar(menuBar);//www  .  ja v a 2 s.  c  o m

    JMenu fileMenu = new JMenu("File");
    JMenu elementMenu = new JMenu("Elements");
    fileMenu.setMnemonic('F');
    elementMenu.setMnemonic('E');

    // Construct the file pull down menu
    newItem = fileMenu.add("New");
    openItem = fileMenu.add("Open");
    closeItem = fileMenu.add("Close");
    fileMenu.addSeparator();
    saveItem = fileMenu.add("Save");
    saveAsItem = fileMenu.add("Save As...");

    JMenu colorMenu = new JMenu("Color");

    fileMenu.addSeparator();
    printItem = fileMenu.add("Print");

    newItem.setAccelerator(KeyStroke.getKeyStroke('N', Event.CTRL_MASK));
    openItem.setAccelerator(KeyStroke.getKeyStroke('O', Event.CTRL_MASK));
    saveItem.setAccelerator(KeyStroke.getKeyStroke('S', Event.CTRL_MASK));
    printItem.setAccelerator(KeyStroke.getKeyStroke('P', Event.CTRL_MASK));

    elementMenu.add(lineItem = new JRadioButtonMenuItem("Line", true));
    elementMenu.add(rectangleItem = new JRadioButtonMenuItem("Rectangle", false));
    elementMenu.add(circleItem = new JRadioButtonMenuItem("Circle", false));
    elementMenu.add(curveItem = new JRadioButtonMenuItem("Curve", false));
    ButtonGroup types = new ButtonGroup();
    types.add(lineItem);
    types.add(rectangleItem);
    types.add(circleItem);
    types.add(curveItem);

    lineItem.setAccelerator(KeyStroke.getKeyStroke('L', Event.CTRL_MASK));
    rectangleItem.setAccelerator(KeyStroke.getKeyStroke('E', Event.CTRL_MASK));
    circleItem.setAccelerator(KeyStroke.getKeyStroke('I', Event.CTRL_MASK));
    curveItem.setAccelerator(KeyStroke.getKeyStroke('V', Event.CTRL_MASK));

    elementMenu.addSeparator();

    elementMenu.add(colorMenu); // Add the sub-menu
    colorMenu.add(redItem = new JCheckBoxMenuItem("Red", false));
    colorMenu.add(yellowItem = new JCheckBoxMenuItem("Yellow", false));
    colorMenu.add(greenItem = new JCheckBoxMenuItem("Green", false));
    colorMenu.add(blueItem = new JCheckBoxMenuItem("Blue", true));

    // Add element color accelerators
    redItem.setAccelerator(KeyStroke.getKeyStroke('R', Event.CTRL_MASK));
    yellowItem.setAccelerator(KeyStroke.getKeyStroke('Y', Event.CTRL_MASK));
    greenItem.setAccelerator(KeyStroke.getKeyStroke('G', Event.CTRL_MASK));
    blueItem.setAccelerator(KeyStroke.getKeyStroke('B', Event.CTRL_MASK));

    menuBar.add(fileMenu);
    menuBar.add(elementMenu);
}

From source file:Main.java

public Main() {
    setTitle("ZipTest");
    setSize(300, 400);//from   ww  w .j  a v a2s .c om

    JMenuBar mbar = new JMenuBar();
    JMenu m = new JMenu("File");
    openItem = new JMenuItem("Open");
    openItem.addActionListener(this);
    m.add(openItem);
    exitItem = new JMenuItem("Exit");
    exitItem.addActionListener(this);
    m.add(exitItem);
    mbar.add(m);

    Container contentPane = getContentPane();
    contentPane.add(mbar, "North");
}

From source file:ImageViewer.java

public ImageViewer() {
    setTitle("ImageViewer");
    setSize(300, 400);//  w w w.  j  a v  a  2  s  .c  o m

    JMenuBar mbar = new JMenuBar();
    JMenu m = new JMenu("File");
    openItem = new JMenuItem("Open");
    openItem.addActionListener(this);
    m.add(openItem);
    exitItem = new JMenuItem("Exit");
    exitItem.addActionListener(this);
    m.add(exitItem);
    mbar.add(m);
    setJMenuBar(mbar);

    label = new JLabel();
    Container contentPane = getContentPane();
    contentPane.add(label, "Center");
}

From source file:FileChooserDemo.java

public FileChooserDemo() {
    setTitle("ZipTest");
    setSize(300, 400);//  ww  w  . jav  a 2 s  . com

    JMenuBar mbar = new JMenuBar();
    JMenu m = new JMenu("File");
    openItem = new JMenuItem("Open");
    openItem.addActionListener(this);
    m.add(openItem);
    exitItem = new JMenuItem("Exit");
    exitItem.addActionListener(this);
    m.add(exitItem);
    mbar.add(m);

    Container contentPane = getContentPane();
    contentPane.add(mbar, "North");
}