使用自定义 AbstractTableModel 进行排序
我创建了自己的类来扩展 AbstractTableModel。这个想法是能够存储隐藏的“id”列。以下代码可以很好地实现此目的:
public class GuiTableModel extends AbstractTableModel {
List<Object[]> rowlist = new ArrayList<Object[]>();
List<String> colNames = new ArrayList<String>();
public void addRow(Object[] data) {
rowlist.add(data);
}
public void addColumn(String name) {
colNames.add(name);
}
public int getColumnCount() {
return colNames.size();
}
public int getRowCount() {
return rowlist.size();
}
public Object getValueAt(int row, int col) {
return rowlist.get(row)[col];
}
@Override
public String getColumnName(int column) {
return colNames.get(column);
}
@Override
public boolean isCellEditable(int rowIndex, int mColIndex) {
return false;
}
正如您在上面所看到的,它是一个非常简单的实现。但问题是,当有人通过单击 JTable 中的列标题按列排序时,AbstractTableModel 中的列表不会更新。我需要列表与视觉布局同步。
关于我如何做到这一点有什么想法吗?
非常感谢
I've made my own class which extends AbstractTableModel. The idea is to be able to store a hidden "id" collumn. The following code works well for this purpose:
public class GuiTableModel extends AbstractTableModel {
List<Object[]> rowlist = new ArrayList<Object[]>();
List<String> colNames = new ArrayList<String>();
public void addRow(Object[] data) {
rowlist.add(data);
}
public void addColumn(String name) {
colNames.add(name);
}
public int getColumnCount() {
return colNames.size();
}
public int getRowCount() {
return rowlist.size();
}
public Object getValueAt(int row, int col) {
return rowlist.get(row)[col];
}
@Override
public String getColumnName(int column) {
return colNames.get(column);
}
@Override
public boolean isCellEditable(int rowIndex, int mColIndex) {
return false;
}
As you can see above, its a very simple implementation. The problem is though, when someone sorts by column by clicking the column title in the JTable, the lists in the AbstractTableModel don't get updated. I need the lists to be in sync with the visual layout.
Any ideas on how I can do this?
Many Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
而这正是它应该工作的方式。当您对列进行排序或重新排序时,模型中的数据不应更改。只是观点发生了变化。
如果你想从表中获取数据,那么你应该使用:
它会给你正确的值。如果您需要直接从模型获取数据,那么您需要使用:
没有理由为此创建自定义模型。只需将所有数据存储在 DefaultTableModel 中即可。然后您可以从表的视图中删除“ID”列。
在这种情况下,数据不在视图中,因此您无法使用 table.getValueAt(...)。相反,您必须使用 table.getModel().getValueAt(...);
And that is exactly the way it should work. The data in the model should never change when you do a sort or reorder columns. Its only the view that changes.
If you want to get data from the table then you should be using:
and it will get you the proper value. If you need to get data from the model directly then you need to use:
There is no reason to create a custom model for this. Just store all the data in the DefaultTableModel. Then you can remove the "ID" column from the view of the table.
In this case the data in not in the view so you can't use table.getValueAt(...). Instead you must use table.getModel().getValueAt(...);
我认为您正在寻找
JTable.convertRowIndexToModel
,如我对 JTable 进行排序后,JTable 和 DefaultTableModel 的行索引失去同步另外,对于更复杂的排序和过滤,请尝试 http://publicobject.com/glazedlists/
I think you are looking for
JTable.convertRowIndexToModel
as outlined at JTable's and DefaultTableModel's row indexes lose their synchronization after I sort JTableAlso, for more complicated sorting and filtering, try http://publicobject.com/glazedlists/
在我看来,观察者模式是解决问题的一个好方法
http://en.wikipedia.org/wiki/Observer_pattern
你的类必须实现观察者接口通过单击列标题来跟踪用户生成的事件
in my opinion, а good solution of your problem is the observer pattern
http://en.wikipedia.org/wiki/Observer_pattern
your class has to implement observer interface to keep track of events that user generate by clicking on column title
为什么不使用 JXTable ?您可以在此处找到它
Why not using JXTable ? You can find it here