tags:

views:

24

answers:

1

I have created a JDialog with three JList lists. The selection of a row in the first list (named FirstList) updates the content of the second list (SecondList) and the selection of a row in the second list updates the content of the third list (ThirdList). In the ThirdList class I have included the following method:

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)); 
} 

Then I have created the ThirdListSelectionListener class which is the following:

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) {

                }

            }

    }

}

Now the problem: if I first select, for example the second row from the first list, the second row from the second list and the second row from the third list, I would get, as expected, the following messages:

1. The first list row selected is 2
2. The second list row selected is 2
first: 2
second: 2
third: 2

However if soon after I select the first row from the second list and then I select again the second row from the third list I get the following output:

1. The first list row selected is 2
2. The second list row selected is 1
first: 2
second: 2
third: 2

Instead of having "second: 1" , I keep having "second: 2". I believe the second_list_selected_row is updated in the ThirdListSelectionListener constructor but it doesn't change in the valueChanged method. Can someone please tell me the cause of this problem and how to fix it? Thanks in advance!!

+1  A: 

You assigned ThirdListSelectionListener's first_list_selected_row and second_list_selected_row in its ctor and never changed them (at least not in your posted code). You can get the currently selected row with first_list.getSelectedIndex() and second_list.getSelectedIndex().

Geoffrey Zheng