Cyril Horad
Cyril Horad

Reputation: 1565

Why does JTable always trigger ListSelectionListener twice?

Is it normal that any changes to the selected row of the JTable will trigger the added ListSelectionListener twice?

Is it possible that the ListSelectionListener can be triggered only once?

Upvotes: 16

Views: 7714

Answers (2)

joshdlv
joshdlv

Reputation: 96

ListSelectionEvent.getValueIsAdjusting() will work as commented by @MeBigFatGuy.

Similar example:

if(!e.getValueIsAdjusting()){
    String selectedData = null;

    int selectedRow = table.getSelectedRow();
    int selectedColumns = table.getSelectedColumn();

    for (int i = 0; i <= selectedRow; i++) {
        for (int j = 0; j <= selectedColumns; j++) {
            selectedData = (String) table.getValueAt(selectedRow, selectedColumns);
        }
    }
    System.out.println("Selected: " + selectedData);
}

it'll be triggered just once.

Upvotes: 6

MeBigFatGuy
MeBigFatGuy

Reputation: 28578

Look at the event that is passed to your listener, specifically

ListSelectionEvent.getValueIsAdjusting() 

perform whatever ops you want to do when this returns false.

Upvotes: 28

Related Questions