39

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

MooHa
  • 819
  • 3
  • 11
  • 23
  • 2
    A MouseListener seems more appropriate. – Guillaume Polet Feb 13 '13 at 11:59
  • yes, but unsure about the adjusting part to get the row – MooHa Feb 13 '13 at 12:01
  • 1
    What is the problem of getting the row? Either is is already selected and you can get it with `JTable.getSelectedRow()` or you use the mouse event point and the method: http://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html#rowAtPoint(java.awt.Point) – Guillaume Polet Feb 13 '13 at 12:13

3 Answers3

101

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 
        }
    }
});
Community
  • 1
  • 1
Anupam Maiti
  • 1,570
  • 1
  • 19
  • 35
  • 6
    should add && row!=-1 – Ofek Ron Sep 06 '15 at 17:00
  • 3
    @OfekRon: Explanation is, that the table might have a certain size, but the data inside does not completely and visually fill it out. So if the user clicks outside of the data area but still inside of the table, the return-value of the row is `-1`, which should be handled. – hamena314 Mar 02 '16 at 14:45
2

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()

Waleed Almadanat
  • 1,027
  • 10
  • 24
-3

@MooHa Your class ListDataUI should implements MouseListener.

Anonymous
  • 152
  • 1
  • 1
  • 11