简体   繁体   English

在Java中双击JTable上的监听器

[英]Double click listener on JTable in Java

I am curious as to how to call valueChanged overridden method only if a row in JTable has been double clicked. 我很好奇如何只在双击JTable的一行时才调用valueChanged重写方法。 For now the below code snippet achieves one click action or event arrow key to navigate through a list of people and would adjust JLabel accordingly. 现在,下面的代码片段实现了一个单击操作或事件箭头键,以便在人员列表中导航,并相应地调整JLabel What I'm trying to do is something similar just like I did for one click, but this time IF and ONLY IF a row has been double clicked dto would change else nothing happens. 我正在尝试做的事情就像我点击一样,但这一次,如果只有一行被双击, dto会改变,否则没有任何反应。 How do I do this :( 我该怎么做呢 :(

   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 {
            }

        }
    });
}

not sure of the rest above... 不确定上面的其余部分......

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. 将事件处理程序的代码重新定位到宿主类中的私有方法中,然后实现MouseListener或扩展MouseAdapter然后在那里调用私有方法。 The first step (ie 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() 通过调用MouseEvent.getClickCount()轻松检测MouseHandler的双击

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

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

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