开发者

Why does JTable always trigger ListSelectionListener twice?

开发者 https://www.devze.com 2023-03-02 15:59 出处:网络
Is it normal that any changes to the selected row of the JTable will trigger the added 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

精彩评论

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

关注公众号