- 首先上一个普通的表格代码:
public class TestTable {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(new BorderLayout());
//如果不放在JScrollPane里面,就不会显示表头
String[] columnNames = new String[]{"id", "name", "hp", "damage"};
String[][] heros = new String[][]{{"1", "盖伦", "616", "100"},
{"2", "提莫", "512", "102"}, {"3", "奎因", "832", "200"}};
JTable t = new JTable(heros, columnNames);
// 根据t创建 JScrollPane
JScrollPane sp = new JScrollPane(t);
//或则创建一个空的JScrollPane,再通过setViewportView把table放在JScrollPane中
// JScrollPane sp = new JScrollPane(t);
// sp.setViewportView(t);
// 设置列宽度
t.getColumnModel().getColumn(0).setPreferredWidth(10);
// 把sp而非JTable加入到JFrame上,
f.add(sp, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
- TableModel
首先说下TableModel的设计思想,在Model这种思想的指导下,数据和显示分离开来了。 比如对于JTable而言,有数据部分,也有显示部分(比如列宽等信息)。 数据部分,专门做一个类,叫做TableModel,就用于存放要显示的数据。
public class HeroTableModel extends AbstractTableModel {
String[] columnNames = new String[]{"id", "name", "hp", "damage"};
String[][] heros = new String[][]{{"1", "盖伦", "616", "100"},
{"2", "提莫", "512", "102"}, {"3", "奎因", "832", "200"}};
// 返回一共有多少行
public int getRowCount() {
// TODO Auto-generated method stub
return heros.length;
}
// 返回一共有多少列
public int getColumnCount() {
// TODO Auto-generated method stub
return columnNames.length;
}
// 获取每一列的名称
public String getColumnName(int columnIndex) {
// TODO Auto-generated method stub
return columnNames[columnIndex];
}
// 单元格是否可以修改
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
// 每一个单元格里的值
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return heros[rowIndex][columnIndex];
}
}
class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(new BorderLayout());
//创建一个TableModel
HeroTableModel htm = new HeroTableModel();
//根据 TableModel来创建 Table
JTable t = new JTable(htm);
JScrollPane sp = new JScrollPane(t);
//只需要修改数据后,调用updateUI()方法即可刷新界面数据
// htm.heros = new String[][]{{"1", "盖伦", "616", "101"},
// {"2", "提莫", "512", "103"}, {"3", "奎因", "832", "201"}};
// t.updateUI();
f.add(sp, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
- 选择具体一行数据
public class TestTable2 {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(new BorderLayout());
final HeroTableModel htm = new HeroTableModel();
final JTable t = new JTable(htm);
// 准备一个Panel上面放一个Label用于显示哪条被选中了
JPanel p = new JPanel();
final JLabel l = new JLabel("暂时未选中条目");
p.add(l);
JScrollPane sp = new JScrollPane(t);
// 使用selection监听器来监听table的哪个条目被选中
t.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
// 当选择了某一行的时候触发该事件
public void valueChanged(ListSelectionEvent e) {
// 获取哪一行被选中了
int row = t.getSelectedRow() + 1;
// 更新标签内容
l.setText("当前选中的是第" + row + "行");
}
});
f.add(p, BorderLayout.NORTH);
f.add(sp, BorderLayout.CENTER);
// //默认选择第一行
// t.getSelectionModel().setSelectionInterval(0, 0);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
- 更新
数据更新时,只需要更新model类中的数据,然后调用JTable的updateUI()方法即可。具体代码可以查看上面第二个完整代码中被注释部分。
Q.E.D.