从 JTable 中的排序中排除列
我有一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因此,使用 JTable(如下所示)对 A 列进行排序将产生以下结果。但是,您希望对数据进行排序,而不是对行号进行排序,对吗?
我会用 TableCellRenderer 来解决这个问题对于第 0 列。技巧是忽略传递的值,而使用行参数。
注意:如果您要对表格进行分页(即模型不包含所有行;例如仅第 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?
I would approach this with a TableCellRenderer for column 0. The trick is to ignore the value passed and instead use the row parameter.
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.JTable 是围绕显示数据行而不是数据单元格而设计的,因此正如您所说,实际上不可能阻止单个列进行排序。相反,我会尝试修改您的
TableModel
以返回该列的值的行索引:如果这不起作用,您也可以尝试修改表格单元格渲染器以使用表格行索引。
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:If that doesn't work you could also try modifying the table cell renderer to use the table row index instead.
如果行号不是数据的一部分,则不应将其存储在模型中。
相反,我会使用显示行号的行标题。就像您在 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.