以前に計算された結果を含むテーブル (jtable) を表示するクラス OutputTable があります。結果は別のクラス (Class Smooth) で計算され、結果がパラメーターとして OutputTable クラスに送信された後に計算されます。
データを 2 回計算する必要があり、その両方について結果を jtable に表示する必要があります。結果の計算中にマルチスレッドはありません。
2 つの異なるテーブルを表示する必要があり、1 つのデータが計算されたらすぐにテーブルを表示したいので、テーブルごとに新しいスレッドを作成することにしました。そのため、最初のデータ処理が完了するとすぐに最初のスレッドを開始し、データ処理の 2 回目のラウンドが完了したら、2 番目のスレッドを開始します。
処理される両方のデータは異なるデータ構造にあります。一方は aArrayList<Station>
で、もう一方は a です。TreeMap<Integer, ArrayList<Station>>
問題は、2 番目のデータ処理が完了したときにのみテーブルに値が入力されることです (したがって、プロセスは再び解放されます)。これにより、スレッドに問題があると結論付けることができます。最初のスレッドが開始すると、ウィンドウのレイアウトだけが表示され、内部には何も表示されません。2 番目のスレッドが開始されると、両方のテーブルに結果が入力されます。
私は 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 にデータを表示することであり、すべての処理の最後ではなく、何らかの理由で現在起こっているためです。