Example usage for javax.swing JPopupMenu show

List of usage examples for javax.swing JPopupMenu show

Introduction

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

Prototype

public void show(Component invoker, int x, int y) 

Source Link

Document

Displays the popup menu at the position x,y in the coordinate space of the component invoker.

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(400, 400);//  ww w.  j  a va  2 s.  c o m
    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: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 . com

    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: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  www  .  jav a  2  s  .  c om

    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: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  .  ja  v  a 2  s .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 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 w ww  .j av a 2  s .  co 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: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();//from  w ww .j  ava2 s  . co  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:MenuDemo1.java

public static void main(String[] args) {
    // Create a window for this demo
    JFrame frame = new JFrame("Menu Demo");
    JPanel panel = new JPanel();
    frame.getContentPane().add(panel, "Center");

    // Create an action listener for the menu items we will create
    // The MenuItemActionListener class is defined below
    ActionListener listener = new MenuItemActionListener(panel);

    // Create some menu panes, and fill them with menu items
    // The menuItem() method is important.  It is defined below.
    JMenu file = new JMenu("File");
    file.setMnemonic('F');
    file.add(menuItem("New", listener, "new", 'N', KeyEvent.VK_N));
    file.add(menuItem("Open...", listener, "open", 'O', KeyEvent.VK_O));
    file.add(menuItem("Save", listener, "save", 'S', KeyEvent.VK_S));
    file.add(menuItem("Save As...", listener, "saveas", 'A', KeyEvent.VK_A));

    JMenu edit = new JMenu("Edit");
    edit.setMnemonic('E');
    edit.add(menuItem("Cut", listener, "cut", 0, KeyEvent.VK_X));
    edit.add(menuItem("Copy", listener, "copy", 'C', KeyEvent.VK_C));
    edit.add(menuItem("Paste", listener, "paste", 0, KeyEvent.VK_V));

    // Create a menubar and add these panes to it.
    JMenuBar menubar = new JMenuBar();
    menubar.add(file);/*from   w ww . j  a v  a2s. com*/
    menubar.add(edit);

    // Add menubar to the main window.  Note special method to add menubars
    frame.setJMenuBar(menubar);

    // Now create a popup menu and add the some stuff to it
    final JPopupMenu popup = new JPopupMenu();
    popup.add(menuItem("Open...", listener, "open", 0, 0));
    popup.addSeparator(); // Add a separator between items
    JMenu colors = new JMenu("Colors"); // Create a submenu
    popup.add(colors); // and add it to the popup menu
    // Now fill the submenu with mutually-exclusive radio buttons
    ButtonGroup colorgroup = new ButtonGroup();
    colors.add(radioItem("Red", listener, "color(red)", colorgroup));
    colors.add(radioItem("Green", listener, "color(green)", colorgroup));
    colors.add(radioItem("Blue", listener, "color(blue)", colorgroup));

    // Arrange to display the popup menu when the user clicks in the window
    panel.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            // Check whether this is the right type of event to pop up a popup
            // menu on this platform.  Usually checks for right button down.
            if (e.isPopupTrigger())
                popup.show((Component) e.getSource(), e.getX(), e.getY());
        }
    });

    // Finally, make our main window appear
    frame.setSize(450, 300);
    frame.setVisible(true);
}

From source file:Main.java

public static void showPopMenu(JPopupMenu popupMenu, MouseEvent e, Component source, Component destination) {
    final MouseEvent event = SwingUtilities.convertMouseEvent(source, e, destination);
    popupMenu.show(destination, event.getX(), event.getY());
}

From source file:Main.java

/**
 * This method exists because popup menus can not be directly moved
 * (the have to be hidden and re-shown).
 *///from w  w w .  java 2s .c  om
public static void setLocationOnScreen(JPopupMenu visibleMenu, int x, int y) {
    Point invokerLocation = visibleMenu.getInvoker().getLocationOnScreen();
    visibleMenu.setVisible(false);

    visibleMenu.show(visibleMenu.getInvoker(), x - invokerLocation.x, y - invokerLocation.y);
}

From source file:Main.java

public static final void addPopup(final Component c, final JPopupMenu m) {
    c.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            if (e.isPopupTrigger())
                m.show(c, e.getX(), e.getY());
        }/*from  w ww  . ja  va2  s  .  co  m*/
    });
}