Example usage for javax.swing JMenuItem JMenuItem

List of usage examples for javax.swing JMenuItem JMenuItem

Introduction

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

Prototype

public JMenuItem(Action a) 

Source Link

Document

Creates a menu item whose properties are taken from the specified Action.

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);/*from w  w w .  j av a 2s  .  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);

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

        @Override
        public void actionPerformed(ActionEvent arg0) {

        }

    }, 0);
}

From source file:Main.java

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

        @Override// w ww .ja v a 2  s .  com
        public void actionPerformed(ActionEvent arg0) {
            System.out.println("action");

        }

    });
    bar.add(menu);

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

        @Override
        public void actionPerformed(ActionEvent arg0) {
            System.out.println("action");

        }

    });

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

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

From source file:Main.java

public Main() {
    super("Browser");
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JMenuBar mb = new JMenuBar();
    JMenu mFile = new JMenu("File");
    JMenuItem mi = new JMenuItem("Add Tab");
    ActionListener addTabl = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            addTab();/*from  w ww .j a  v a2s  .c  om*/
        }
    };
    mi.addActionListener(addTabl);
    mFile.add(mi);
    mb.add(mFile);
    setJMenuBar(mb);

    JPanel pnlURL = new JPanel();
    tp = new JTabbedPane();
    addTab();
    add(tp, BorderLayout.CENTER);
    setSize(300, 300);
    setVisible(true);
}

From source file:MenuAcceleratorKeyStroke.java

public MenuAcceleratorKeyStroke() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar bar = new JMenuBar();
    JMenu menu = new JMenu("File");
    menu.setMnemonic('f');
    bar.add(menu);/*from   w  ww  . j a  va 2 s.  c  o  m*/

    JMenuItem exit = new JMenuItem("Exit");
    exit.setMnemonic('x');
    exit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Exit performed");
            MenuAcceleratorKeyStroke.this.dispose();
            System.exit(0);
        }
    });
    menu.add(exit);

    menu = new JMenu("Edit");
    menu.setMnemonic('e');
    bar.add(menu);

    EditListener l = new EditListener();
    JMenuItem mi;
    mi = menu.add(new JMenuItem("Cut", 't'));
    mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK));
    mi.addActionListener(l);
    mi = menu.add(new JMenuItem("Copy", 'c'));
    mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK));
    mi.addActionListener(l);
    mi = menu.add(new JMenuItem("Paste", 'p'));
    mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK));
    mi.addActionListener(l);

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

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

From source file:Main.java

public PopupMenu() {
    super(BoxLayout.Y_AXIS);
    final JPopupMenu menu = new JPopupMenu("Options");
    for (int i = 1; i < 20; i++)
        menu.add(new JMenuItem("Option" + i));

    JLabel clickMe = new JLabel("ClickMe");
    clickMe.setAlignmentX(RIGHT_ALIGNMENT);
    clickMe.addMouseListener(new MouseAdapter() {
        @Override//w w w  . j  a  va2 s.  co  m
        public void mouseClicked(MouseEvent e) {
            menu.show(e.getComponent(), e.getX(), e.getY());
        }
    });
    add(clickMe);
}

From source file:Main.java

public Main() {
    int MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();

    KeyStroke exitKey = KeyStroke.getKeyStroke(KeyEvent.VK_W, MASK);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    JMenu fileMenu = new JMenu("File");
    JMenuItem fileExit = new JMenuItem(exitAction);
    fileMenu.add(fileExit);//from  w w w  .ja v  a2  s  .  c  o m
    JMenuBar menu = new JMenuBar();
    menu.add(fileMenu);

    JDialog d = new JDialog(this, "Dialog");
    JPanel p = new JPanel();
    p.getInputMap().put(exitKey, exitName);
    p.getActionMap().put(exitName, exitAction);
    p.add(new JButton(exitAction));
    d.add(p);
    d.pack();
    d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

    this.setJMenuBar(menu);
    this.pack();
    this.setSize(new Dimension(320, 240));
    this.setVisible(true);
    d.setLocation(this.getRootPane().getContentPane().getLocationOnScreen());
    d.setVisible(true);
}

