1 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 ; 16 import javax.swing.JComponent ; 17 import javax.swing.JList ; 18 import javax.swing.JTable ; 19 import javax.swing.event.ListSelectionEvent ; 20 import javax.swing.event.ListSelectionListener ; 21 22 25 26 public class RowSelector { 27 private DataModel dataModel; 28 private JComponent component; 29 30 public RowSelector(JTable table, DataModel dataModel) { 31 this(table.getSelectionModel(), dataModel); 32 component = table; 33 } 34 35 public RowSelector(JList list, DataModel dataModel) { 36 this(list.getSelectionModel(), dataModel); 37 component = list; 38 } 39 40 public RowSelector(ListSelectionModel selectModel, DataModel dataModel) { 41 this.dataModel = dataModel; 42 selectModel.addListSelectionListener(new ListSelectionListener () { 48 public void valueChanged(ListSelectionEvent e) { 49 ListSelectionModel selectionModel = (ListSelectionModel )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 } 58 } 59 }); 60 } 61 } | Popular Tags |