线程和jtable [英] Threads and jtable
问题描述
我的jtable有问题。
I have a problem with jtable.
我有许多线程,每个线程都必须向jTable添加一行,但表格仍为空。我正在使用netbeans,图形完全独立于逻辑。有人可以帮帮我吗?
I have a number of threads and each of them have to add a row to the jTable, but the table remains empty. I'm working with netbeans, the graphics are totally separate from the logic. Can someone help me, please?
这是我用来添加行的代码
this is the code that i use for adding a row
MainGui.java
public void addToTable(String from, String to, int request, int response, String timeElapsed) {
Object [][] temp = new Object [data.length + 1][5];
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < 5; j++) {
temp[i][j] = data[i][j];
}
}
temp[data.length][0] = from;
temp[data.length][1] = to;
temp[data.length][2] = request;
temp[data.length][3] = response;
temp[data.length][4] = timeElapsed;
data = temp;
table.setModel(new DefaultTableModel(data, columnName));
}
MyThread.java
public void run() {
try {
MainGui mg = new MainGui();
mg.addtotable("null", "null", 0, 0, "null");
} catch (Exception e) {
}
推荐答案
Swing的第一条规则 - 不要从事件调度线程以外的任何线程修改UI
The first rule of Swing - Don't modify the UI from any Thread other then the Event Dispatching Thread
首先,确保使用 SwingUtilities#invokeLater
同步更新调用,或者更新是否对代码路径至关重要 SwingUtilities# invokeAndWait
First, make sure your updates calls are synchronized, using SwingUtilities#invokeLater
or if the update is critical to the code path SwingUtilities#invokeAndWait
向表模型添加行时,您负责触发通知视图模型已更改所需的更新事件。
When adding rows to the table model, you are responsible for firing the update events required to notify the view that the model has changed.
如果您使用的是 DefaultTableModel
,那么如果您使用的话,这将为您完成code> AbstractTableModel 或者你拥有 TableModel
然后你需要自己做。
If you're using a DefaultTableModel
then this will be done for you, if you're using an AbstractTableModel
or you own TableModel
then you will need to do this yourself.
最简单的方法是编写一个从 AbstractTableModel
扩展的实现,并调用 AbstractTableModel#fireTableRowsInserted(int,int)
>在你的add方法中,这将告诉 JTable
该模型已添加行并将导致表自动更新。
The easiest way is to write an implementation that extends from AbstractTableModel
and call AbstractTableModel#fireTableRowsInserted(int, int)
from within your add method, this will tell the JTable
that the model has added rows and will cause the table to update itself accordiningly.
不要被愚弄,使用 fireTableRowsInserted
通常比使用 fireTableDataChanged
更快将行添加到表模型时,因为整个表不需要重新绘制。如果您已经显着更改了表(添加/删除/更新了许多行),那么 fireTableDataChanged
将比单个插入/删除/更新的系列调用更快。
Don't be fooled, using fireTableRowsInserted
is generally faster then using fireTableDataChanged
when adding rows to a table model as the whole table doesn't need to be repainted. If you've significantly changed the table (added/removed/updated lots of rows), then fireTableDataChanged
would be quicker than a series individual insert/deleted/updated calls.
更新
因为看起来我可能没有做过自己明确。该模型负责解雇事件。这意味着,在你的add方法中,你应该调用 fireTableRowsInserted
。你应该避免从外部来源调用这些方法。
As it appears as I may not have made myself clear. The model is responsible for fire the events. That means, that within your add method, you should be calling fireTableRowsInserted
. You should refrain from calling these methods from an external source.
这篇关于线程和jtable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!