从 JTable 中的排序中排除列

发布于 2024-10-15 15:56:13 字数 236 浏览 1 评论 0原文

我有一个简单的 Swing JTable 和一个我制作的 TableRowSorter。但是,我会从排序中排除第一列,因为我想保留它来显示行号。 我看不到任何东西,除了

sorter.setSortable(0, false);

这使得该列不可单击,但在单击另一列时仍然可以排序...... 那么简单的问题是:如何防止 TableRowSorter 对列进行排序?

谢谢你!

I have a simple Swing JTable and a TableRowSorter made by me. However, I would exclude the first column from the sorting, as I want to keep it to show row numbers.
I can't see to find anything, except

sorter.setSortable(0, false);

Which makes the column not clickable, but still sortable when another column is clicked...
So quick question will be: how to keep a column from being sorter by a TableRowSorter?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如此安好 2024-10-22 15:56:13

因此,使用 JTable(如下所示)对 A 列进行排序将产生以下结果。但是,您希望对数据进行排序,而不是对行号进行排序,对吗?

|row| column A  |       |row| column A  |
+---+-----------+       +---+-----------+
| 1 | blah blah |  -->  | 1 | blah blah |
| 2 | something |       | 3 | more blah |
| 3 | more blah |       | 2 | something |

我会用 TableCellRenderer 来解决这个问题对于第 0 列。技巧是忽略传递的值,而使用行参数。

public class RowRenderer extends JLabel implements TableCellRenderer {
    public Component getTableCellRendererComponent(JTable table, Object color,
        boolean isSelected, boolean hasFocus, int row, int column) {
        setText(Integer.toString(row));
        return this;
    }
}

注意:如果您要对表格进行分页(即模型不包含所有行;例如仅第 100-200 行),您需要向单元格渲染器告知要添加到 < code>row 获取要显示的行号。

So, with a JTable (ex below) sorting on column A would produce the following. However, you want the data to sort, but not the row numbers, correct?

|row| column A  |       |row| column A  |
+---+-----------+       +---+-----------+
| 1 | blah blah |  -->  | 1 | blah blah |
| 2 | something |       | 3 | more blah |
| 3 | more blah |       | 2 | something |

I would approach this with a TableCellRenderer for column 0. The trick is to ignore the value passed and instead use the row parameter.

public class RowRenderer extends JLabel implements TableCellRenderer {
    public Component getTableCellRendererComponent(JTable table, Object color,
        boolean isSelected, boolean hasFocus, int row, int column) {
        setText(Integer.toString(row));
        return this;
    }
}

Note: if you are paginating your table (ie the model does not contain all of the rows; for example only rows 100-200) you will need to advise the cell renderer of the amount to add to row to obtain the row number to display.

小镇女孩 2024-10-22 15:56:13

JTable 是围绕显示数据行而不是数据单元格而设计的,因此正如您所说,实际上不可能阻止单个列进行排序。相反,我会尝试修改您的 TableModel 以返回该列的值的行索引:

@Override public Object getValueAt(int rowIndex, int columnIndex) {
    if (columnIndex == 0) return rowIndex;
    else {
        // handle other columns
    }
}

如果这不起作用,您也可以尝试修改表格单元格渲染器以使用表格行索引。

A JTable is designed around displaying rows of data, not cells of data, so it isn't really possible to prevent an individual column from sorting, as you put it. Instead I would try modifying your TableModel to return the row index for the value for that column:

@Override public Object getValueAt(int rowIndex, int columnIndex) {
    if (columnIndex == 0) return rowIndex;
    else {
        // handle other columns
    }
}

If that doesn't work you could also try modifying the table cell renderer to use the table row index instead.

甜妞爱困 2024-10-22 15:56:13

如果行号不是数据的一部分,则不应将其存储在模型中。

相反,我会使用显示行号的行标题。就像您在 Excel 中看到的那样。您可以使用行号表来实现此目的。

If the row number isn't part of the data, then it should not be stored in the model.

Instead I would use a row header that displays a row number. Something like you would see in Excel. You can use the Row Number Table for this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文