JTable实用教程

提到Java GUI编程,有一个组件我们不得不掌握,那就是用来显示管理数据的JTable。今天我们就来一起学习JTable的简单使用。
对于JTable,JDK是这样介绍的:
The JTable is used to display and edit regular two-dimensional tables of cells.
The JTable has many facilities that make it possible to customize its rendering and editing but provides defaults for these features so that simple tables can be set up easily. For example, to set up a table with 10 rows and 10 columns of numbers:
TableModel dataModel = new AbstractTableModel() { public int getColumnCount() { return 10; } public int getRowCount() { return 10; } public Object getValueAt(int row, int col) { return new Integer(row*col); } }; JTable table = new JTable(dataModel); JScrollPane scrollpane = new JScrollPane(table);

JTable 用来显示和编辑常规二维单元表。并且,我们要使用表之前要实现抽象类AbstractTableModel,用来表示表格的数据结构。这个我们在上面给出的程序片段就可以看出来。那么有没有简单一些的方法,答案是肯定的,于是JDK上出现了如下一段话:
The DefaultTableModel is a model implementation that uses a Vector of Vectors of Objects to store the cell values. As well as copying the data from an application into the DefaultTableModel, it is also possible to wrap the data in the methods of the TableModel interface so that the data can be passed to the JTable directly, as in the example above. This often results in more efficient applications because the model is free to choose the internal representation that best suits the data. A good rule of thumb for deciding whether to use the AbstractTableModel or the DefaultTableModel is to use the AbstractTableModel as the base class for creating subclasses and the DefaultTableModel when subclassing is not required.
DefaultTableModelAbstractTableModel的实现类。除了可以用 TableModel 接口的方法来包装数据外,还可以将数据从应用程序复制到 DefaultTableModel 中这样可将数据直接传递到 JTable。在决定使用 AbstractTableModel 还是使用 DefaultTableModel 方面有一个好的实践经验,即在创建子类时使用 AbstractTableModel 作为基类,在不需要创建子类时则使用 DefaultTableModel。
在一般模式下,我们使用DefaultTableModel便可以满足我们的需求,我们今天也是主要讲解DefaultTableModel+JTable的数据模式。
使用JTable的步骤如下:
* 创建DefaultTableModel,设置好数据与表格的数据结构
* 使用创建好的DefaultTableModel创建JTable对象
* 使用JTable
创建DefaultTableModel对象 DefaultTableModel有好几个构造方法,我们一般使用图中选中的那个:

public DefaultTableModel(Object[][] data, Object[] columnNames) 构造一个 DefaultTableModel,并通过将 data 和 columnNames 传递到 setDataVector 方法来初始化该表。 Object[][] 数组中的第一个索引是行索引,第二个索引是列索引。 参数: data - 表的数据 columnNames - 列的名称

首先我们需要一个数组来表示表格的属性名,一般使用String[],我们还需要一个二维表来装载数据。
model = new DefaultTableModel( new String[][]{{"德怀特.霍华德", "老鹰", "12"}, {"德里克.罗斯", "尼克斯", "1"}}, new String[]{"Name", "Team", "Number"});

创建JTable对象 这个就很简单了,直接把DefaultTableModel传入JTable的构造方法。
JTable table = new JTable(model);

其实使用JTable的关键还是在于DefaultTableModel的使用。
现在我们可以运行程序来显示目前这个表格看看。
public class Main extends JFrame{private DefaultTableModel model; private JTable table; public Main() { this.setSize(500, 300); initTable(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); }private void initTable() { model = new DefaultTableModel( new String[][]{{"德怀特.霍华德", "老鹰", "12"}, {"德里克.罗斯", "尼克斯", "1"}}, new String[]{"Name", "Team", "Number"}); table = new JTable(model); JScrollPane jsp = new JScrollPane(); jsp.setViewportView(table); this.add(jsp); }public static void main(String[] args) { Main frame = new Main(); }}


使用JTable 类似于数据库,我们要使用JTable无非是要实现对表格进行数据的增删查改操作,对于数据层面的操作,我们一般使用DefaultTableModel这个对象进行数据的管理。接下来我们一一实现。
  • Insert
public void addRow(Object[] rowData) 添加一行到模型的结尾。如果未指定 rowData,则新行将包含 null 值。将生成添加行的通知。 参数: rowData - 要添加的行数据(可选)public void insertRow(int row, Vector rowData) 在模型中的 row 位置插入一行。如果未指定 rowData,则新行将包含 null 值。将生成添加行的通知。 参数: row - 要插入的行的行索引 rowData - 要添加的行数据(可选) 抛出: ArrayIndexOutOfBoundsException - 如果 row 无效

model.addRow(new String[]{"周琦", "火箭", "11"});


  • Delete
public void removeRow(int row) 移除模型中 row 位置的行。向所有侦听器发送移除行的通知。 参数: row - 要移除的行的行索引 抛出: ArrayIndexOutOfBoundsException - 如果 row 无效

model.removeRow(0);


  • Select
public Object getValueAt(int row, int column) 返回 row 和 column 处单元格的属性值。 指定者: 接口 TableModel 中的 getValueAt 参数: row - 要查询其值的行 column - 要查询其值的列 返回: 指定单元格处的值 Object 抛出: ArrayIndexOutOfBoundsException - 如果给定了无效的 row 或 column

String str = (String)model.getValueAt(0, 0); System.out.println(str);


  • Update
public void setValueAt(Object aValue, int row, int column) 设置 column 和 row 处单元格的对象值。 aValue 是新值。此方法将生成一个 tableChanged 通知。 指定者: 接口 TableModel 中的 setValueAt 覆盖: 类 AbstractTableModel 中的 setValueAt 参数: aValue - 新值,可以为 null row - 要更改其值的行 column - 要更改其值的列 抛出: ArrayIndexOutOfBoundsException - 如果给定了无效的 row 或 column

model.setValueAt("公牛", 0, 1);


修改JTable常用属性 1.设置表格字体居中
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(); renderer.setHorizontalAlignment(SwingConstants.CENTER); table.setDefaultRenderer(Object.class, renderer);


2.设置表格数据不可编辑
DefaultTableModel model = new DefaultTableModel( new String[][]{{"德怀特.霍华德", "老鹰", "12"}, {"德里克.罗斯", "尼克斯", "1"}}, new String[]{"Name", "Team", "Number"}) { @Override public boolean isCellEditable(int row, int column) { return false; }};

3.获取表格选中行号
table.getSelectedRow();

END 【JTable实用教程】Demo下载地址

    推荐阅读