KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > form > RowSelector


1 /*
2  * $Id: RowSelector.java,v 1.3 2005/02/24 20:35:29 rbair Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.form;
9
10 import org.jdesktop.swing.data.DataModel;
11 import org.jdesktop.swing.data.DefaultTableModelExt;
12
13 import org.jdesktop.swing.JXTable;
14
15 import javax.swing.ListSelectionModel JavaDoc;
16 import javax.swing.JComponent JavaDoc;
17 import javax.swing.JList JavaDoc;
18 import javax.swing.JTable JavaDoc;
19 import javax.swing.event.ListSelectionEvent JavaDoc;
20 import javax.swing.event.ListSelectionListener JavaDoc;
21
22 /**
23  * @author Amy Fowler
24  */

25
26 public class RowSelector {
27     private DataModel dataModel;
28     private JComponent JavaDoc component;
29
30     public RowSelector(JTable JavaDoc table, DataModel dataModel) {
31         this(table.getSelectionModel(), dataModel);
32         component = table;
33     }
34
35     public RowSelector(JList JavaDoc list, DataModel dataModel) {
36         this(list.getSelectionModel(), dataModel);
37         component = list;
38     }
39
40     public RowSelector(ListSelectionModel JavaDoc selectModel, DataModel dataModel) {
41         this.dataModel = dataModel;
42 // if (dataModel.getRecordCount() > 0) {
43
// selectModel.setLeadSelectionIndex(0);
44
// selectModel.setAnchorSelectionIndex(0);
45
// dataModel.setRecordIndex(0);
46
// }
47
selectModel.addListSelectionListener(new ListSelectionListener JavaDoc() {
48             public void valueChanged(ListSelectionEvent JavaDoc e) {
49                 ListSelectionModel JavaDoc selectionModel = (ListSelectionModel JavaDoc)e.getSource();
50                 int selectionIndex = selectionModel.getLeadSelectionIndex();
51                 if (component != null && selectionModel.isSelectedIndex(selectionIndex)) {
52                     if (component instanceof JXTable) {
53                         selectionIndex = ((JXTable)component).convertRowIndexToModel(
54                             selectionIndex);
55                     }
56 // RowSelector.this.dataModel.setRecordIndex(selectionIndex);
57
}
58             }
59         });
60     }
61 }
Popular Tags