JTable是Swing编程中很常用的控件,这里总结了一些常用方法以备查阅.欢迎补充,转载请注明作者与出处.
一.创建表格控件的各种方式:
1)
调用无参构造函数.
JTable table
=
new
JTable();
2) 以表头和表数据创建表格.
Object[][] cellData
=
{{"row1-col1", "row1-col2"},{"row2-col1", "row2-col2"}}
;
String[] columnNames
=
{"col1", "col2"}
;
JTable table
=
new
JTable(cellData, columnNames);
3) 以表头和表数据创建表格,并且让表单元格不可改.
String[] headers
=
{ "表头一", "表头二", "表头三" }
;
Object[][] cellData
=
null
;
DefaultTableModel model
=
new
DefaultTableModel(cellData, headers)
{
public boolean isCellEditable(int row, int column) {
return false;
}
}
;
table
=
new
JTable(model);
二.对表格列的控制
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类如下:
public
class
Member
{
// 名称
private String name;
// 年龄
private String age;
// 职务
private String title;
}
填充数据的代码:
public
void
fillTable(List
<
Member
>
members)
{
DefaultTableModel tableModel = (DefaultTableModel) table
.getModel();
tableModel.setRowCount(0);// 清除原有行
// 填充数据
for(Member member:members){
String[] arr=new String[3];
arr[0]=member.getName();
arr[1]=member.getAge();
arr[2]=member.getTitle();
// 添加数据到表格
tableModel.addRow(arr);
}
// 更新表格
table.invalidate();
}
2) 取得表格中的数据
public
List
<
Member
>
getShowMembers()
{
List<Member> members=new ArrayList<Member>();
DefaultTableModel tableModel = (DefaultTableModel) table
.getModel();
int rowCount=tableModel.getRowCount();
for(int i=0;i<rowCount;i++){
Member member=new Member();
member.setName((String)tableModel.getValueAt(i, 0));// 取得第i行第一列的数据
member.setAge((String)tableModel.getValueAt(i, 1));// 取得第i行第二列的数据
member.setTitle((String)tableModel.getValueAt(i, 2));// 取得第i行第三列的数据
members.add(member);
}
return members;
}
五.取得用户所选的行
1) 取得用户所选的单行
int
selectRows
=
table.getSelectedRows().length;
//
取得用户所选行的行数
DefaultTableModel tableModel
=
(DefaultTableModel) table.getModel();
if
(selectRows
==
1
)
{
int selectedRowIndex = table.getSelectedRow(); // 取得用户所选单行
.// 进行相关处理
}
2) 取得用户所选的多行
int
selectRows
=
table.getSelectedRows().length;
//
取得用户所选行的行数
DefaultTableModel tableModel
=
(DefaultTableModel) table.getModel();
if
(selectRows
>
1
)
int
[] selRowIndexs
=
table.getSelectedRows();
//
用户所选行的序列
for
(
int
i
=
0
;i
<
selRowIndexs.length;i
++
)
{
// 用tableModel.getValueAt(row, column)取单元格数据
String cellValue=(String) tableModel.getValueAt(i, 1);
}
}
六.添加表格的事件处理
view.getTable().addMouseListener(
new
MouseListener()
{
public void mousePressed(MouseEvent e) {
// 鼠标按下时的处理
}
public void mouseReleased(MouseEvent e) {
// 鼠标松开时的处理
}
public void mouseEntered(MouseEvent e) {
// 鼠标进入表格时的处理
}
public void mouseExited(MouseEvent e) {
// 鼠标退出表格时的处理
}
public void mouseClicked(MouseEvent e) {
// 鼠标点击时的处理
}
}
);
例子:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class PTest{
public PTest()
{
String head[] = {"学号", "姓名", "成绩(请双击以下空格输入)"};
Icon bug = new ImageIcon(getClass().getResource("789.jpg"));
JLabel a = new JLabel (bug);
JTable a1 = new JTable (20,3);
a1.setRowHeight (30);//设置高度
a1.setDragEnabled(true);
//设置第一、二列不可可编辑
DefaultTableModel tableModel = new DefaultTableModel(20,3){
boolean[] editables = {false, false, true};
public boolean isCellEditable(int row, int col)
{
return editables[col];
}
//使table不可编辑的代码(放在model中有效)全部不可编辑
// public boolean isCellEditable(int row, int column)
// {
// return false;
// }
};
a1.setModel(tableModel);
for (int i=0; i<3; i++)
a1.getModel().setValueAt(head[i], 0, i);
JPanel panelwrap = new JPanel (new FlowLayout ());
JPanel panel = new JPanel (new GridLayout (2,1));
panel.setPreferredSize (new Dimension (500,400));
// panel.setBackground (Color.black);
panel.add (a,BorderLayout.CENTER);
JScrollPane a2 = new JScrollPane (a1);
panel.add (a2,BorderLayout.CENTER);
panelwrap.add(panel);
JFrame frame = new JFrame ("JTableDemo");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setContentPane (panelwrap);
frame.setSize(500,400);
frame.pack();
frame.show();
}
public static void main(String[] args) {
PTest k = new PTest();
}
}
//////////////////////////////////////////自己的//////////////////////////////////////
private JTable getJTable0() {
if (jTable0 == null) {
jTable0 = new JTable();
jTable0.setModel(new DefaultTableModel(new Object[][] { {false, "null", "null", "com1", "null", "null", "null", "null", "null", "null", },
{false, "null", "null", "com2", "null", "null", "null", "null", "null", "null", },
{ false, "null", "null", "com3", "null", "null", "null", "null", "null", "null",},
{ false,"null", "null", "com4", "null", "null", "null", "null", "null", "null", },
{ false,"null", "null", "com5", "null", "null", "null", "null", "null", "null", },
{ false, "null", "null", "com6", "null", "null", "null", "null", "null", "null",},
{ false, "null", "null", "com7", "null", "null", "null", "null", "null", "null", },
{false, "null", "null", "com8", "null", "null", "null", "null", "null", "null", },
{ false,"null", "null", "com9", "null", "null", "null", "null", "null", "null", },
{ false,"null", "null", "com10", "null", "null", "null", "null", "null", "null", },
{false, "null", "null", "com11", "null", "null", "null", "null", "null", "null", },
{ false, "null", "null", "com12", "null", "null", "null", "null", "null", "null", }, }, new String[] {"操作", "ID", "status", "序号", "产品序列号", "半成品序列号",
"所属工作任务号", "产品型号(pn码)", "生产进度状态", "端口状态", }) {
private static final long serialVersionUID = 1L;
Class<?>[] types = new Class<?>[] { Boolean.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class,
Object.class, };
public Class<?> getColumnClass(int columnIndex) {
return types[columnIndex];
}
// //使table不可编辑的代码(放在model中有效)全部不可编辑
// public boolean isCellEditable(int row, int column)
// {
// return false;
// }
//指定列不可编辑
boolean[] editables = {true, false, false,false,false,false,false,false,false,false};
public boolean isCellEditable(int row, int col)
{
return editables[col];
}
//指定行不可编辑
boolean[] editables = {true, false, false,false,false,false,false,false};
public boolean isCellEditable(int row, int col)
{
return editables[row];
}
});
}
return jTable0;
}