我有一个类,OutputTable,它负责显示一个包含先前计算结果的表(jtable)。结果在另一个类(平滑类)中计算,然后将结果作为参数发送到 OutputTable 类。
我需要计算数据两次,对于他们两次,我都需要显示一个带有结果的 jtable。计算结果时没有多线程。
由于我需要显示 2 个不同的表,并且一旦计算出一个数据,我就想显示该表,因此我决定为每个表创建一个新线程。所以我在第一个数据处理完成后立即启动第一个线程,当第二轮数据处理完成后我启动第二个线程。
两个要处理的数据都在不同的数据结构中,一个是一个ArrayList<Station>
而另一个是一个TreeMap<Integer, ArrayList<Station>>
问题是只有在第二次数据处理完成时才会填充表(进程再次空闲),这导致我得出结论,线程有问题。当第一个线程启动时,它只显示窗口布局,内部没有其他内容。当第二个线程启动时,两个表都填充了结果。
我正在使用 GUI,当用户按下开始按钮时,它会开始数据处理。GUI 与
javax.swing.JFrame 实现 ActionListener、ItemListener
所以我的代码是:
public class OutputTable extends JFrame implements Runnable{
TreeMap<Integer, ArrayList<Station>> map;
ArrayList<Station> arrayStation;
public OutputTable(TreeMap<Integer, ArrayList<Station>> map, ArrayList<Station> arrayStation) {
this.map = map;
this.arrayStation = arrayStation;
}
public void run()
{
DefaultTableModel model = new DefaultTableModel() {
String[] columnsName = { /* my column names go here*/ };
@Override
public int getColumnCount() {
return columnsName.length;
}
@Override
public String getColumnName(int index) {
return columnsName[index];
}
};
JTable table = new JTable(model);
add(new JScrollPane(table));
setSize(1300, 700);
setDefaultCloseOperation(HIDE_ON_CLOSE);
setVisible(true);
if(map != null)
{
for (ArrayList<Station> arrayAux : map.values())
{
for(int a = 0; a<arrayAux.size(); a++)
{
model.addRow(new Object[] { /* here I populate the table with my get methods*/ });
}
}
}
if(arrayStation != null)
{
for(int a = 0; a<arrayStation.size(); a++)
{
model.addRow(new Object[] { /* here I populate the table with my get methods*/ });
}
}
}
}
这是来自我启动线程的 GUI 代码
/* (where I start processing the data for the first time) */
Runnable r = new OutputTable(null, processme);
new Thread(r).start();
/* (I start processing data for a second time) */
Runnable r2 = new OutputTable(xpto, null);
new Thread(r2).start();
编辑:
如果我不清楚,我假装是在创建 jtable 后立即显示 jtable 中的数据,而不是在所有处理结束时,因为由于某种我不明白的原因,它现在正在发生。