我创建了一个包含三个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) {
}
}
}
}
现在问题:如果我先选择,例如第一个列表中的第二行,第二个列表中的第二行和第三个列表中的第二行,我会按预期获得以下消息:< / p>
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
我没有“秒:1”,而是持续“秒:2”。我相信second_list_selected_row在ThirdListSelectionListener构造函数中更新,但它在valueChanged方法中没有更改。有人可以告诉我这个问题的原因以及如何解决它?在此先感谢!!
答案 0 :(得分:1)
您在其ctor中分配了ThirdListSelectionListener
的{{1}}和first_list_selected_row
,但从未更改过它们(至少在您发布的代码中没有更改过)。您可以使用second_list_selected_row
和first_list.getSelectedIndex()
获取当前选定的行。