Example usage for javax.swing JMenuItem addActionListener

List of usage examples for javax.swing JMenuItem addActionListener

Introduction

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

Prototype

public void addActionListener(ActionListener l) 

Source Link

Document

Adds an ActionListener to the button.

Usage

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  va2s .  com
    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;
}

From source file:TabPanelwithImageIconCustom.java

public TabPanelwithImageIconCustom() {
    setSize(450, 350);/*from w ww.  jav  a  2s.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:Main.java

public Main() {
    setSize(450, 350);// ww  w  .jav  a  2 s  . co 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:PopUpColorMenu.java

private JMenuItem makeMenuItem(String label) {
    JMenuItem item = new JMenuItem(label);
    item.addActionListener(this);
    return item;/*from  w  ww  . j a  va2 s  .c  o  m*/
}

From source file:Test.java

public Test() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuItem item = new JMenuItem("Test1");
    item.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Menu item Test1");
        }//from   w ww . j  av a2 s  . co  m
    });
    menu.add(item);

    item = new JMenuItem("Test2");
    item.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Menu item Test2");
        }
    });
    menu.add(item);

    getContentPane().add(label);
    pack();
    setSize(300, 100);
}

From source file:Popup.java

public void init() {
    Container cp = getContentPane();
    cp.setLayout(new FlowLayout());
    cp.add(t);//w  w w .ja  va2s. co  m
    ActionListener al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            t.setText(((JMenuItem) e.getSource()).getText());
        }
    };
    JMenuItem m = new JMenuItem("Hither");
    m.addActionListener(al);
    popup.add(m);
    m = new JMenuItem("Yon");
    m.addActionListener(al);
    popup.add(m);
    m = new JMenuItem("Afar");
    m.addActionListener(al);
    popup.add(m);
    popup.addSeparator();
    m = new JMenuItem("Stay Here");
    m.addActionListener(al);
    popup.add(m);
    PopupListener pl = new PopupListener();
    addMouseListener(pl);
    t.addMouseListener(pl);
}

From source file:Main.java

private void myPopupEvent(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();
    JTree tree = (JTree) e.getSource();
    TreePath path = tree.getPathForLocation(x, y);
    if (path == null)
        return;// ww w.  j a va  2 s . c o m

    DefaultMutableTreeNode rightClickedNode = (DefaultMutableTreeNode) path.getLastPathComponent();

    TreePath[] selectionPaths = tree.getSelectionPaths();

    boolean isSelected = false;
    if (selectionPaths != null) {
        for (TreePath selectionPath : selectionPaths) {
            if (selectionPath.equals(path)) {
                isSelected = true;
            }
        }
    }
    if (!isSelected) {
        tree.setSelectionPath(path);
    }
    if (rightClickedNode.isLeaf()) {
        JPopupMenu popup = new JPopupMenu();
        final JMenuItem refreshMenuItem = new JMenuItem("refresh");
        refreshMenuItem.addActionListener(ev -> System.out.println("refresh!"));
        popup.add(refreshMenuItem);
        popup.show(tree, x, y);
    }
}

From source file:MainClass.java

public MainClass() {

    popup = new JPopupMenu();
    slider = new SliderMenuItem();

    popup.add(slider);//from w  w  w .  j a  v a  2  s.  c  o m
    popup.add(new JSeparator());

    JMenuItem ticks = new JCheckBoxMenuItem("Slider Tick Marks");
    ticks.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            slider.setPaintTicks(!slider.getPaintTicks());
        }
    });
    JMenuItem labels = new JCheckBoxMenuItem("Slider Labels");
    labels.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            slider.setPaintLabels(!slider.getPaintLabels());
        }
    });
    popup.add(ticks);
    popup.add(labels);

}

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 w w . ja va2  s.  co  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:DialogTest.java

public DialogFrame() {
    setTitle("DialogTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    // construct a File menu

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);/*from  www  .  j  a v a2 s .  c o  m*/
    JMenu fileMenu = new JMenu("File");
    menuBar.add(fileMenu);

    // add About and Exit menu items

    // The About item shows the About dialog

    JMenuItem aboutItem = new JMenuItem("About");
    aboutItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            if (dialog == null) // first time
                dialog = new AboutDialog(DialogFrame.this);
            dialog.setVisible(true); // pop up dialog
        }
    });
    fileMenu.add(aboutItem);

    // The Exit item exits the program

    JMenuItem exitItem = new JMenuItem("Exit");
    exitItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    });
    fileMenu.add(exitItem);
}