java fireTableDataChanged 对 JTable 没有影响

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7210532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:05:30  来源:igfitidea点击:

fireTableDataChanged has no effect on JTable

javaswinguser-interfacejtable

提问by highfish87

I have a problem with updating my JTable in Java Swing.

我在 Java Swing 中更新 JTable 时遇到问题。

The datas I want to show changes a few times per second and I look for a efficient way to update the data in the JTable.

我想显示的数据每秒更改几次,我正在寻找一种有效的方法来更新 JTable 中的数据。

I used the method setModel() to update the data, and it works, BUT it has 2 drawbacks:

我使用了 setModel() 方法来更新数据,它可以工作,但它有两个缺点:

  1. If the user resize the table columns in the header, then he wil get about 10 exceptions (I think because the model is no longer available because it changes a few times per second)

  2. The information of the length of the resized column (in Pixel) get lost, every time the data (and so also the TableModel) changed.

  1. 如果用户调整标题中表格列的大小,那么他将得到大约 10 个异常(我认为是因为模型不再可用,因为它每秒更改几次)

  2. 每次数据(以及 TableModel)更改时,调整大小的列的长度信息(以像素为单位)都会丢失。

For the TableModel i use my own model ResultSetTableModel which extends AbstractTableModel. This ResultSetTableModel has a method setResultSet(ResultSet rs)and overwrites the method getValueAt(x,y)...

对于 TableModel,我使用我自己的模型 ResultSetTableModel,它扩展了 AbstractTableModel。这个 ResultSetTableModel 有一个方法setResultSet(ResultSet rs)并覆盖了该方法getValueAt(x,y)......

As I told if I set a new ResultSet to my ResultSetTableModel and then add it to the JTable by the method setModel(resultSetTableModel) it works, but it has the 2 drawbacks i told.

正如我所说,如果我将一个新的 ResultSet 设置为我的 ResultSetTableModel,然后通过 setModel(resultSetTableModel) 方法将其添加到 JTable 中,它可以工作,但它有我所说的 2 个缺点。

So I think I can solve this problem with the method fireTableDataChanged() but I tried many possibilities but get no change.

所以我想我可以用 fireTableDataChanged() 方法解决这个问题,但我尝试了很多可能性但没有改变。

Do you know, where I have to place the fireevent?

你知道我必须把fireevent放在哪里吗?

At the moment I try this, but it doesn't work and I don't know why:

目前我尝试这个,但它不起作用,我不知道为什么:

private ResultSetTableModel resultSetTableModel;
private DataFetcher dataFetcher;
private JTable table;

...

//works fine
public void initaialUpdateTable() {
    resultSetTableModel = new CachingResultSetTableModel(dataFetcher.getRS());
    table.setModel(resultSetTableModel);
}

//does not work
public void updateTable(){
    resultSetTableModel.setResultSet(dataFetcher.getRS());
    resultSetTableModel.fireTableDataChanged();
}

If I every times call initaialUpdateTable(), it works fine, but i want that just the data changes and not the whole model

如果我每次都调用initaialUpdateTable(),它工作正常,但我希望只更改数据而不是整个模型

Thanks for your answers

谢谢你的回答

Michael

迈克尔

回答by mKorbel

but i want that just the data changes and not the whole model

Hmm how can I..., there is no only one ...

嗯,我怎么能……,没有只有一个……

1) Something that you can see in the GUI is TableView, only presentation layer, and all data are always stored in the TableModel

1)在GUI中可以看到的是TableView,只有表示层,并且所有数据始终存储在TableModel

2) If you don't declare any TableModel, this doesn't mean that there isn't exist, still are there DefaultTableModel

2)如果你不声明 any TableModel,这并不意味着不存在,仍然存在DefaultTableModel

3) Your private ResultSetTableModel resultSetTableModel;must extend AbstractTableModel,

3)你private ResultSetTableModel resultSetTableModel;必须扩展AbstractTableModel

4) If you'll to block any of fireXxxXxxChanged();, then no changes goes back to the TableView,

4) 如果您要阻止任何fireXxxXxxChanged();, 则不会有任何更改返回到TableView,

5) Basic stuff here, start with fireTableCellUpdated(row, col);

5)这里的基本东西,从fireTableCellUpdated(row, col);

EDIT

编辑

More informations about TableModels here, hereor search for ResultSetTableModel, TableFromDatabase

关于 TableModels 的更多信息在这里在这里或搜索ResultSetTableModelTableFromDatabase

回答by Nate W.

Sorry I don't have a concrete answer to your question, but I couldn't quite fit all that I want to say in a comment.

抱歉,我对你的问题没有具体的答案,但我不能完全符合我想在评论中说的全部内容。

I used the method setModel() to update the data

我使用方法 setModel() 来更新数据

You should probably stick to a single model that provides methods to modify its data. These methods should appropriately notify listeners when something has changed.

您可能应该坚持使用提供修改其数据的方法的单一模型。当某些事情发生变化时,这些方法应该适当地通知侦听器。

Here's a really awesome article that shows how to implement a high-performance, multi-threaded table with frequently changing data. You could probably use a lot of the example source code.

这是一篇非常棒的文章,展示了如何使用频繁更改的数据实现高性能、多线程表。您可能会使用很多示例源代码。