有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

ListSelectionListener接口的valueChanged方法中存在java问题

我创建了一个包含三个JList列表的JDialog。在第一个列表(名为FirstList)中选择一行将更新第二个列表(SecondList)的内容,在第二个列表中选择一行将更新第三个列表(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)); 
} 

然后,我创建了第三个ListSelectionListener类,如下所示:

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

我没有用“秒:1”,而是一直用“秒:2”。我相信第二行列表已在ThirdListSelectionListener构造函数中更新,但在valueChanged方法中没有更改。有人能告诉我这个问题的原因和解决方法吗?提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    您在其ctor中分配了ThirdListSelectionListenerfirst_list_selected_rowsecond_list_selected_row,并且从未更改过它们(至少在您发布的代码中没有)。您可以使用first_list.getSelectedIndex()second_list.getSelectedIndex()获取当前选定的行