Example usage for javax.swing JPopupMenu setInvoker

List of usage examples for javax.swing JPopupMenu setInvoker

Introduction

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

Prototype

@BeanProperty(bound = false, expert = true, description = "The invoking component for the popup menu")
public void setInvoker(Component invoker) 

Source Link

Document

Sets the invoker of this popup menu -- the component in which the popup menu menu is to be displayed.

Usage

From source file:Main.java

/**
 * Gives a tray icon a popup menu.//from  w w w .  j a v a2s  .com
 *
 * @param trayIcon
 *            The tray icon
 * @param popup
 *            The popup menu
 */

public static void setPopupMenu(final TrayIcon trayIcon, final JPopupMenu popup) {
    trayIcon.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(final MouseEvent e) {
            if (e.isPopupTrigger()) {
                popup.setLocation(e.getX(), e.getY());
                popup.setInvoker(popup);
                popup.setVisible(true);
            }
        }

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

From source file:MouseTest.java

public MouseTest() {
    super();//  ww  w. j a  v  a2s.c om

    final JPopupMenu pop = new JPopupMenu();
    pop.add(new JMenuItem("Cut"));
    pop.add(new JMenuItem("Copy"));
    pop.add(new JMenuItem("Paste"));
    pop.addSeparator();
    pop.add(new JMenuItem("Select All"));
    pop.setInvoker(this);

    MouseListener popup = new MouseListener() {
        public void mouseClicked(MouseEvent e) {
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }

        public void mousePressed(MouseEvent e) {
            if (e.isPopupTrigger()) {
                showPopup(e);
            }
        }

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

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

    MouseListener drawing1 = new MouseListener() {
        public void mouseClicked(MouseEvent e) {
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }

        public void mousePressed(MouseEvent e) {
            color = Color.RED;
            startX = endX = e.getX();
            startY = endY = e.getY();
            repaint();
        }

        public void mouseReleased(MouseEvent e) {
            color = Color.BLACK;
            repaint();
        }
    };
    addMouseListener(drawing1);

    MouseMotionListener drawing2 = new MouseMotionListener() {
        public void mouseDragged(MouseEvent e) {
            endX = e.getX();
            endY = e.getY();
            repaint();
        }

        public void mouseMoved(MouseEvent e) {
        }
    };
    addMouseMotionListener(drawing2);

}

From source file:jatoo.app.App.java

private JPopupMenu getWindowPopup(final Point location) {

    ////w  ww.ja va 2s. c  om
    // hide

    final JMenuItem hideItem = new JMenuItem(getText("popup.hide"));
    hideItem.addActionListener(new ActionListener() {
        public void actionPerformed(final ActionEvent e) {
            hide();
        }
    });

    //
    // send to back

    final JMenuItem sendToBackItem = new JMenuItem(getText("popup.send_to_back"));
    sendToBackItem.addActionListener(new ActionListener() {
        public void actionPerformed(final ActionEvent e) {
            sendToBack();
        }
    });

    //
    // always on top

    final JCheckBoxMenuItem alwaysOnTopItem = new JCheckBoxMenuItem(getText("popup.always_on_top"),
            isAlwaysOnTop());
    alwaysOnTopItem.addItemListener(new ItemListener() {
        public void itemStateChanged(final ItemEvent e) {
            setAlwaysOnTop(alwaysOnTopItem.isSelected());
        }
    });

    //
    // transparency

    final JSlider transparencySlider = new JSlider(JSlider.VERTICAL, 0, 100, getTransparency());
    transparencySlider.setMajorTickSpacing(25);
    transparencySlider.setMinorTickSpacing(5);
    transparencySlider.setSnapToTicks(true);
    transparencySlider.setPaintTicks(true);
    transparencySlider.setPaintLabels(true);
    transparencySlider.addChangeListener(new ChangeListener() {
        public void stateChanged(final ChangeEvent e) {
            setTransparency(transparencySlider.getValue());
        }
    });

    final JMenu transparencyItem = new JMenu(getText("popup.transparency"));
    transparencyItem.add(transparencySlider);

    //
    // close

    final JMenuItem closeItem = new JMenuItem(getText("popup.close"), getIcon("close-016.png"));
    closeItem.addActionListener(new ActionListener() {
        public void actionPerformed(final ActionEvent e) {
            System.exit(0);
        }
    });

    //
    // the popup

    JPopupMenu popup = new JPopupMenu(getTitle());

    popup.add(hideItem);
    popup.addSeparator();
    popup.add(sendToBackItem);
    popup.add(alwaysOnTopItem);
    popup.add(transparencyItem);
    popup.addSeparator();
    popup.add(closeItem);

    popup.setInvoker(popup);
    popup.setLocation(location);

    return popup;
}

From source file:net.java.sip.communicator.impl.gui.main.chat.ChatWritePanel.java

/**
 * Opens the <tt>WritePanelRightButtonMenu</tt> when user clicks with the
 * right mouse button on the editor area.
 *
 * @param e the <tt>MouseEvent</tt> that notified us
 */// w  ww. ja v  a 2 s  . c  o m
public void mouseClicked(MouseEvent e) {
    if ((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0 || (e.isControlDown() && !e.isMetaDown())) {
        Point p = e.getPoint();
        SwingUtilities.convertPointToScreen(p, e.getComponent());

        //SPELLCHECK
        ArrayList<JMenuItem> contributedMenuEntries = new ArrayList<JMenuItem>();

        for (ChatMenuListener listener : this.menuListeners) {
            contributedMenuEntries.addAll(listener.getMenuElements(this.chatPanel, e));
        }

        for (JMenuItem item : contributedMenuEntries) {
            rightButtonMenu.add(item);
        }

        JPopupMenu rightMenu = rightButtonMenu.makeMenu(contributedMenuEntries);
        rightMenu.setInvoker(editorPane);
        rightMenu.setLocation(p.x, p.y);
        rightMenu.setVisible(true);
    }
}

From source file:Main.java

public static JPopupMenu createStdEditPopupMenu(final JTextComponent[] fields) {
    final JPopupMenu popupMenu = new JPopupMenu();

    /* text fields popup menu: "Cut" */
    final JMenuItem cutMenuItem = new JMenuItem("Cut", 't');
    cutMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            final Component c = popupMenu.getInvoker();

            if (c instanceof JTextComponent) {
                ((JTextComponent) c).cut();
            }//from  w ww . ja  v a 2s  .c o m
        }
    });
    popupMenu.add(cutMenuItem);

    /* text fields popup menu: "Copy" */
    final JMenuItem copyMenuItem = new JMenuItem("Copy", 'C');
    copyMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            final Component c = popupMenu.getInvoker();

            if (c instanceof JTextComponent) {
                ((JTextComponent) c).copy();
            }
        }
    });
    popupMenu.add(copyMenuItem);

    /* text fields popup menu: "Paste" */
    final JMenuItem pasteMenuItem = new JMenuItem("Paste", 'P');
    pasteMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            final Component c = popupMenu.getInvoker();

            if (c instanceof JTextComponent) {
                ((JTextComponent) c).paste();
            }
        }
    });
    popupMenu.add(pasteMenuItem);
    popupMenu.addSeparator();

    /* text fields popup menu: "Select All" */
    final JMenuItem selectAllMenuItem = new JMenuItem("Select All", 'A');
    selectAllMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            final Component c = popupMenu.getInvoker();

            if (c instanceof JTextComponent) {
                ((JTextComponent) c).selectAll();
            }
        }
    });
    popupMenu.add(selectAllMenuItem);

    /* add mouse listeners to the specified fields */
    for (final JTextComponent f : fields) {
        f.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                processMouseEvent(e);
            }

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

            private void processMouseEvent(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    popupMenu.show(e.getComponent(), e.getX(), e.getY());
                    popupMenu.setInvoker(f);
                }
            }
        });
    }
    return popupMenu;
}

