[英]How to update JTableModel after the sorter has changed the rows order?
I have implemented a JTable, on which I can search entries with a textField. 我实现了一个JTable,可以在其上搜索带有textField的条目。
When a request is made, my code gets the DefaultTableModel
, looks for the element in each column and row, and then sets the selection in the table to the line where the element was found. 发出请求时,我的代码获取
DefaultTableModel
,在每一列和每一行中查找元素,然后将表中的选择设置为找到该元素的行。
This works, but it's useless if I sort the table by clicking on any column, because the sorting doesn't seem to update the DefaultTableModel
. 这行得通,但是如果我通过单击任何列对表进行排序就没有用,因为排序似乎没有更新
DefaultTableModel
。
The table is part of a bigger project which is extremely complicated and full of dependencies so I cannot post a small example, but I think this will sum it up: 该表是一个更大的项目的一部分,该项目非常复杂并且充满依赖性,因此我无法张贴一个小例子,但是我认为这可以总结一下:
Given a DefaultTableModel A
full of non-sorted data about a JTable B
, where B.setAutoCreateRowSorter()
is true
, how does one update B
after several/any cloumn-sortings of A
? 给定一个
DefaultTableModel A
,其中B.setAutoCreateRowSorter()
有关JTable B
所有未排序数据,其中B.setAutoCreateRowSorter()
为true
,在对A
进行多次/任何排序排序后,如何更新B
?
I have read the docs and also looked into this: 我已经阅读了文档,并进行了调查:
http://www.codejava.net/java-se/swing/6-techniques-for-sorting-jtable-you-should-know http://www.codejava.net/java-se/swing/6-techniques-for-sorting-jtable-you-should-know
As well as dug a bit into TableRowSorter#addRowSorterListener
, but it can only tell me that a column was sorted, not really useful. 除了深入研究
TableRowSorter#addRowSorterListener
,它还只能告诉我列已排序,并不是真正有用。 Unless of course I use what that column is to try and sort all the values in a multidimensional array, then clear the table, then assign everything back.. but clearly this is extremely slow for and not really an option in my case. 当然,除非我使用那一列来尝试对多维数组中的所有值进行排序,然后清除表,然后将所有值都分配回去..但是显然,这对于我来说是非常缓慢的,并不是我的选择。
Refer to this for the info provided by a RowSorterEvent
: 请参阅此以获取
RowSorterEvent
提供的信息:
https://docs.oracle.com/javase/7/docs/api/javax/swing/event/RowSorterEvent.html https://docs.oracle.com/javase/7/docs/api/javax/swing/event/RowSorterEvent.html
Can someone point me in the right direction ? 有人可以指出我正确的方向吗?
When a request is made, my code gets the DefaultTableModel, looks for the element in each column and row...
发出请求时,我的代码获取DefaultTableModel,在每一列和每一行中查找元素...
So don't search the TableModel. 因此,请勿搜索TableModel。 You can use the table.getValueAt(...) method to search for the element in each row/column of the table.
您可以使用table.getValueAt(...)方法在表的每一行/列中搜索元素。 The data will be accessed in the currently sorted order of the table.
将按照表的当前排序顺序访问数据。
because the sorting doesn't seem to update the DefaultTableModel.
因为排序似乎并没有更新DefaultTableModel。
Correct, only the View (JTable) is updated. 正确,只有视图(JTable)被更新。
If you want to keep searching the TableModel directly then you need to convert the model indexes to the view indexes whenever you want to invoke a JTable method (ie. selecting a table row). 如果要继续直接搜索TableModel,则无论何时要调用JTable方法(即选择表行),都需要将模型索引转换为视图索引。 This is done by using the following
JTable
methods: 这是通过使用以下
JTable
方法完成的:
int columnColumn = table.convertColumnIndexToView(modelColumn);
int row = table.convertRowIndexToView(modelRow);
There are also methods to convert the view values to the model values. 还有一些方法可以将视图值转换为模型值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.