JTable是Swing编程中很常用的控件,这里总结了一些常用方法以备查阅.欢迎补充,转载请注明作者与出处.
一.创建表格控件的各种方式:
1) 调用无参构造函数.

JTable table = new JTable();
2) 以表头和表数据创建表格.
3) 以表头和表数据创建表格,并且让表单元格不可改.
二.对表格列的控制
1) 设置列不可随容器组件大小变化自动调整宽度.

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
2) 限制某列的宽度.

TableColumn firsetColumn = table.getColumnModel().getColumn(0);

firsetColumn.setPreferredWidth(30);

firsetColumn.setMaxWidth(30);

firsetColumn.setMinWidth(30);
3) 设置当前列数.

DefaultTableModel tableModel = (DefaultTableModel) table.getModel();

int count=5;

tableModel.setColumnCount(count);
4) 取得表格列数

int cols = table.getColumnCount();
5) 添加列

DefaultTableModel tableModel = (DefaultTableModel) table.getModel();

tableModel.addColumn("新列名");
6) 删除列

table.removeColumn(table.getColumnModel().getColumn(columnIndex));// columnIndex是要删除的列序号
三.对表格行的控制
1) 设置行高

table.setRowHeight(20);
2) 设置当前航数

DefaultTableModel tableModel = (DefaultTableModel) table.getModel();

int n=5;

tableModel.setRowCount(n);
3) 取得表格行数

int rows = table.getRowCount();

4) 添加表格行

DefaultTableModel tableModel = (DefaultTableModel) table.getModel();

tableModel.addRow(new Object[]
{"sitinspring", "35", "Boss"});
5) 删除表格行

DefaultTableModel tableModel = (DefaultTableModel) table.getModel();

model.removeRow(rowIndex);// rowIndex是要删除的行序号
四.存取表格单元格的数据
1) 取单元格数据

DefaultTableModel tableModel = (DefaultTableModel) table.getModel();

String cellValue=(String) tableModel.getValueAt(row, column);// 取单元格数据,row是行号,column是列号
2) 填充数据到表格.
注:数据是Member类型的链表,Member类如下:
填充数据的代码:
2) 取得表格中的数据
五.取得用户所选的行
1) 取得用户所选的单行
2) 取得用户所选的多行
六.添加表格的事件处理
转自:http://www.blogjava.net/sitinspring/archive/2007/12/18/168425.html