JTable - Selected Row click event

Brian picture Brian · Apr 12, 2012 · Viewed 149.3k times · Source

I have a Jtable that is populated with a linkedlist through an AbstractTableModel.

What I want to do is when I click (left-mouse click) on a row in the JTable, the linkedlist is search (in this case it contains movie titles) and displays the values in the linked list in Jtextboxes

How do I do this?

Here is the code

My guess it retrieve the data from the selected row into an array, split it, and put it into the jtextareas. How can I do this ?

Answer

Michael Kazarian picture Michael Kazarian · Oct 5, 2015

Here's how I did it:

table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
        public void valueChanged(ListSelectionEvent event) {
            // do some actions here, for example
            // print first column value from selected row
            System.out.println(table.getValueAt(table.getSelectedRow(), 0).toString());
        }
    });

This code reacts on mouse click and item selection from keyboard.