Double click listener on JTable in Java

旧时模样 提交于 2019-11-28 19:12:48
Anupam Maiti

Try this:

mytable.addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent mouseEvent) {
        JTable table =(JTable) mouseEvent.getSource();
        Point point = mouseEvent.getPoint();
        int row = table.rowAtPoint(point);
        if (mouseEvent.getClickCount() == 2 && table.getSelectedRow() != -1) {
            // your valueChanged overridden method 
        }
    }
});

Relocate the code of the event handler into a private method in your host class, then implement the MouseListener or extend the MouseAdapter then invoke the private method there. The first step (i.e. creating the private method helps you invoke the same logic from multiple event handlers).

Detecting the double click in the MouseHandler is made easy by the call to MouseEvent.getClickCount()

@MooHa Your class ListDataUI should implements MouseListener.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!