Example usage for javax.swing JPopupMenu JPopupMenu

List of usage examples for javax.swing JPopupMenu JPopupMenu

Introduction

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

Prototype

public JPopupMenu() 

Source Link

Document

Constructs a JPopupMenu without an "invoker".

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JPopupMenu popupMenu = new JPopupMenu();

    boolean lwPopup = popupMenu.isLightWeightPopupEnabled(); // true

    popupMenu.setLightWeightPopupEnabled(false);

}

From source file:Main.java

License:asdf

public static void main(String[] argv) throws Exception {
    final JPopupMenu popupMenu = new JPopupMenu();

    JMenu submenu = new JMenu("SubMenu1");
    submenu.add("asdf");
    submenu.add("asdf");

    // Add submenu to popup menu
    popupMenu.add(submenu);// ww w  . j ava2 s. c  o m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    final JPopupMenu menu = new JPopupMenu();

    JMenuItem item = new JMenuItem("Item Label");
    menu.add(item);// ww  w .ja va 2 s  . c om

    JButton component = new JButton("button");
    component.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent evt) {
            if (evt.isPopupTrigger()) {
                menu.show(evt.getComponent(), evt.getX(), evt.getY());
            }
        }

        public void mouseReleased(MouseEvent evt) {
            if (evt.isPopupTrigger()) {
                menu.show(evt.getComponent(), evt.getX(), evt.getY());
            }
        }
    });

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    final JPopupMenu menu = new JPopupMenu();

    JMenuItem item = new JMenuItem("Item Label");
    //  item.addActionListener(actionListener);
    menu.add(item);//w w  w . j a  v  a 2s  .  c  o  m

    JButton component = new JButton("button");
    component.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent evt) {
            if (evt.isPopupTrigger()) {
                menu.show(evt.getComponent(), evt.getX(), evt.getY());
            }
        }

        public void mouseReleased(MouseEvent evt) {
            if (evt.isPopupTrigger()) {
                menu.show(evt.getComponent(), evt.getX(), evt.getY());
            }
        }
    });

}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPopupMenu popupMenu = new JPopupMenu();
    popupMenu.add(new JMenuItem("One"));
    popupMenu.add(new JMenuItem("Two"));
    popupMenu.add(new JMenuItem("Three"));
    JList<String> list = new JList<>(
            new String[] { "Hello", "World", "Something", "Else", "Out", "Of", "Ideas" });
    list.setComponentPopupMenu(popupMenu);
    f.add(list);//  ww  w  .  ja  v  a 2 s.  c  om
    f.pack();
    f.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(400, 400);/*from w  w w.j  a  va2s  .  com*/
    frame.setVisible(true);
    String[] list = { "1", "2", "3", "4", };
    JComboBox<String> comb = new JComboBox<>(list);
    final JPopupMenu pop = new JPopupMenu();
    pop.add(comb);
    frame.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            System.out.println("mousePressed");
            pop.show(e.getComponent(), e.getX(), e.getY());
        }
    });
}

From source file:PopupMenu.java

public static void main(String[] args) {
    JFrame frame = new JFrame("JPopupMenu");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final Toolkit toolkit = frame.getToolkit();

    final JPopupMenu menu = new JPopupMenu();
    JMenuItem menuItemBeep = new JMenuItem("Beep");

    menuItemBeep.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            toolkit.beep();/* ww w  . j a v  a2s.c  o  m*/
        }
    });

    menu.add(menuItemBeep);

    JMenuItem menuItemClose = new JMenuItem("Close");
    menuItemClose.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });

    menu.add(menuItemClose);
    frame.addMouseListener(new MouseAdapter() {
        public void mouseReleased(MouseEvent e) {
            if (e.getButton() == e.BUTTON3) {
                menu.show(e.getComponent(), e.getX(), e.getY());
            }
        }
    });

    frame.setSize(250, 200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Popup Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JPopupMenu popup = new JPopupMenu();
    JMenuItem menuItem1 = new JMenuItem("Option 1");
    popup.add(menuItem1);//from ww w .  j a  v  a 2s .c  o m

    JMenuItem menuItem2 = new JMenuItem("Option 2");
    popup.add(menuItem2);

    final JTextField textField = new JTextField();
    frame.add(textField, BorderLayout.NORTH);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            try {
                int dotPosition = textField.getCaretPosition();
                Rectangle popupLocation = textField.modelToView(dotPosition);
                popup.show(textField, popupLocation.x, popupLocation.y);
            } catch (BadLocationException badLocationException) {
                System.err.println("Oops");
            }
        }
    };
    KeyStroke keystroke = KeyStroke.getKeyStroke(KeyEvent.VK_PERIOD, 0, false);
    textField.registerKeyboardAction(actionListener, keystroke, JComponent.WHEN_FOCUSED);

    frame.add(new JLabel("Press '.' to activate Popup menu"), BorderLayout.SOUTH);
    frame.setSize(250, 150);
    frame.setVisible(true);
}

From source file:MainClass.java

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

    final JTextField textField = new JTextField();
    frame.add(textField, BorderLayout.NORTH);

    final JPopupMenu popup = new JPopupMenu();
    JMenuItem menuItem1 = new JMenuItem("Option 1");
    popup.add(menuItem1);/*from ww w  .j a  v a2  s  .  co  m*/

    JMenuItem menuItem2 = new JMenuItem("Option 2");
    popup.add(menuItem2);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            popup.show(textField, 10, 10);
        }
    };

    KeyStroke keystroke = KeyStroke.getKeyStroke(KeyEvent.VK_PERIOD, 0, false);
    textField.registerKeyboardAction(actionListener, keystroke, JComponent.WHEN_FOCUSED);

    frame.setSize(250, 150);
    frame.setVisible(true);
}

From source file:PopupSample.java

public static void main(String args[]) {
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Selected: " + actionEvent.getActionCommand());
        }//from w  w w .  ja v  a  2s  . co m
    };

    PopupMenuListener popupMenuListener = new PopupMenuListener() {
        public void popupMenuCanceled(PopupMenuEvent popupMenuEvent) {
            System.out.println("Canceled");
        }

        public void popupMenuWillBecomeInvisible(PopupMenuEvent popupMenuEvent) {
            System.out.println("Becoming Invisible");
        }

        public void popupMenuWillBecomeVisible(PopupMenuEvent popupMenuEvent) {
            System.out.println("Becoming Visible");
        }
    };

    JFrame frame = new JFrame("Popup Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPopupMenu popupMenu = new JPopupMenu();
    popupMenu.addPopupMenuListener(popupMenuListener);

    JMenuItem cutMenuItem = new JMenuItem("Cut");
    cutMenuItem.addActionListener(actionListener);
    popupMenu.add(cutMenuItem);

    JMenuItem copyMenuItem = new JMenuItem("Copy");
    copyMenuItem.addActionListener(actionListener);
    popupMenu.add(copyMenuItem);

    JMenuItem pasteMenuItem = new JMenuItem("Paste");
    pasteMenuItem.addActionListener(actionListener);
    pasteMenuItem.setEnabled(false);
    popupMenu.add(pasteMenuItem);

    popupMenu.addSeparator();

    JMenuItem findMenuItem = new JMenuItem("Find");
    findMenuItem.addActionListener(actionListener);
    popupMenu.add(findMenuItem);

    MouseListener mouseListener = new JPopupMenuShower(popupMenu);
    frame.addMouseListener(mouseListener);

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