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

/**
 * Adds a MouseListener to the component specified that will show the popup
 * specified (at the position that the mouse was clicked) when the mouse is
 * right-clicked, or whatever mouse event returns true from the
 * {@link MouseEvent#isPopupTrigger()} method.<br/><br/>
 * /*w w w .  j  a v a2  s  .  co  m*/
 * @param c
 *            The component to add the mouse listener to
 * @param popup
 *            the popup to show whe the component is clicked
 */
public static void addPopup(Component c, final JPopupMenu popup) {
    c.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            if (e.isPopupTrigger())
                popup.show(e.getComponent(), e.getX(), e.getY());
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            mousePressed(e);
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            mousePressed(e);
        }
    });
}

From source file:Main.java

/**
 * Gives a component a popup menu//from   w ww.jav  a  2s  .co  m
 *
 * @param component
 *            The target component
 * @param popup
 *            The popup menu
 */

public static void setPopupMenu(final JComponent component, final JPopupMenu popup) {
    component.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(final MouseEvent e) {
            if (e.isPopupTrigger()) {
                popup.show(component, e.getX(), e.getY());
            }
        }

        @Override
        public void mouseReleased(final MouseEvent e) {
            if (e.isPopupTrigger()) {
                popup.show(component, e.getX(), e.getY());
            }
        }
    });
}

From source file:Main.java

/**
 * Attach popup menu on the given component.
 * //  w  w w.j a v a  2s. c om
 * @param component
 *            component to which the popupMenu is attached
 * @param popupMenu
 *            popupMenu to be attached
 */
public static void attachPopupMenu(final JComponent component, final JPopupMenu popupMenu) {
    component.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent e) {
            if (e.isPopupTrigger() && e.getComponent() instanceof JTable) {
                popupMenu.show(e.getComponent(), e.getX(), e.getY());
            }
        }

    });
}

From source file:Main.java

public static void addPopup(Component component, final JPopupMenu popup) {
    component.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            if (e.isPopupTrigger()) {
                showMenu(e);// w w  w  .  jav  a  2 s .co m
            }
        }

        public void mouseReleased(MouseEvent e) {
            if (e.isPopupTrigger()) {
                showMenu(e);
            }
        }

        private void showMenu(MouseEvent e) {
            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    });
}

From source file:com.googlecode.blaisemath.app.MenuConfig.java

private static JButton popupButton(String name, final JPopupMenu popup) {
    final JButton button = new JButton(name);
    button.addMouseListener(new MouseAdapter() {
        @Override/*from  www.  ja v  a  2  s .c  o  m*/
        public void mousePressed(MouseEvent e) {
            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    });
    return button;
}

From source file:Main.java

public static void showPopupMenu(final JPopupMenu popup, final Component component, int x, int y) {
    final Point p = new Point(x, y);
    SwingUtilities.convertPointToScreen(p, component);
    final Dimension size = popup.getPreferredSize();
    final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

    boolean horiz = false;
    boolean vert = false;

    final int origX = x;

    if ((p.x + size.width > screen.width) && (size.width < screen.width)) {
        x += (screen.width - p.x - size.width);
        horiz = true;//from   ww  w .  j a va 2  s .c  o m
    }

    if ((p.y + size.height > screen.height) && (size.height < screen.height)) {
        y += (screen.height - p.y - size.height);
        vert = true;
    }

    if (horiz && vert) {
        x = origX - size.width - 2;
    }
    popup.show(component, x, y);
}

From source file:de.ailis.xadrian.utils.SwingUtils.java

/**
 * Gives a component a popup menu/*  w ww.  j  a v a  2 s .  c  o  m*/
 * 
 * @param component
 *            The target component
 * @param popup
 *            The popup menu
 */
public static void setPopupMenu(final JComponent component, final JPopupMenu popup) {
    component.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(final MouseEvent e) {
            // Ignore mouse buttons outside of the normal range. This
            // fixes problems with trackpad scrolling.
            if (e.getButton() > MouseEvent.BUTTON3)
                return;

            if (e.isPopupTrigger()) {
                popup.show(component, e.getX(), e.getY());
            }
        }

        @Override
        public void mouseReleased(final MouseEvent e) {
            // Ignore mouse buttons outside of the normal range. This
            // fixes problems with trackpad scrolling.
            if (e.getButton() > MouseEvent.BUTTON3)
                return;

            if (e.isPopupTrigger()) {
                popup.show(component, e.getX(), e.getY());
            }
        }
    });
}

From source file:lu.lippmann.cdb.ext.hydviga.ui.GapsUIUtil.java

private static void addExportPopupMenu(final Instances ds, final ChartPanel cp) {
    cp.addChartMouseListener(new ChartMouseListener() {
        public void chartMouseClicked(ChartMouseEvent e) {
            final JPopupMenu jPopupMenu = new JPopupMenu("feur");
            final JMenuItem mi1 = new JMenuItem("Export as CSV");
            mi1.addActionListener(new ActionListener() {
                @Override//from w  w  w. j  a  va  2  s. c  o m
                public void actionPerformed(final ActionEvent e) {
                    final JFileChooser fc = new JFileChooser();
                    fc.setAcceptAllFileFilterUsed(false);
                    final int returnVal = fc.showSaveDialog(cp);
                    if (returnVal == JFileChooser.APPROVE_OPTION) {
                        try {
                            final File file = fc.getSelectedFile();
                            WekaDataAccessUtil.saveInstancesIntoCSVFile(ds, file);
                        } catch (final Exception ee) {
                            ee.printStackTrace();
                        }
                    }
                }
            });
            jPopupMenu.add(mi1);
            jPopupMenu.show(cp, e.getTrigger().getX(), e.getTrigger().getY());
        }

        public void chartMouseMoved(ChartMouseEvent e) {
        }
    });
}

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;/*from   ww w  .j  av  a 2  s.  co  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:com.mirth.connect.client.ui.components.MirthIconTextField.java

public MirthIconTextField(ImageIcon icon) {
    setIcon(icon);/*w w w .j  a  v a2 s  . c  om*/

    addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent evt) {
            if (isIconActive(evt) && iconPopupMenuComponent != null) {
                JPopupMenu iconPopupMenu = new JPopupMenu();
                iconPopupMenu.insert(iconPopupMenuComponent, 0);
                iconPopupMenu.show(evt.getComponent(), evt.getX(), evt.getY());
            }
        }
    });

    addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseMoved(MouseEvent evt) {
            int cursorType = getCursor().getType();

            if (isIconActive(evt)) {
                if (StringUtils.isNotBlank(alternateToolTipText)) {
                    MirthIconTextField.super.setToolTipText(alternateToolTipText);
                }

                if (iconPopupMenuComponent != null) {
                    if (cursorType != Cursor.HAND_CURSOR) {
                        setCursor(new Cursor(Cursor.HAND_CURSOR));
                    }
                } else {
                    if (cursorType != Cursor.DEFAULT_CURSOR) {
                        setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                    }
                }
            } else {
                if (StringUtils.isNotBlank(alternateToolTipText)) {
                    MirthIconTextField.super.setToolTipText(originalToolTipText);
                }

                if (cursorType != Cursor.TEXT_CURSOR) {
                    setCursor(new Cursor(Cursor.TEXT_CURSOR));
                }
            }
        }
    });
}