From source file:Main.java

private void addCard(String name) {
    JLabel label = new JLabel(name);
    mainPanel.add(label, name);/*from w w w  .j a v  a2s.c  o  m*/
    menu.add(new JMenuItem(new MenuAction(name)));
}

From source file:Main.java

public Main() {
    popup = new JPopupMenu();
    ActionListener menuListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.out.println("Popup menu item [" + event.getActionCommand() + "] was pressed.");
        }/* w w w . j a v  a2 s  .co  m*/
    };
    JMenuItem item;
    popup.add(item = new JMenuItem(new ImageIcon("left.gif")));
    item.setHorizontalTextPosition(JMenuItem.RIGHT);
    item.addActionListener(menuListener);
    popup.add(item = new JMenuItem("Center", new ImageIcon("center.gif")));
    item.setHorizontalTextPosition(JMenuItem.RIGHT);
    item.addActionListener(menuListener);
    popup.add(item = new JMenuItem("Right", new ImageIcon("right.gif")));
    item.setHorizontalTextPosition(JMenuItem.RIGHT);
    item.addActionListener(menuListener);
    popup.add(item = new JMenuItem("Full", new ImageIcon("full.gif")));
    item.setHorizontalTextPosition(JMenuItem.RIGHT);
    item.addActionListener(menuListener);
    popup.addSeparator();
    popup.add(item = new JMenuItem("Settings . . ."));
    item.addActionListener(menuListener);

    popup.setLabel("Justification");
    popup.setBorder(new BevelBorder(BevelBorder.RAISED));
    popup.addPopupMenuListener(new PopupPrintListener());

    addMouseListener(new MousePopupListener());
}

From source file:Main.java

public Main() {
    popup = new JPopupMenu();
    ActionListener menuListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.out.println("Popup menu item [" + event.getActionCommand() + "] was pressed.");
        }/*  w  w w  .j a  va  2  s. c om*/
    };
    JMenuItem item;
    popup.add(item = new JMenuItem("Left"));
    item.setHorizontalTextPosition(JMenuItem.RIGHT);
    item.addActionListener(menuListener);
    popup.add(item = new JMenuItem("Center", new ImageIcon("center.gif")));
    item.setHorizontalTextPosition(JMenuItem.RIGHT);
    item.addActionListener(menuListener);
    popup.add(item = new JMenuItem("Right", new ImageIcon("right.gif")));
    item.setHorizontalTextPosition(JMenuItem.RIGHT);
    item.addActionListener(menuListener);
    popup.add(item = new JMenuItem("Full", new ImageIcon("full.gif")));
    item.setHorizontalTextPosition(JMenuItem.RIGHT);
    item.addActionListener(menuListener);
    popup.addSeparator();
    popup.add(item = new JMenuItem("Settings . . ."));
    item.addActionListener(menuListener);

    popup.setLabel("Justification");
    popup.setBorder(new BevelBorder(BevelBorder.RAISED));
    popup.addPopupMenuListener(new PopupPrintListener());

    addMouseListener(new MousePopupListener());
}

From source file:Main.java

private JMenu getDialogMenu() {
    JMenu myMenu = new JMenu("Dialogs");
    JMenuItem myItem = new JMenuItem("Information");
    myItem.addActionListener(this);
    myMenu.add(myItem);//from  w w w. j a va  2s  .co  m
    myItem = new JMenuItem("Warning");
    myItem.addActionListener(this);
    myMenu.add(myItem);
    myItem = new JMenuItem("Error");
    myItem.addActionListener(this);
    myMenu.add(myItem);
    myItem = new JMenuItem("Plain");
    myItem.addActionListener(this);
    myMenu.add(myItem);
    return myMenu;
}