Example usage for javax.swing JPopupMenu setLocation

List of usage examples for javax.swing JPopupMenu setLocation

Introduction

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

Prototype

@BeanProperty(description = "The location of the popup menu.")
public void setLocation(int x, int y) 

Source Link

Document

Sets the location of the upper left corner of the popup menu using x, y coordinates.

Usage

From source file:Main.java

/**
 * Gives a tray icon a popup menu.//from   w  w  w  . j  ava 2 s .  c om
 *
 * @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: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  w  w .j  av a 2s  .  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:org.squidy.designer.zoom.impl.SourceCodeShape.java

private JEditorPane createCodePane(URL sourceCodeURL) {

    codePane = new JEditorPane() {
        @Override/*w  w  w  .jav a2s .c  om*/
        protected void processComponentKeyEvent(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_SPACE && e.isControlDown()) {
                System.out.println("Code completion");

                int caretPosition = codePane.getCaretPosition();

                String code = codePane.getText();
                switch (code.charAt(caretPosition - 1)) {
                case '.':
                    int pseudoCaret = caretPosition - 1;
                    StringBuilder word = new StringBuilder();
                    for (char c = code.charAt(--pseudoCaret); !isEndOfWord(c); c = code.charAt(--pseudoCaret)) {
                        word.append(c);
                    }

                    word = word.reverse();

                    System.out.println("WORD: " + word);

                    // Class<?> type =
                    // ReflectionUtil.loadClass(word.toString());
                    //                  
                    // System.out.println("TYPE: " + type);

                    JPopupMenu menu = new JPopupMenu("sdaf");
                    Point p = codePane.getCaret().getMagicCaretPosition();
                    System.out.println("CARET POS: " + p);
                    // Point p = codePane.get

                    // menu.setPreferredSize(new Dimension(200, 200));
                    menu.setLocation(30, 30);
                    menu.add("test");

                    codePane.add(menu);

                    // System.out.println(p);

                    // codePane.get

                    menu.show(codePane, p.x, p.y);

                    break;
                }
            }

            super.processComponentKeyEvent(e);
        }

        /**
         * @param c
         * @return
         */
        private boolean isEndOfWord(char c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t';
        }
    };

    EditorKit editorKit = new StyledEditorKit() {

        /**
         * 
         */
        private static final long serialVersionUID = 7024886168909204806L;

        public Document createDefaultDocument() {
            return new SyntaxDocument();
        }
    };

    codePane.setEditorKitForContentType("text/java", editorKit);
    codePane.setContentType("text/java");

    try {
        FileInputStream fis = new FileInputStream(sourceCodeURL.getPath());
        codePane.read(fis, null);
        originSourceCode = codePane.getText();

        computeHeightOfCodePane();
        codePane.setAutoscrolls(true);
    } catch (Exception e) {
        codePane.setText("File not found!");
    }

    codePane.requestFocus();
    codePane.setBorder(BorderFactory.createLineBorder(Color.BLACK));

    codePane.addKeyListener(new KeyAdapter() {

        /*
         * (non-Javadoc)
         * 
         * @see
         * java.awt.event.KeyAdapter#keyPressed(java.awt.event.KeyEvent)
         */
        @Override
        public void keyPressed(KeyEvent e) {
            super.keyPressed(e);

            switch (e.getKeyCode()) {
            case KeyEvent.VK_ENTER:
            case KeyEvent.VK_DELETE:
            case KeyEvent.VK_BACK_SPACE:
            case KeyEvent.VK_SPACE:
                computeHeightOfCodePane();
                break;
            }

            markDirty();
        }
    });

    // final JPopupMenu menu = new JPopupMenu();
    //
    // JMenuItem i = new JMenuItem("Option 1");
    // JMenuItem i2 = new JMenuItem("Option 2");
    // menu.add(i);
    // menu.add(i2);
    // edit.add(menu);
    // getComponent().addKeyListener(new KeyAdapter() {
    //      
    // public void keyTyped(KeyEvent e) {
    // if(e.getModifiers() == 2 && e.getKeyChar() == ' ') {
    // Point popupLoc = edit.getCaret().getMagicCaretPosition();
    // System.out.println(popupLoc);
    // menu.setLocation(new
    // Point((int)popupLoc.getX(),(int)popupLoc.getY()));
    // menu.setVisible(true);
    // }
    //      
    // }
    // });

    return codePane;
}