2010-10-03 15 views
1

我已經創建了一個帶有三個JList列表的JDialog。第一列表(名爲FirstList)中的行的選擇更新第二列表(第二列表)的內容,並且第二列表中的行的選擇更新第三列表(第三列表)的內容。在ThirdList I類已包括下面的方法:來自ListSelectionListener接口的valueChanged方法中的問題

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 

而不必「第二:1」的,我一直有「第二:2」。我相信在ThirdListSelectionListener構造函數中更新second_list_selected_row,但它在valueChanged方法中不會更改。有人可以告訴我這個問題的原因,以及如何解決它?提前致謝!!

回答

1

您在其ctor中分配了ThirdListSelectionListenerfirst_list_selected_rowsecond_list_selected_row,並且從未更改過它們(至少在您的發佈代碼中沒有更改過)。您可以使用first_list.getSelectedIndex()second_list.getSelectedIndex()獲取當前選定的行。