KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > cbutil > CBSingleSelectionModel


1 package com.ca.commons.cbutil;
2
3 import javax.swing.*;
4
5
6 /**
7  * Create a SINGLE_SELECTION ListSelectionModel that calls a new
8  * method, updateSingleSelection(), each time the selection
9  * changes. This can be a little bit more convienent than using the
10  * ListModels ListSelectionListener, since ListSelectionListeners
11  * are only given the range of indices that the change spans.
12  *
13  * @see http://java.sun.com/products/jfc/tsc/tech_topics/jlist_1/jlist.html
14  */

15
16 public class CBSingleSelectionModel extends DefaultListSelectionModel
17 {
18     JList list = null;
19
20     public CBSingleSelectionModel(JList list)
21     {
22         this.list = list;
23         setSelectionMode(SINGLE_SELECTION);
24     }
25
26     public void setSelectionInterval(int index0, int index1)
27     {
28         int oldIndex = getMinSelectionIndex();
29         super.setSelectionInterval(index0, index1);
30         int newIndex = getMinSelectionIndex();
31
32         if (oldIndex != newIndex)
33         {
34             updateSingleSelection(oldIndex, newIndex);
35         }
36     }
37
38     public void updateSingleSelection(int oldIndex, int newIndex)
39     {
40         list.ensureIndexIsVisible(newIndex);
41     }
42 }
43
Popular Tags