1 10 11 12 package org.enhydra.shark.corbaclient; 13 14 import java.awt.*; 15 import java.util.*; 16 17 import javax.swing.*; 18 import javax.swing.border.*; 19 import javax.swing.table.*; 20 21 25 public class TablePanel extends JPanel { 26 27 private JTable allItems; 28 29 public TablePanel (Vector columnNames, boolean isFirstColumnVisible) { 30 super(); 31 32 int emptyBorderHSize=10; 33 int emptyBorderVSize=10; 34 Border emptyb=BorderFactory.createEmptyBorder(emptyBorderVSize,emptyBorderHSize, 35 emptyBorderVSize,emptyBorderHSize); 36 Border inb=BorderFactory.createMatteBorder(1,1,1,1,Color.darkGray); 37 inb=BorderFactory.createTitledBorder(inb,""); 38 39 setBorder(BorderFactory.createCompoundBorder(emptyb,inb)); 40 setLayout(new BoxLayout(this,BoxLayout.Y_AXIS)); 41 42 allItems=new JTable(new Vector(),columnNames) { 44 public boolean isCellEditable(int row, int col) { 45 return false; 46 } 47 }; 48 49 if (!isFirstColumnVisible) { 50 TableColumnModel tcm=allItems.getColumnModel(); 51 TableColumn column; 52 column = allItems.getColumnModel().getColumn(0); 54 column.setMinWidth(0); 55 column.setMaxWidth(0); 56 column.setPreferredWidth(0); 57 column.setResizable(false); 58 } 59 60 allItems.setColumnSelectionAllowed(false); 62 allItems.setRowSelectionAllowed(true); 63 allItems.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 64 allItems.getTableHeader().setReorderingAllowed(false); 65 66 JScrollPane allItemsPane=new JScrollPane(); 68 allItemsPane.setViewportView(allItems); 69 71 add(allItemsPane); 72 add(Box.createVerticalGlue()); 73 } 74 75 public JTable getTable () { 76 return allItems; 77 } 78 79 public Object getColumnValueOfSelectedRow (int columnNo) { 80 int row=allItems.getSelectedRow(); 81 Object frw=null; 82 if (row>=0) { 83 try { 84 frw=allItems.getValueAt(row,columnNo); 85 } catch (Exception ex) { 86 return null; 87 } 88 } 89 return frw; 90 } 91 92 public void setColumnValueOfSelectedRow (int columnNo,Object val) { 93 int row=allItems.getSelectedRow(); 94 if (row>=0) { 95 try { 96 allItems.setValueAt(val,row,columnNo); 97 } catch (Exception ex) { 98 } 99 } 100 } 101 102 public void addElement(Vector v) throws Exception { 103 DefaultTableModel dtm=(DefaultTableModel)allItems.getModel(); 104 try { 105 dtm.addRow(v); 106 } catch (Exception ex) { 107 System.out.println("Cannot add row to the table"); 108 } 109 } 110 111 116 public void removeElement(Object [] columnValues) { 117 if (columnValues==null || columnValues.length==0) return; 118 int rowCnt=allItems.getRowCount(); 119 DefaultTableModel dtm=(DefaultTableModel)allItems.getModel(); 120 for (int row=0; row<rowCnt; row++) { 121 boolean canRemove=true; 122 for (int col=0; col<columnValues.length; col++) { 123 canRemove=canRemove && allItems.getValueAt(row,col).equals(columnValues[col]); 124 if (!canRemove) break; 125 } 126 if (canRemove) { 127 dtm.removeRow(row); 128 break; 129 } 130 } 131 } 132 133 public void removeAll () { 134 DefaultTableModel dtm=(DefaultTableModel)allItems.getModel(); 135 while (true) { 136 try { 137 dtm.removeRow(0); 138 } catch (Exception ex) { 139 break; 140 } 141 } 142 } 143 144 } 145 146 147 | Popular Tags |