我创建了一个包含三个 JList 列表的 JDialog。选择第一个列表(名为 FirstList)中的一行会更新第二个列表(SecondList)的内容,选择第二个列表中的一行会更新第三个列表(ThirdList)的内容。在 ThirdList 类中,我包含了以下方法:
public void addRowsSelected(int format_row, int pathway_row){
first_list_selected_row = fl_row;
second_list_selected_row = sl_row;
ListSelectionModel listSelectionModel = this.getSelectionModel();
listSelectionModel.addListSelectionListener(new ThirdListSelectionListener(dialog, first_list_selected_row, second_list_selected_row));
}
然后我创建了 ThirdListSelectionListener 类,如下所示:
package eu.keep.gui.mainwindow.menubar.renderfile;
import java.io.IOException;
import java.util.List;
import java.util.ListIterator;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import eu.keep.characteriser.registry.Pathway;
public class ThirdListSelectionListener implements ListSelectionListener{
private ContentGenerated content;
public EmulatorList emulator_list;
private int first_list_selected_row;
private int second_list_selected_row;
private FirstList first_list;
private SecondList second_list;
private ThirdList third_list;
public ThirdListSelectionListener(MyDialog dialog, int first_list_selected_row, int second_list_selected_row){
this.content = dialog.content;
this.first_list_selected_row = first_list_selected_row;
this.second_list_selected_row = second_list_selected_row;
this.first_list = dialog.firstList;
this.second_list = dialog.secondList;
this.third_list = dialog.thirdList;
System.out.println("1. The first list row selected is "+this.first_list_selected_row);
System.out.println("2. The second list row selected is "+this.second_list_selected_row);
}
public void valueChanged(ListSelectionEvent e){
if (e.getValueIsAdjusting())
return;
Object source = e.getSource();
ListSelectionModel list = (ListSelectionModel)e.getSource();
if (list.isSelectionEmpty()) {
}else{
//int selected_row = list.getMinSelectionIndex();
try {
System.out.println("first: "+first_list_selected_row);
System.out.println("second: "+second_list_selected_row);
//System.out.println("third: "+selected_row);
// DO SOMETHING HERE
} catch (IOException e1) {
}
}
}
}
现在的问题:如果我首先选择,例如从第一个列表中选择第二行,从第二个列表中选择第二行,从第三个列表中选择第二行,我会按预期得到以下消息:
1. The first list row selected is 2
2. The second list row selected is 2
first: 2
second: 2
third: 2
但是,如果在我从第二个列表中选择第一行然后我再次从第三个列表中选择第二行后不久,我会得到以下输出:
1. The first list row selected is 2
2. The second list row selected is 1
first: 2
second: 2
third: 2
我没有使用 "second: 1" ,而是一直使用 "second: 2" 。我相信 second_list_selected_row 在 ThirdListSelectionListener 构造函数中更新,但在 valueChanged 方法中没有改变。有人可以告诉我这个问题的原因以及如何解决它吗?提前致谢!!