单击JPopupMenu时,JPopupPanel关闭

时间:2011-04-27 09:45:38

标签: java swing user-interface jpopupmenu

单击按钮时,我显示JPopupPanel

JPopupPanel的{​​{1}}显示鼠标右键,显示不同的选项。 当按下左键以在PopupMenu中选择其中一个选项时,PopupPanel会自动关闭PopupMenu片刻,当释放按钮时,PopupMenu也会消失(如预期的那样),但是在该按钮中无法看到该动作。 PopupPanel,因为它已经关闭。

如何在选择JPopupMenu的其中一个选项时避免JPopupPanel关闭?

由于

2 个答案:

答案 0 :(得分:1)

抱歉,我没有使用JPopupPanel的经验。因此,我只能提供一个简单的 hack

我怀疑你可以做一些类似我的例子(下面)的事情,我忽略'隐藏选项选择上的弹出菜单。

我的方法是在选项选项上重新显示弹出菜单。因此,您可以尝试在隐藏时重新显示您的面板。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;

public class JPopupMenuIgnoreOptionCloseOnClick extends JPanel
{
    private static final long serialVersionUID = 1L;
    private JPopupMenu popup = new JPopupMenu("Oi I am popup");
    private MouseListener mL = new MouseAdapter()   
    {
        @Override
        public void mousePressed(MouseEvent e)
        {
            System.out.println("mL mousePressed e.isP="+e.isPopupTrigger());
            super.mousePressed(e);
            showPopup(e);
        }

        @Override
        public void mouseReleased(MouseEvent e)
        {           
            System.out.println("mL mouseReleased e.isP="+e.isPopupTrigger());
            super.mouseReleased(e);
            showPopup(e);
        }

        private void showPopup(MouseEvent e)
        {
            if(e.isPopupTrigger())
            {
                prevLocation = e.getPoint();
                popup.show(e.getComponent(), e.getX(), e.getY());
            }
        }
    };
    private Point prevLocation = null;
    private MouseListener optionML = new MouseAdapter()
    {
        @Override
        public void mouseReleased(MouseEvent e)
        {
            System.out.println("optionML mouseReleased prevLocation="+prevLocation);
            e.consume();
            popup.show(JPopupMenuIgnoreOptionCloseOnClick.this, prevLocation.x,prevLocation.y);
        } 

    };

    public JPopupMenuIgnoreOptionCloseOnClick()
    {
        addMouseListener(mL);
        JMenuItem opt1 =new JMenuItem("Option 1");
        opt1.addMouseListener(optionML);
        popup.add(opt1);
        JMenuItem opt2 =new JMenuItem("Option 2");
        opt2.addMouseListener(optionML);
        popup.add(opt2);
        JMenuItem opt3 =new JMenuItem("Option 3");
        opt3.addMouseListener(optionML);
        popup.add(opt3);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() 
        {
            @Override
            public void run()
            {           
                JPopupMenuIgnoreOptionCloseOnClick p = new JPopupMenuIgnoreOptionCloseOnClick();
                p.setPreferredSize(new Dimension(400, 400));
                JPanel contentPane = new JPanel();
                contentPane.setBackground(Color.CYAN);
                contentPane.add(p);
                JFrame f = new JFrame();        
                f.setContentPane(contentPane);
                f.setSize(800, 600);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setVisible(true);
            }
        });
    }   
}

答案 1 :(得分:1)

我在JCommandButton上安装自定义JPopupMenu时遇到了这个问题。 对于你的JCommandButton,我发现这有助于防止过早丢弃父弹出窗口:

this.putClientProperty(BasicCommandButtonUI.DONT_DISPOSE_POPUPS, true);

如果您正在寻找的是在进行JPopupMenu JMenuItem选择时,父弹出式面板将保持打开状态,您有几个选项。问题源于JPopupMenu在UI所依赖的祖先容器链中断开的链接。而不是getParent(),您需要返回getInvoker()。

1:
修改BasicPopupPanelUI.WindowTracker.eventDispatched()中的库源。要么更改 SwingUtilities .getAncestorOfClass()调用,要使用 SwingXUtilities .getAncestorOfClass()来解释这种特殊情况。或者自己实现逻辑。

if(parent instanceof JPopupMenu)  parent = ((JPopupMenu)parent).getInvoker()

2:

将此代码添加到窗口小部件(CustomButton?)

 final JPopupMenu popper = new JPopupMenu(){  //hack
        @Override public Container getParent(){
            StackTraceElement ste = Thread.currentThread().getStackTrace()[2];
            if(ste.getClassName().equals(SwingUtilities.class.getName()))
                return CustomButton.this.getParent();
            return super.getParent();
        }
    };

我选择了#2,因为我在修改第三方库时遇到了问题。