简体   繁体   English

Java JTable忽略鼠标事件

[英]java JTable ignores mouse event

My problem is when I add a MouseListener to a JTable, there is no result. 我的问题是,当我将MouseListener添加到JTable时,没有结果。

Example: The class OnlineList extends JTable. 示例:类OnlineList扩展了JTable。

// In constructor:
public OnlineList() {

    this.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {

            System.out.println("Check mouse click");
        }
    });
}

There is no output like "Check mouse click" I think the event listener is ignored. 没有像“检查鼠标单击”这样的输出,我认为事件监听器被忽略了。

I have tried to do what you want and all works properly. 我已尝试做您想要的事情,并且一切正常。 Here is my code: 这是我的代码:

    public static void main(String... s) {
     JFrame test = new JFrame("Test");
     JTable t = new JTable(new Object[][]{{1,2},{2,3}},new Object[]{"a","b"});
     t.addMouseListener(getMouseListener());
     test.add(new JScrollPane(t));
     test.setVisible(true);
     test.pack();
}

private static MouseListener getMouseListener() {
    return new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            System.out.println("test");
        }
    };
}

I recommend you to validate this object in your code, is it really your table? 我建议您在代码中验证对象,这真的是您的表吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM