tags:

views:

44

answers:

3

I have created the following class implementing a ListSelectionListener interface. This class should "listen" to the selection events of a JList I have created. Everytime the use clicks on a row of this list, the selected_row value should be updated and the string "The format row selected is ...." should therefore change. However after clicking the rows more than once, the select_row value doesn't change. Can anybody provide me a with an explanation for this and hopefully, a way to do what I want? Thanks in advance!!

import java.util.List;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import ee.dobax.portal.CommonPath;

public class FormatListSelectionListener implements ListSelectionListener{

public ContentGenerated content;
private CommonPathList path_list;
private ConfigRenderingDialog dialog;

public FormatListSelectionListener(ConfigRenderingDialog dialog){

    content = dialog.content;
    path_list = dialog.pathList;
}

public void valueChanged(ListSelectionEvent e) {
    int selected_row;

    if(e.getValueIsAdjusting() == false){
        selected_row = e.getLastIndex();


    System.out.println("The format row selected is "+selected_row);
        path_list.addFormatListRowSelected(selected_row);

        List<CommonPath> list_p = content.getPathList(selected_row);

        Object[] path_list_to_array = new Object[list_p.size()];

        path_list.getContents().removeAllElements();

        for(int x = 0; x < list_p.size(); x++){
            path_list_to_array[x] = list_p.get(x);
            path_list.getContents().addElement(path_list_to_array[x]);
            }

        }
  }


 }   
A: 

Wouldn't you want to check for e.getValueIsAdjusting() being true? Because that should mean that the event changed. This is probably why it works once (the first time there may be no change) and after that doesn't work.

Also I'd change it to say if(e.getValueIsAdjusting()) as it returns a boolean value.

Chris Aldrich
+1  A: 

I read the docs as indicating that the ListSelectionEvent only tells you that the selection between firstIndex and lastIndex was changed, but not in which direction. Once you know that a change has occurred (that a ListSelectionEvent has been fired) you could just read the current selected value from the JList:

selected_row = ((JList) e.getSource()).getSelectedIndex();

You'll want to check selected_row is non-negative, in case the user operation is simply unselecting the only selected option.

Mark E
((JList) e.getSource()).getSelectedIndex() doesn't retrieve anything....
Tony
@Tony: Are you sure something is in fact selected, then?
Mark E
yes, in the FormatList constructor class I have this.setSelectedIndex(0);
Tony
@Tony: what do you mean it doesn't retrieve anything? that method returns an `int`, what value do you get?
Mark E
+1  A: 

Can you please share the code that attaches this listener the the JList? It should be something like:

list = new JList(listData);
listSelectionModel = list.getSelectionModel();
listSelectionModel.addListSelectionListener(
         new FormatListSelectionListener());

See How to write ListSelection Listener

yshalbar
yes it's pretty much that:ListSelectionModel listSelectionModel = this.getSelectionModel();listSelectionModel.addListSelectionListener(new FormatListSelectionListener(dialog));
Tony