2

I have got a JRadioButton and set a ComponentPopupMenu to it.

JRadioButton rdbtnTest = new JRadioButton();
rdbtnTest.setComponentPopupMenu(popupMenu);

The popupMenu is a JPopupMenu with one JPopupMenuItem. The JPopupMenuItem has a MouseListener, so that on a mouse click, a webpage is opened.

JPopupMenu popupMenu = new JPopupMenu("GO");
PopupMenuListener popupMenuListener = new MyPopupMenuListener();
popupMenu.addPopupMenuListener(popupMenuListener);
MenuItem openMenuItem = new JMenuItem("Open Webpage");

openMenuItem.addMouseListener(new MouseListener() {
        @Override
        public void mousePressed(MouseEvent arg0) {
            Component comp = arg0.getComponent(); // will only return the JPopupMenuItem
            // Determine the right-clicked radio button and open webpage
        }
popupMenu.add(openMenuItem);

My question is: if I have another JRadioButton rdbtnTest2 with the same popupMenu, can I determine which of the two radio buttons triggered the JPopupMenu? When opening the webpage, I need to pass a specific parameter according to which radio button was right-clicked.

4

1 に答える 1

3

MouseListener でポップアップ メニューを表示するときは、イベント ソースを取得してどこかに保存します (メニューを呼び出すラジオボタン)。コードでは、ポップアップ呼び出し元を確認してください。

于 2012-04-30T09:12:28.390 に答える