我已经创建了一个包含三个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方法中不会更改。有人能告诉我这个问题的原因和解决方法吗?提前感谢!!
发布于 2010-10-04 02:07:32
您在其ctor中分配了ThirdListSelectionListener
的first_list_selected_row
和second_list_selected_row
,并且从未更改过它们(至少在您发布的代码中没有更改)。您可以使用first_list.getSelectedIndex()
和second_list.getSelectedIndex()
获取当前选定的行。
https://stackoverflow.com/questions/3851694
复制