From source file:edu.ku.brc.ui.tmanfe.SpreadSheet.java

/**
 * CReates the popup menu for a cell. (THis really needs to be moved outside of this class).
 * @param pnt the point to pop it up/*from  w  w  w .  j  a v  a 2s .  co  m*/
 * @return the popup menu
 */
protected JPopupMenu createMenuForSelection(final Point pnt) {
    //final int row = rowAtPoint(pnt);

    Class<?> cellClass = getModel().getColumnClass(convertColumnIndexToModel(columnAtPoint(pnt)));
    boolean isImage = cellClass == ImageIcon.class || cellClass == Image.class;

    JPopupMenu pMenu = new JPopupMenu();
    UsageTracker.incrUsageCount("WB.SpreadsheetContextMenu");
    if (getSelectedColumnCount() == 1) {
        final int[] rows = getSelectedRowModelIndexes();
        if (rows.length > 1) {
            //if (row == rows[0])
            //{
            if (!isImage) {
                JMenuItem mi = pMenu.add(new JMenuItem(UIRegistry.getResourceString("SpreadSheet.FillDown")));
                mi.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent ae) {
                        int selectedUICol = getSelectedColumn();
                        int selectedModelCol = convertColumnIndexToModel(selectedUICol);
                        model.fill(selectedModelCol, rows[0], rows);
                        popupMenu.setVisible(false);
                    }
                });
            }
            //} else if (row == rows[rows.length-1])
            //{
            if (!isImage) {
                JMenuItem mi = pMenu.add(new JMenuItem(UIRegistry.getResourceString("Spreadsheet.FillUp")));
                mi.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent ae) {
                        int selectedUICol = getSelectedColumn();
                        int selectedModelCol = convertColumnIndexToModel(selectedUICol);
                        model.fill(selectedModelCol, rows[rows.length - 1], rows);
                        popupMenu.setVisible(false);
                    }
                });
            }
            //}
        }
    }

    if (!isImage) {
        JMenuItem mi = pMenu.add(new JMenuItem(UIRegistry.getResourceString("SpreadSheet.ClearCells")));
        mi.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                int[] rows = getSelectedRowModelIndexes();
                int[] cols = getSelectedColumnModelIndexes();

                model.clearCells(rows, cols);
                popupMenu.setVisible(false);
            }
        });
    }

    if (deleteAction != null) {
        JMenuItem mi = pMenu.add(new JMenuItem(UIRegistry.getResourceString("SpreadSheet.DeleteRows")));
        mi.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                deleteAction.actionPerformed(ae);
                popupMenu.setVisible(false);
            }
        });
    }

    //add copy, paste, cut
    if (!isImage) //copy, paste currently only implemented for string data
    {
        boolean isSelection = getSelectedColumnCount() > 0 && getSelectedRowCount() > 0;
        if (pMenu.getComponentCount() > 0) {
            pMenu.add(new JPopupMenu.Separator());
        }
        JMenuItem mi = pMenu.add(new JMenuItem(UIRegistry.getResourceString("CutMenu")));
        mi.setEnabled(isSelection && !isImage); // copy, paste currently
        // only implemented for
        // string data
        mi.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                SwingUtilities.invokeLater(new Runnable() {

                    /*
                     * (non-Javadoc)
                     * 
                     * @see java.lang.Runnable#run()
                     */
                    @Override
                    public void run() {
                        cutOrCopy(true);

                    }

                });
            }
        });
        mi = pMenu.add(new JMenuItem(UIRegistry.getResourceString("CopyMenu")));
        mi.setEnabled(isSelection && !isImage);
        mi.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                SwingUtilities.invokeLater(new Runnable() {

                    /*
                     * (non-Javadoc)
                     * 
                     * @see java.lang.Runnable#run()
                     */
                    @Override
                    public void run() {
                        cutOrCopy(false);

                    }

                });
            }
        });
        mi = pMenu.add(new JMenuItem(UIRegistry.getResourceString("PasteMenu")));
        mi.setEnabled(isSelection && !isImage && canPasteFromClipboard());
        mi.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                SwingUtilities.invokeLater(new Runnable() {

                    /*
                     * (non-Javadoc)
                     * 
                     * @see java.lang.Runnable#run()
                     */
                    @Override
                    public void run() {
                        paste();

                    }

                });
            }
        });

    }

    pMenu.setInvoker(this);
    return pMenu;
}