JTable - ActionListener for select a row

user3057184 picture user3057184 · Dec 2, 2013 · Viewed 31.3k times · Source

I need the right AcionListener for my JTable.

At program start there is no row selected by default. If I now select any row in this JTable then the ActionListener shall start.

Answer

Paul Samsotha picture Paul Samsotha · Dec 2, 2013

Try this. I use a ListSelectionListener and it works for me. I added a listener to the table Model

jTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
    @Override
    public void valueChanged(ListSelectionEvent event) {
        if (jTable.getSelectedRow() > -1) {
            // print first column value from selected row
            System.out.println(jTable.getValueAt(jTable.getSelectedRow(), 0).toString());
        }
    }
});