且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何通过在外部提供列索引来对 JTable 进行排序

更新时间:2023-12-03 16:53:28

你真的应该坚持 Java 编码标准 在编写 Java 代码时,但无论你走哪条路,你至少应该保持一致.

You should really stick to the Java coding standards when writing Java code, but whatever route you go, you should at the very least be consistent.

查看您的代码,您实际上并没有在桌子上设置排序器.来自 JavaDocs 以下是您的代码中缺少:

Looking at your code you don't actually set the sorter on the table. From the JavaDocs the following is missing from your code:

TableModel myModel = createMyTableModel();
JTable table = new JTable(myModel);
table.setRowSorter(new TableRowSorter(myModel));

所以它在你的代码中看起来像这样(或者至少删除了你的非标准约定)

So it would look something like this in your code (or at least with your non-standard conventions removed)

public void createTable() {
    tableModel = new DefaultTableModel();
    table = new JTable(tableModel);
    sorter = new TableRowSorter<DefaultTableModel>(tableModel);
    table.setRowSorter(sorter);
    ...
}