在Java中对JTable中双击监听器(Double click listener on JTabl

2019-07-19 19:33发布

我很好奇,如何调用valueChanged只有当连续重写的方法JTable已经双点击。 现在下面的代码片段实现了点击动作或事件箭头键通过的人员名单导航和将调整JLabel相应。 我正在试图做的是,就像我做了一个点击类似的东西,但这次当且仅当行已双击了dto会改变别的什么也没有发生。 我该怎么做呢 :(

   class ListDataUI {

    public void addListSelectionListener(ListSelectionListener listSelectionListener) {
            summaryTable.getSelectionModel().addListSelectionListener(listSelectionListener);

 public T getSelectedDTO() {
        final int selectedRowIndex = summaryTable.getSelectedRow();
        if (selectedRowIndex != -1) {
            return data.get(summaryTable.convertRowIndexToModel(selectedRowIndex));
        } else {
            return null;
        }
    }
        }
    }




    class MainMenu extends javax.swing.JFrame {
    private void initListeners() {
    searchTable.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                AcademicDTO dto = (AcademicDTO) searchTable.getSelectedDTO();
                acImgLabel.setIcon(new ImageIcon());
                label_name.setText(dto.getTitle() + " " + dto.getForename() + " " + dto.getSurname());
                label_role.setText("Role: " + dto.getRole());
                label_phone.setText("Phone: " + dto.getPhone());
                label_room.setText("Room: " + dto.getRoom());
                label_hours.setText("Hours: " + dto.getHours());
                label_mobile.setText("Mobile: " + dto.getMobile());
                if (dto.getImage() != null) {
                    acImgLabel.setIcon(new ImageIcon(dto.getImage()));
                }
            }
        }
    });
}

}


 private void initListeners() {
    contactTable.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            ContactDTO dto = (ContactDTO) contactTable.getSelectedDTO();
            if (e.getClickCount() == 2) {
                System.out.println(dto.getForename());
            } else {
            }

        }
    });
}

不知道上面休息的......

Answer 1:

试试这个:

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 
        }
    }
});


Answer 2:

重新定位的事件处理程序的代码到你的主机类的私有方法,然后实现MouseListener或延长MouseAdapter然后调用私有方法那里。 第一步(即创建私有方法可以帮助你从调用多个事件处理程序相同的逻辑)。

检测到的双击MouseHandler由呼叫使得容易MouseEvent.getClickCount()



Answer 3:

@MooHa您的类ListDataUI应该实现的MouseListener。



文章来源: Double click listener on JTable in Java