I'm trying to run an app that shows the result of the 4 elapsed time of 4 cars in a JTable
.
So I'm looking for update the cell of the faster car first meanwhile, the other cars are arriving by the time by a sleep
ing via threads.
Each thread run the result of the elapsed time for each car, but my problem is when I'm trying to add the result to a JTable
.
I'm coding through a extended AbstractTableModel
, implemented in my JTable
of my main class.
I tried to create a Object[][]
to add the data indexed of every single elapsed time. But when leave it blank or adding some default code, this data is not showed in my JTable
.
public void run() {
double abs = 0; // absolute value of lap
int rowCount = 0;
// Loop for laps (Array{1,2,3,4})
for(int lap = 1; lap < laps.length+1; lap++){
Thread.sleep((int)(this.car.getElapsedTime() * 1000)); // Sleep method(MILLISECONDS) for Car EnlapsedTime
abs = abs + this.car.getElapsedTime(); // Absolute time sum
newModel.setValueAt(abs, rowCount, this.car.getIdCars());
rowCount = rowCount + 1;
newModel.fireTableCellUpdated(rowCount, this.car.getIdCars());
}
class DynamicTable extends AbstractTableModel {
Car LotusElise = new Car(1, "Lotus Elise", 189, 901);
Car FerrariCalifornia = new Car(2, "Ferrari California", 483, 1735);
Car LamborguiniAventador = new Car(3, "Lamborguini Aventador", 691, 1731);
Car MaseratiMCStradale = new Car(4, "Maserati MC Stradale", 450, 1931);
private String[] columnNames = {LotusElise.getName(), FerrariCalifornia.getName(), LamborguiniAventador.getName(),
MaseratiMCStradale.getName()};
private Object[][] data = {
// adding this code crash the program by the same Exception - this is an example of one lap
// {LotusElise.getIdCars(), LotusElise.getElapsedTime()},
// {FerrariCalifornia.getIdCars(), FerrariCalifornia.getElapsedTime()},
// {LamborguiniAventador.getIdCars(), LamborguiniAventador.getElapsedTime()},
// {MaseratiMCStradale.getIdCars(), MaseratiMCStradale.getElapsedTime()}
};
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
Under this part of code, I'm running throw ArrayIndexOutOfBoundsException: 0
which 0 = rowCount
. I understand that I don't have any rows from the beginning, but I don't know how add one to this. Probably with Vector<Double>
?
In this point, I'm having trouble Overriding the int get value at(int row, int col)
to return indexing from the Vector<Double>
, because with Object[][]
returns [col][row]
.