在按下ALT时阻止JPopupMenu关闭

时间:2018-05-15 09:43:30

标签: java swing jpopupmenu

希望有人可以提供一些帮助。我想将filter用于JTables swingbits。 我想使用buildin机会通过正则表达式过滤不同的项目。最大的问题是,似乎不可能进入| (管道)在搜索字段中。弹出窗口(基于JPopupmenu)总是在按下Alt-Gr时关闭。我试图删除Alt-Gr的键绑定,没有任何效果。它似乎在内心深处。任何想法如何解决这个问题? 提前致谢。 朱莉安

1 个答案:

答案 0 :(得分:1)

看起来像Windows L& F的一个功能,它位于Java源代码的深处。但我找到了另一种解决方案。

import java.awt.Color;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JWindow;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;

public class PopupTest implements Runnable {

    private Popup popup;

    @Override
    public void run() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        JFrame frm = new JFrame("Popup test");
        JLabel label = new JLabel("Click somewhere right mouse button to get a popup");
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (SwingUtilities.isRightMouseButton(e)) {
                    if (popup != null) {
                        popup.hide();
                    }
                    JTextField field = new JTextField(10);
                    JLabel lbl = new JLabel("Enter text here: ");
                    JPanel p = new JPanel();
                    p.add(lbl);
                    p.add(field);
                    p.setBorder(new LineBorder(Color.BLACK));
                    popup = PopupFactory.getSharedInstance().getPopup(label, p, e.getXOnScreen(), e.getYOnScreen());
                    // we need some logic for a heavy-weight popup
                    Window win = SwingUtilities.windowForComponent(p);
                    if (win instanceof JWindow) {
                        // if heavy weight make the window focusable
                        win.setFocusableWindowState(true);
                        // and install focus listener to hide popup when it lost focus
                        win.addWindowFocusListener(new WindowFocusListener() {

                            @Override
                            public void windowLostFocus(WindowEvent e) {
                                if (popup != null) {
                                    popup.hide();
                                }
                            }

                            @Override
                            public void windowGainedFocus(WindowEvent e) {
                                // TODO Auto-generated method stub

                            }
                        });
                    }
                    popup.show();
                    field.requestFocus();
                }
            }
        });
        frm.addWindowListener(new WindowAdapter() {
            @Override
            public void windowDeactivated(WindowEvent e) {
                if (popup != null) {
                    popup.hide();
                }
            }
        });
        frm.add(label);
        frm.setSize(500, 200);
        frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new PopupTest());
    }
}

在文本字段中,我可以使用“Alt Gr”键而不关闭popop。