1 package com.copy; 2 3 import java.awt.BorderLayout; 4 import java.awt.Color; 5 import java.awt.Component; 6 import java.awt.event.ActionEvent; 7 import java.awt.event.ActionListener; 8 import java.util.Vector; 9 10 import javax.swing.DefaultCellEditor; 11 import javax.swing.JButton; 12 import javax.swing.JComboBox; 13 import javax.swing.JFrame; 14 import javax.swing.JPanel; 15 import javax.swing.JScrollPane; 16 import javax.swing.JTable; 17 import javax.swing.ListSelectionModel; 18 import javax.swing.event.ListSelectionEvent; 19 import javax.swing.event.ListSelectionListener; 20 import javax.swing.table.AbstractTableModel; 21 import javax.swing.table.DefaultTableCellRenderer; 22 import javax.swing.table.TableColumnModel; 23 24 /** 25 * 26 * @项目名称:TestTable 27 * @类名称:Test 28 * @类描述: swing 三层演示 以及 Jtable小测试 29 * @创建人:wangyongfei 30 * @创建时间:2012-9-18 上午10:57:40 31 * @修改人:wangyongfei 32 * @修改时间:2012-9-18 上午10:57:40 33 * @修改备注: 34 * @version 35 */ 36 public class TestTable { 37 38 private JFrame frame; 39 private JTable table; 40 private Table_Model model; 41 private JScrollPane s_pan; 42 private JButton btnDelete; 43 private JButton btnAdd; 44 private JButton btnSave; 45 private JPanel pane; 46 private JComboBox sexBox; 47 private JBoxTestCell jc; 48 49 public TestTable() { 50 frame = new JFrame("JTable测试"); 51 pane = new JPanel(); 52 btnDelete = new JButton("删除"); 53 btnDelete.addActionListener(new ActionListener() { 54 public void actionPerformed(ActionEvent e) { 55 int row = table.getSelectedRow(); 56 removeData(row); 57 } 58 }); 59 btnAdd = new JButton("添加"); 60 btnAdd.addActionListener(new ActionListener() { 61 public void actionPerformed(ActionEvent e) { 62 addData(); 63 } 64 }); 65 btnSave = new JButton("保存"); 66 btnSave.addActionListener(new ActionListener() { 67 public void actionPerformed(ActionEvent e) { 68 saveData(); 69 } 70 }); 71 pane.add(btnDelete); 72 pane.add(btnAdd); 73 pane.add(btnSave); 74 model = new Table_Model(20); 75 table = new JTable(model); 76 77 //得到选中的行号 78 ListSelectionModel model = table.getSelectionModel(); 79 model.addListSelectionListener(new ListSelectionListener() { 80 @Override 81 public void valueChanged(ListSelectionEvent e) { 82 if(table.getSelectedRowCount() > 0) 83 System.out.println(table.getSelectedRow()); 84 } 85 }); 86 table.setRowHeight(30); 87 table.setGridColor(Color.red); 88 table.setBackground(Color.white); 89 try { 90 table.setDefaultRenderer(Class.forName("java.lang.Object"), new TableRender()); 91 } catch (ClassNotFoundException e1) { 92 e1.printStackTrace(); 93 } 94 95 jc = new JBoxTestCell(); 96 TableColumnModel tcm = table.getColumnModel(); 97 tcm.getColumn(3).setCellEditor(jc); 98 tcm.getColumn(0).setPreferredWidth(50); 99 tcm.getColumn(1).setPreferredWidth(100); 100 101 String[] sex = { "女", "男", "未知" }; 102 sexBox = new JComboBox(sex); 103 sexBox.setSelectedIndex(0); 104 TableColumnModel sexT = table.getColumnModel(); 105 sexT.getColumn(2).setCellEditor(new DefaultCellEditor(sexBox)); 106 107 s_pan = new JScrollPane(table); 108 frame.getContentPane().add(s_pan, BorderLayout.CENTER); 109 frame.getContentPane().add(pane, BorderLayout.NORTH); 110 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 111 frame.setSize(300, 200); 112 frame.setVisible(true); 113 114 } 115 116 private void addData() { 117 Object[] str = {"小张", "", ""}; 118 model.addRow(str); 119 table.updateUI(); 120 } 121 122 /** 123 * 清除选中行 124 * @param row 125 */ 126 private void removeData(int row) { 127 model.removeRows(row); 128 table.updateUI(); 129 } 130 131 /** 132 * 打印到控制台 133 */ 134 private void saveData() { 135 136 int col = model.getColumnCount(); 137 int row = model.getRowCount(); 138 int i = 0; 139 while(i < col){ 140 System.out.print(model.getColumnName(i) + "\t"); 141 i++; 142 } 143 System.out.print("\r\n"); 144 int k = 0, j = 0; 145 while(k < row){ 146 while(j < col){ 147 System.out.print(model.getValueAt(k, j) + "\t"); 148 j++; 149 } 150 k++; 151 } 152 System.out.print("\r\n"); 153 } 154 155 public static void main(String args[]) { 156 new TestTable(); 157 } 158 159 } 160 161 162 /** 163 *模型 164 */ 165 class Table_Model extends AbstractTableModel { 166 private Vector vector = null; 167 private String[] titleName = { "编号", "姓名", "性别", "爱好" }; 168 public Table_Model() { 169 vector = new Vector(); 170 } 171 public Table_Model(int count) { 172 vector = new Vector(count); 173 } 174 175 public void addRow(Object[] str) { 176 177 Vector v = new Vector(titleName.length); 178 v.add(0, new Integer(vector.size())); 179 for(int i = 1; i <= str.length; i++){ 180 v.add(i, str[i-1]); 181 182 } 183 vector.add(v); 184 } 185 186 /** 187 * 删除选中行 188 * @param row 189 */ 190 public void removeRows(int row) { 191 vector.remove(row); 192 } 193 194 /** 195 * 是否为可编辑 196 */ 197 public boolean isCellEditable(int rowIndex, int columnIndex) { 198 if (columnIndex == 0) { 199 return false; 200 } 201 return true; 202 } 203 204 /** 205 * 使修改的内容生效 206 */ 207 public void setValueAt(Object value, int row, int col) { 208 ((Vector) vector.get(row)).remove(col); 209 ((Vector) vector.get(row)).add(col, value); 210 this.fireTableCellUpdated(row, col);//通知所有侦听器,已更新 [row, column] 处的单元格值。 211 } 212 213 public String getColumnName(int col) { 214 return titleName[col]; 215 } 216 217 public int getColumnCount() { 218 return titleName.length; 219 } 220 221 public int getRowCount() { 222 return vector.size(); 223 } 224 225 /** 226 * 返回 col 和 row 位置的单元格值。 227 */ 228 public Object getValueAt(int row, int col) { 229 return ((Vector) vector.get(row)).get(col); 230 } 231 232 /** 233 * 针对列中所有的单元格值,返回最具体的超类 234 * 返回数据类型 235 */ 236 public Class getColumnClass(int col) { 237 return getValueAt(0, col).getClass(); 238 } 239 240 } 241 /** 242 * 视图处理 243 */ 244 class TableRender extends DefaultTableCellRenderer{ 245 private int row; 246 private int column; 247 private String value; 248 public Component getTableCellRendererComponent(JTable table, Object value, 249 boolean isSelected, boolean hasFocus, int row, int column) { 250 Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 251 if(hasFocus){ 252 this.row = row; 253 this.column = column; 254 this.value = value.toString(); 255 // cell.setBackground(Color.RED); 256 // cell.setForeground(Color.BLACK); 257 } 258 return cell; 259 } 260 public int getSelectedRow(){ 261 return row; 262 } 263 public int getSelectedColumn(){ 264 return column; 265 } 266 public String getSelectedValue(){ 267 return value; 268 } 269 270 }
1 package com.copy; 2 3 import java.awt.BorderLayout; 4 import java.awt.Component; 5 import java.awt.Dimension; 6 import java.util.EventObject; 7 8 import javax.swing.AbstractCellEditor; 9 import javax.swing.JComboBox; 10 import javax.swing.JPanel; 11 import javax.swing.JTable; 12 import javax.swing.table.TableCellEditor; 13 14 public class JBoxTestCell extends AbstractCellEditor implements 15 TableCellEditor { 16 int row; 17 private JTable table; 18 private JPanel p1; 19 private JComboBox jbxType1; 20 private JComboBox jbxType2; 21 private Object val = null; 22 public JBoxTestCell() { 23 p1 = new JPanel(new BorderLayout()); 24 p1.setPreferredSize(new Dimension(300, 30)); 25 String[] interest1 = { "足球1", "游泳1", "拉丁舞1", "击剑1", "马术1", "读书1", "旅游1" }; 26 jbxType1 = new JComboBox(interest1); 27 jbxType1.setPreferredSize(new Dimension(80,25)); 28 29 String[] interest2 = { "足球2", "游泳2", "拉丁舞2", "击剑2", "马术2", "读书2", "旅游2" }; 30 jbxType2 = new JComboBox(interest2); 31 jbxType2.setPreferredSize(new Dimension(80,25)); 32 jbxType1.setSelectedIndex(0); 33 jbxType2.setSelectedIndex(0); 34 p1.add(jbxType1, BorderLayout.WEST); 35 p1.add(jbxType2, BorderLayout.EAST); 36 } 37 38 @Override 39 public boolean isCellEditable(EventObject anEvent) { 40 return true; 41 } 42 public void showSave() { 43 jbxType1.setVisible(true); 44 jbxType2.setVisible(true); 45 } 46 47 public Object getCellEditorValue() { 48 String v1 = jbxType1.getSelectedItem().toString(); 49 String v2 = jbxType2.getSelectedItem().toString(); 50 return v1+", "+v2; 51 52 } 53 54 public Component getTableCellEditorComponent(JTable table, Object value, 55 boolean isSelected, int row, int column) { 56 this.table = table; 57 this.row = row; 58 val = value; 59 return this.p1; 60 } 61 }
界面展示: