On creating original tables by using table model in Java

The JTable class used to create a table is not responsible for storing the data in the table, but is stored by the table model. When the JTable class is used to directly create a table, it just encapsulates the data into the default table model. Next, let's learn how to use the table model.

Create a table using a table model

The interface tablemodel defines a table model. The abstract class abstracttablemodel implements most of the methods of the tablemodel interface. Only the following three abstract methods are not implemented.

By inheriting the abstracttablemodel class and implementing the above three abstract methods, you can create your own table model class. The defaulttablemodel class is a table model class provided by swing that inherits the abstracttablemodel class and implements the above three abstract methods.

After the table model is created, the table is created through the construction method JTable (tablemodel DM) of JTable class, and the table is created using the table model.

Starting from JDK 1.6, it provides the function of sorting tables. You can set the sorter for the table through the setrowsorter (rowsorter sorter) method of JTable class. The tablerowsorter class is a sorter class provided by swing. Typical codes for setting a sorter for a table are as follows:

If a sorter is set for a table, when a column header of a stand-alone table, a ▲ mark will appear after the column name, indicating that all rows in the table are arranged in ascending order of the column; When the column header is selected again, the tag will change to indicate that all rows in the table are arranged in descending order of the column.

be careful:

When using a table sorter, you usually set up a table model for it. One method is to create a sorter by constructing the method tablerowsorter (tablemodel model); Another method is to set the table model for the sorter through the setmodel (tablemodel model) method.

The above is all the content of this article. Thank you for your support for this site. It will be more wonderful later!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>