1 21 package net.mlw.vlh.swing.support; 22 23 import javax.swing.JTable ; 24 25 29 public class SelectionSupport 30 { 31 public static void maintainSelectedRows(KeyRetriever keyRetriever, JTable table, AbstractValueListTableModel model, TableSorter sorter, 32 Runnable runnable) 33 { 34 String key = null; 35 int row = table.getSelectedRow(); 36 37 if (table.getSelectedRow() != -1) 38 { 39 int selectedRow = table.getSelectedRow(); 40 int sortedRow = sorter.modelIndex(selectedRow); 41 key = keyRetriever.getKey(model.getBean(sortedRow)); 42 } 43 44 runnable.run(); 45 46 if (key != null) 47 { 48 int topIndex = 0; 49 int bottomIndex = 0; 50 int length = model.getRowCount(); 51 for (int i = 1;; i++) 52 { 53 if (((topIndex = row + i) < length) || ((bottomIndex = row - i) > -1)) 54 { 55 if (topIndex < length) 56 { 57 Object bean = model.getBean(sorter.modelIndex(topIndex)); 58 if (key.equals(keyRetriever.getKey(bean))) 59 { 60 table.getSelectionModel().clearSelection(); 61 table.getSelectionModel().setLeadSelectionIndex(topIndex); 62 break; 63 } 64 } 65 66 if (bottomIndex > -1) 67 { 68 Object bean = model.getBean(sorter.modelIndex(bottomIndex)); 69 if (key.equals(keyRetriever.getKey(bean))) 70 { 71 table.getSelectionModel().clearSelection(); 72 table.getSelectionModel().setLeadSelectionIndex(bottomIndex); 73 break; 74 } 75 } 76 } 77 else 78 { 79 break; 80 } 81 } 82 } 83 } 84 85 public interface KeyRetriever 86 { 87 String getKey(Object bean); 88 } 89 } | Popular Tags |