JList highlighed stay on previously selected item

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dvjava
    New Member
    • May 2009
    • 3

    #1

    JList highlighed stay on previously selected item

    Hi,
    I am wondering if there is way to twist JList such that the highlight still remains on previously selected item after other item is selected.
    For example, this list has 4 items: A, B, C, D
    If I select A, A will be highlighted.
    I then select C, C will be highlighted and A still remains highlighted as well.
    I then re-select A again, A now becomes "de-highlighted".

    Thanks in advance,

    dvnjava
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Doesn't one of the selection modes, using the setSelectionMod e( ... ) method help you?

    kind regards,

    Jos

    Comment

    • dvjava
      New Member
      • May 2009
      • 3

      #3
      Can you be more specific, JosAH?
      I tried the below code borrowed from Hans Muller, Sun java website, and it makes item being toggle only but does not keep 2 items highlighted.


      JList list = new JList(MyArray);
      list.setSelecti onModel(new ToggleSelection Model());

      class ToggleSelection Model extends DefaultListSele ctionModel
      {
      public void setSelectionInt erval(int index0, int index1) {
      if (isSelectedInde x(index0)) {
      super.removeSel ectionInterval( index0, index1);
      }
      else {
      super.setSelect ionInterval(ind ex0, index1);
      }
      }
      }

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Try to set the selection mode:

        Code:
        JList yourList ...;
        
        yourList.setSelectionMode(
        ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        kind regards,

        Jos

        Comment

        Working...