开发者

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?


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

ListSelectionEvent.getValueIsAdjusting() 

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