java表格模型_[原创]浅谈Java利用表格模型创建表格

[原创]浅谈Java利用表格模型创建表格

发布于 2020-12-14|

复制链接

摘记:         用来创建表格的 JTable 类并不负责存储表格中的数据,而是由表格模型负责存储。当利用 JTable 类直接创建表格时,只是将数据封装到了默认的表格模型中。接下来,我们来学习表格模型的使用方法。利用表格模型创建表格        接口 TableModel 定义了一个表格模 ..

用来创建表格的 JTable 类并不负责存储表格中的数据,而是由表格模型负责存储。当利用 JTable 类直接创建表格时,只是将数据封装到了默认的表格模型中。接下来,我们来学习表格模型的使用方法。利用表格模型创建表格        接口 TableModel 定义了一个表格模型,抽象类 AbstractTableModel 实现了 TableModel 接口的大部分方法,只有一下三个抽象方法没有实现。

```java

(1)public int getRowCount()

(2)public int getColumnCount()

(3)public Object getValueAt(int rowIndex , int columnIndex)

```

通过继承 AbstractTableModel 类实现上面三个抽象方法可以创建自己的表格模型类。DefaultTableModel 类便是由 Swing 提供的继承了 AbstractTableModel 类并实现了上面三个抽象方法的表格模型类。        表格模型创建完成后,通过 JTable 类的构造方法 JTable(TableModel dm)创建表格,就实现了利用表格模型创建表格。        从JDK 1.6 开始,提供了对表格进行排序的功能。通过 JTable 类的 setRowSoreter(RowSortersorter)方法可以为表格设置排序器。TableRowSorter 类是由Swing 提供的排序器类。为表格设置排序器的典型代码如下:

```java

DefaultTableModel tableModel = new DefaultTableModel(); //创建表格模型

JTable table = new JTable(tableModel); //创建表格

table.setRowSorter(new TableRowSorter(tableModel)); //设置排序器

```

如果为表格设置了排序器,当单机表格的某一列头时,在该列名称的后面将出现▲标记,说明按该列升序排列表格中的所有行;当再次单机该列头时,标记将变成▼,说明按该列降序排列表格中的所有行。注意:        在使用表格排序器时,通常要为其设置表格模型。一种方法是通过构造方法 TableRowSorter(TableModel model)创建排序器;另一种方法是通过 setModel(TableModel model)方法为排序器设置表格模型。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 AbstractTableModel 构建Table表格中添加JButton按钮,之前在网上找了2天没有找到好用的程序,最终终于找到一个好用的例子。 不要使,我退你们分。。 sing the Swing JTable class can quickly become a sticky business when you want to customize it to your specific needs. First you must become familiar with how the JTable class is organized. Individual cells are rendered by TableCellRenderer implementations. The table contents are represented by an implementation of the TableModel interface. By default, JTable uses DefaultTableCellRenderer to draw its cells. DefaultTableCellRenderer recognizes a few primitive types, rendering them as strings, and can even display Boolean types as checkboxes. But it defaults to displaying the value returned by toString() for types it does not specifically handle. You have to provide your own TableCellRenderer implementation if you want to display buttons in a JTable. The TableCellRenderer interface contains only one method, getTableCellRendererComponent(...), which returns a java.awt.Component that knows how to draw the contents of a specific cell. Usually, getTableCellRendererComponent() will return the same component for every cell of a column, to avoid the unnecessary use of extra memory. But when the contents of a cell is itself a component, it is all right to return that component as the renderer. Therefore, the first step towards having JButtons display correctly in a JTable is to create a TableCellRenderer implementation that returns the JButton contained in the cell being rendered. In the accompanying code listing, JTableButtonRenderer demonstrates how to do this. Even after creating a custom TableCellRenderer, you're still not done. The TableModel associated with a given JTable does not only keep track of the contents of each cell, but it also keeps track of the class of data stored in each column. DefaultTableModel is designed to work with DefaultTableCellRenderer and will return java.lang.String.class for columns containing data types that it does not specifically handle. The exact method that does this is getColumnClass(int column). Your second step is to create a TableModel implementation that returns JButton.class for cells that contain JButtons. JTableButtonModel shows one way to do this. It just returns the result of getClass() for each piece of cell data. At this point, you're almost done, but not quite. What's the use of putting a JButton in a JTable if you can't press the darn thing? By default, JTable will not forward mouse events to components contained in its cells. If you want to be able to press the buttons you add to JTable, you have to create your own MouseListener that forwards events to the JButton cells. JTableButtonMouseListener demonstrates how you could do this.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值