I'm having problem properly using JTable. This is run through a thread and I want the JTable to be able to update its values for each tick. The values (from physics) are updated at each tick. I have tried using
SpecialTable.repaint();
SpecialTable.revalidate();
which apparently does nothing. I have also tried SpecialTable.fireTableDataChanged() suggested in other thread: JTable How to refresh table model after insert delete or update the data.. This also does nothing since fireTableDataChanged(); is undefined for JTable. If I try casting it:
((AbstractTableModel) SpecialTable).fireTableDataChanged()
I get a compiler errror: Unresolved compilation problem: Cannot cast from JTable to AbstractTableModel
I have also tried reinitializing JTable every tick of the simluation by creating a remakeTable-method for my JPanel-class and calling that one every tick but it doesn't do anything either.
class SpecialTable extends AbstractTableModel {
Model physics;
Object[][] partikelinfo = new Object[10][4];
String[] columns = new String[4];
SpecialTable(Model m){
this.physics = m;
LinkedList<Particle> partiklar = physics.getParticles();
String[] tmp = { "Particle no.", "Coordinates", "Moving",
"Tracking" };
for (int i = 0; i < tmp.length; i++) {
Arrays.fill(columns, i, i+1, tmp[i]);
}
Object[][] tmp2 = {
{ "1", partiklar.get(1).y + " , " + partiklar.get(1).x,
partiklar.get(1).isMoving, new Boolean(true) },
{ "2", partiklar.get(2).y + " , " + partiklar.get(2).x,
partiklar.get(2).isMoving, new Boolean(true) },
{ "3", partiklar.get(3).y + " , " + partiklar.get(3).x,
partiklar.get(3).isMoving, new Boolean(true) },
{ "4", partiklar.get(4).y + " , " + partiklar.get(4).x,
partiklar.get(4).isMoving, new Boolean(true) },
{ "5", partiklar.get(5).y + " , " + partiklar.get(5).x,
partiklar.get(5).isMoving, new Boolean(true) },
{ "6", partiklar.get(6).y + " , " + partiklar.get(6).x,
partiklar.get(6).isMoving, new Boolean(true) },
{ "7", partiklar.get(7).y + " , " + partiklar.get(7).x,
partiklar.get(7).isMoving, new Boolean(true) },
{ "8", partiklar.get(8).y + " , " + partiklar.get(8).x,
partiklar.get(8).isMoving, new Boolean(true) },
{ "9", partiklar.get(9).y + " , " + partiklar.get(9).x,
partiklar.get(9).isMoving, new Boolean(true) },
{ "10", partiklar.get(10).y + " , " + partiklar.get(10).x,
partiklar.get(10).isMoving, new Boolean(true) }
};
for (int i = 0; i < tmp2.length; i++) {
Arrays.fill(partikelinfo, i, i+1, tmp2[i]);
}
}
public int getColumnCount() {
return columns.length;
}
public int getRowCount() {
return partikelinfo.length;
}
public String getColumnName(int col) {
return columns[col];
}
public Object getValueAt(int row, int col) {
return partikelinfo[row][col];
}
public Class<? extends Object> getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public void setValueAt(Object value, int row, int col) {
partikelinfo[row][col] = value;
fireTableCellUpdated(row, col);
}
public boolean isCellEditable(int row, int col) {
if (col < 3) {
return false;
} else {
return true;
}
}
//Don't know how to call this || if it works.
public void updateValues(){
fireTableDataChanged();
}
}
I don't know whether the problem lies with SpecialTable not getting the updated values or whether they just don't display or if there's a third alternative.