3

I have a JList and register a selection handler (ListSelectionListener). Now I need to now the previous selected item/index.

Up to now, I save the last selected item on my own. Is there a better way to do so? In other words: Is there are methode/best practice which I miss all the years?!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Marcel Jaeschke
  • 707
  • 7
  • 24

2 Answers2

10

One of my list is single-selection-only. like kleopatra says. The event data does not help here.

That is not what Kleopatra said. The event data does help. You just can't assume that the first index represents the selected row and the last index represents the previous row.

As Kleopatra suggested you need to do further checking. Something like:

public void valueChanged(ListSelectionEvent e)
{
    JList list = (JList)e.getSource();
    int selected = list.getSelectedIndex();
    int previous = selected == e.getFirstIndex() ? e.getLastIndex() : e.getFirstIndex();

    System.out.println();
    System.out.println("Selected:" + selected);
    System.out.println("Previous:" + previous);
}
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Hmm looks a little bit awkward to me, but seems to be a suitable solution.(For Single-Selection-Mode) Nevertheless I think the Java Guys at Oracle should work on this. – Marcel Jaeschke Feb 08 '12 at 22:08
  • Ah! Thank you. I thought e.getFirstIndex() == list.getSelectedIndex(), but that is not the case! The API is not clear at all what First/Last should be. – mikesjawnbit Jan 24 '13 at 06:36
-1

You don't need to write your custom code to store the previous selected item in list. JList provide a ListSelectionListener which will do the work for you. Here is the way to get last selected item.

 customList.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent arg0) {
             // TODO Auto-generated method stub

                         //Previous Selected Item index will be obtained by arg0.getFirstIndex()
                       // Similarly Currently Selected Item index will be obtained by this method arg0.getLastIndex()



        }
    });
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Prateek Sharma
  • 346
  • 3
  • 12
  • 4
    that's not true - the first/last are the _boundaries_ of the range where a change has happened. Only for single selection mode _and_ a nice implementation of the model (aka: it keeps the notification range a small as possible) one of those is the old and the other the new selection - but you cant know which is which (not without further checking their selection status :) – kleopatra Feb 08 '12 at 12:12
  • i think it will work fine in single selection model..because it will give the last element and current selected element – Prateek Sharma Feb 08 '12 at 12:52
  • 1
    no, that's not the case (read the api doc and/or write and run a small example) – kleopatra Feb 08 '12 at 13:44
  • i have already seen api docs and implemented a small program and there it's working fine... that's why i posted this code here – Prateek Sharma Feb 08 '12 at 14:49
  • 1
    which part of "_Returns the index of the first row whose selection may have changed. getFirstIndex()<= getLastIndex()_" makes you think that firstIndex is not selected? – kleopatra Feb 08 '12 at 15:06
  • One of my list is single-selection-only. like kleopatra says. The event data does not help here. – Marcel Jaeschke Feb 08 '12 at 15:16
  • @kleopatra is right, the jdk doc has made it clear [here](https://docs.oracle.com/javase/7/docs/api/javax/swing/event/ListSelectionEvent.html) – kenshinji Nov 13 '15 at 03:02
  • I thinks it works for @Prateek Sharma, maybe because of the selection change is from a small index to a big one. – kenshinji Nov 13 '15 at 03:05