Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
648 views
in Technique[技术] by (71.8m points)

swing - 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. 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. 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. 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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...