【发布时间】:2013-08-28 21:11:06
【问题描述】:
我有一个类,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 中的数据,而不是在所有处理结束时,因为它现在正在发生,我不知道的原因明白。
【问题讨论】:
标签: java multithreading swing concurrency