I've a class, OutputTable, that is responsible to show a table (jtable) with previously computed results. The results are computed in another class (Class Smooth) and after the results are sent as a parameter to the OutputTable class.
I need to compute data two times and for both of them I need to show a jtable with the results. There is no multi-threading while computing the results.
As I need to show 2 different tables and as soon as one data is computed I want to show the table I decided to create a new thread for each table. So I start the first thread as soon as the first data processing is done and when the second round for data processing is done I start the second thread.
Both data to be processed are in different data structures, one is a ArrayList<Station>
while the other is an TreeMap<Integer, ArrayList<Station>>
The problem is that the tables are only populated when the second data processing is done (so is the process free again) which leads me to conclude that there is something wrong with the threads. When the first thread starts it just shows the window layout and nothing else inside. When the second thread starts both tables get populated with results.
I'm using a GUI and when the user presses the start buttom it starts the data processing. The GUI is with
javax.swing.JFrame implements ActionListener, ItemListener
So my code is:
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*/ });
}
}
}
}
And this is from the GUI code where I start the threads
/* (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();
EDIT:
If I wasn't clear, what I pretend is to immediately show the data in the jtable as soon as the jtable is created and not at the end of all processing as it is happening now for some reason that I don't understand.