getting right click location from popmenu action event
Asked Answered
A

4

5

I have a java program that opens a popup menu when right clicked in a JPanel. When any of the popup menu items are clicked, I want to print the location of the right click that triggered the popupmenu in the terminal. How do I do this? How do I get the location of where the right click happened from within popup action events?

How does the code change if the popup menu is in a JComponent?

Here is the program.

    import java.awt.EventQueue;
    import java.awt.event.*;
    import javax.swing.*;

    public class MenuTest
    {
        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    MenuFrame frame = new MenuFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }
            });
        }
    }

    class MenuFrame extends JFrame
    {
        public MenuFrame()
        {
            setTitle("MenuTest");
            setSize(300, 200);

            Action cutAction = new TestAction("Cut");
            Action copyAction = new TestAction("Copy");
            Action pasteAction = new TestAction("Paste");

            JPopupMenu popup = new JPopupMenu();
            popup.add(cutAction);
            popup.add(copyAction);
            popup.add(pasteAction);

            JPanel panel = new JPanel();
            panel.setComponentPopupMenu(popup);
            add(panel);

            panel.addMouseListener(new MouseAdapter() {});
        }

        class TestAction extends AbstractAction
        {
            public TestAction(String name)
            {
                super(name);
            }

            public void actionPerformed(ActionEvent event)
            {
                System.out.println("Right click happened at ?"); // How do I get right click location?          
            }
        }
    }
Avigdor answered 1/12, 2012 at 15:22 Comment(0)
P
4

Add a mouse listener to pressed events, (clicked events get captured by popup):

    panel.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            clickLocation.setSize(e.getX(), e.getY());
        }
    });
    Action cutAction = new TestAction("Cut", clickLocation);
    Action copyAction = new TestAction("Copy", clickLocation);
    Action pasteAction = new TestAction("Paste", clickLocation);

Print out the dimension:

    private Dimension clickLocation;

    public TestAction(String name, Dimension clickLocation) {
        super(name);
        this.clickLocation = clickLocation;
    }

    public void actionPerformed(ActionEvent event) {
        System.out.println("Right click happened at " + clickLocation);
    }
Piazza answered 1/12, 2012 at 16:57 Comment(0)
H
3

you were on the right track. i personally prefer to show it manually in the MouseAdapter so i can add methods on other mouseevents. for this you probably need to remove the panel.setComponentPopupMenu(popup);


panel.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent arg0) {
            if (arg0.getButton() == MouseEvent.BUTTON3) { //Button3 is rightclick
                popup.show(panel, arg0.getX(), arg0.getY());
            }
        }
});
Haines answered 1/12, 2012 at 16:29 Comment(2)
Note: one should use isPopupTrigger rather than doing it blindly in the mouseClicked - see How do I create a right click context menu in Java Swing?Intuitional
You could replace arg0.getButton() == MouseEvent.BUTTON3 with SwingUtilities.isRightMouseButton(arg0). So you don't have to remember button numbers.Ungraceful
A
0

Here is the code that I was looking for. Thank you Schippi and Garret for your help.

    import java.awt.EventQueue;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.geom.*;

    public class MenuTest
    {
        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    MenuFrame frame = new MenuFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }
            });
        }
    }

    class MenuFrame extends JFrame
    {
        public MenuFrame()
        {
            setTitle("MenuTest");
            setSize(300, 200);

            Action cutAction = new TestAction("Cut");
            Action copyAction = new TestAction("Copy");
            Action pasteAction = new TestAction("Paste");

            JPopupMenu popup = new JPopupMenu();
            popup.add(cutAction);
            popup.add(copyAction);
            popup.add(pasteAction);

            JPanel panel = new JPanel();
            panel.setComponentPopupMenu(popup);
            add(panel);

            panel.addMouseListener(new MouseAdapter() { 
                @Override
                public void mousePressed(MouseEvent e) {
                    clickLocation= e.getPoint();
                }
            });
        }

        class TestAction extends AbstractAction
        {
            public TestAction(String name)
            {
                super(name);
            }

            public void actionPerformed(ActionEvent event)
            {
                System.out.println("Right click happened at (" + clickLocation.getX()+"," + clickLocation.getY()+ ")");         
            }
        }

        private Point2D clickLocation;
    }
Avigdor answered 1/12, 2012 at 23:52 Comment(0)
C
0

Or if you don't want to get it from the event.

Point mousepospoint=null;

if((mousepospoint=componentname.getMousePosition()) != null){
//mouseposArray[0]=mousepospoint.x;
//mouseposArray[1]=mousepospoint.y;
mousepoints(mousepospoint.x,mousepospoint.y);
}//enif


int[] mouseposArray={0,0};
// requires a function to return it if mouseposArray[] is global
protected int[] mousepoints(int xpo,int ypo){
mouseposArray=new int[2];
mouseposArray[0]=xpo;
mouseposArray[1]=ypo;
return mouseposArray;
}//enmeth 
Cerotype answered 2/12, 2012 at 0:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.