Java 为什么JTable总是触发ListSelectionListener两次?

Java 为什么JTable总是触发ListSelectionListener两次?,java,swing,event-handling,jtable,Java,Swing,Event Handling,Jtable,对JTable所选行的任何更改都会触发添加的ListSelectionListener两次,这是否正常 ListSelectionListener是否可能只触发一次?查看传递给您的侦听器的事件,特别是 ListSelectionEvent.getValueIsAdjusting() 当返回false时,执行您想要执行的任何操作。ListSelectionEvent.getValueIsAdjusting()将按照@MeBigFatGuy的注释工作 类似的例子: if(!e.getValueI

JTable
所选行的任何更改都会触发添加的
ListSelectionListener
两次,这是否正常


ListSelectionListener
是否可能只触发一次?

查看传递给您的侦听器的事件,特别是

ListSelectionEvent.getValueIsAdjusting() 

当返回false时,执行您想要执行的任何操作。

ListSelectionEvent.getValueIsAdjusting()
将按照@MeBigFatGuy的注释工作

类似的例子:

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);
}
if(!e.getValueIsAdjusting()){
字符串selectedData=null;
int selectedRow=table.getSelectedRow();
int selectedColumns=table.getSelectedColumn();

对于(int i=0;我可以给出一些更详细的ans@mebigfatguy事实上,您通过ListSelectionEvent传递了两次,第一次getValueIsAdjusting()为true,第二次getValueIsAdjusting()使用getSelectedIndex()获取选择的索引如果为false,则您可以使用此选择执行程序所需的操作,如果在第二遍中再次调用getSelectedIndex(),则会得到getSelectedIndex()=-1。