jstakk
asked on
How to update JTable when data changes
I'm working on a JTable using a custom table model. The data source for the table model will sometimes change number of columns. If number of rows change, it will be updated, but not columns. I'm calling the fireTableDataChanged() method on the table model.
How do I update number of columns on the JTable?
How do I update number of columns on the JTable?
You need to use java.swing.table.TableColu mnModel to add columns when ever there is a change also you need to set autoCreateColumnsFromModel this field to true in the JTable
>>I'm calling the fireTableDataChanged() method on the table model.
That should be OK. What's the problem with doing that?
That should be OK. What's the problem with doing that?
ASKER
>>>>I'm calling the fireTableDataChanged() method on the table model.
>>That should be OK. What's the problem with doing that?
I don't know if there is something wrong with that. My problem is how to update the table to show the new colomns or remove the ones missing.
Could someone explain how I use TableColumnModel?
>>That should be OK. What's the problem with doing that?
I don't know if there is something wrong with that. My problem is how to update the table to show the new colomns or remove the ones missing.
Could someone explain how I use TableColumnModel?
It would happen automatically if you were using an existing model, such DefaultTableModel. Do you really need your own TableModel?
If so, you need to alter the behaviour of http://java.sun.com/javase/6/docs/api/javax/swing/table/TableModel.html#getColumnCount() and any other column-oriented methods accordingly
If so, you need to alter the behaviour of http://java.sun.com/javase/6/docs/api/javax/swing/table/TableModel.html#getColumnCount() and any other column-oriented methods accordingly
Could you post your code for the model?
ASKER
Here is my code.
Thanks for helping me.
Thanks for helping me.
public class MyTableModel extends AbstractTableModel implements Observer
{
private static final long serialVersionUID = -5926837260794060766L;
private CustomData data;
public MyTableModel( CustomData data ) {
this.data = data;
data.addObserver( this );
}
@Override
public int getColumnCount() {
double[][] vector = data.getVector();
return vector[0].length;
}
@Override
public int getRowCount() {
return data.getVector().length;
}
@Override
public Object getValueAt( int row, int column ) {
double[][] vector = data.getVector();
if (vector != null) return vector[row][column];
else return 0.0;
}
@Override
public void update( Observable o, Object arg ) {
fireTableDataChanged();
}
}
You should be OK with that - it should return the correct column count
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
So basically when you change a column you will need to change
fireTableDataChanged();
instead of
fireTableStructureChanged( )
both are inherited as part of AbstractTableModel
fireTableDataChanged();
instead of
fireTableStructureChanged(
both are inherited as part of AbstractTableModel
Oops got those backwards, fireTableStructureChanged( ) when you change a column.
ASKER
That did it... now it works. Thanks.
>>Oops got those backwards, fireTableStructureChanged( ) when you change a column.
Yes, that's right
Yes, that's right