JTable sorting rows in Java 1.5

半城伤御伤魂 提交于 2019-12-01 00:43:27

Sorting in Java 1.5 is only possible via libraries.

E.g. use the JXTable mentioned from Kaarel or VLTable from here.

Another good library is glazedlists

which is also used in the Spring Rich Client project.

There are even ways to use Glazed Lists with JXTable

Use the JXTable from the SwingX project, see e.g.

JXTable requires a big package, and is difficult to get the right version for. (no higher than version 1.0 for Java 1.5).

Try instead TableSorter.java. Get it at:

http://ouroborus.org/java/2.1/TableSorter.java

And insert it in your project.

Now you wrap your tableModel in an instance of TableSorter, and instert that into the JTable. TableSorter acts as a "go-between" the JTable-instance and your tableModel.

Use it something like this (code untested):

JTable myTable = new JTable();
TableSorter mySorter = new TableSorter(myTableModel, myTable.getTableHeader());
myTable.setTableModel(mySorter);

You can set sprting programmatically like this:

mySorter.setSortingStatus(0,TableSorter.ASCENDING);

Try tweeking MouseHandler.mouseClicked() to get it to skip the NOT_SORTED option in the clicking order, and mess with renderers for e better column header and placing and visibility of triangle.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!