使用自定义 AbstractTableModel 进行排序

发布于 2024-09-30 20:11:41 字数 929 浏览 1 评论 0原文

我创建了自己的类来扩展 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 技术交流群。

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

发布评论

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

评论(4

作妖 2024-10-07 20:11:41

问题是,当有人通过单击 JTable 中的列标题按列排序时,AbstractTableModel 中的列表不会更新

而这正是它应该工作的方式。当您对列进行排序或重新排序时,模型中的数据不应更改。只是观点发生了变化。

如果你想从表中获取数据,那么你应该使用:

table.getValueAt(row, column) 

它会给你正确的值。如果您需要直接从模型获取数据,那么您需要使用:

table.getModel().getValueAt(table.convertRowIndexToModel(row), column);

这个想法是能够存储隐藏的“id”列。

没有理由为此创建自定义模型。只需将所有数据存储在 DefaultTableModel 中即可。然后您可以从表的视图中删除“ID”列。

table.getColumnModel().removeColumn( table.getColumn("ID") );

在这种情况下,数据不在视图中,因此您无法使用 table.getValueAt(...)。相反,您必须使用 table.getModel().getValueAt(...);

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

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:

table.getValueAt(row, column) 

and it will get you the proper value. If you need to get data from the model directly then you need to use:

table.getModel().getValueAt(table.convertRowIndexToModel(row), column);

The idea is to be able to store a hidden "id" collumn.

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.

table.getColumnModel().removeColumn( table.getColumn("ID") );

In this case the data in not in the view so you can't use table.getValueAt(...). Instead you must use table.getModel().getValueAt(...);

魄砕の薆 2024-10-07 20:11:41

我认为您正在寻找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 JTable

Also, for more complicated sorting and filtering, try http://publicobject.com/glazedlists/

诗酒趁年少 2024-10-07 20:11:41

在我看来,观察者模式是解决问题的一个好方法
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

去了角落 2024-10-07 20:11:41

为什么不使用 JXTable ?您可以在此处找到它

Why not using JXTable ? You can find it here

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