Link to home
Start Free TrialLog in
Avatar of jstakk
jstakkFlag for Norway

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?
Avatar of Gibu George
Gibu George
Flag of India image

You need to use java.swing.table.TableColumnModel to  add columns when ever there is a change also you need to set autoCreateColumnsFromModel  this field to true in the JTable
Avatar of CEHJ
>>I'm calling the fireTableDataChanged() method on the table model.

That should be OK. What's the problem with doing that?
Avatar of jstakk

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?
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
Could you post your code for the model?
Avatar of jstakk

ASKER

Here is my code.

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();
	}
 
}

Open in new window

You should be OK with that - it should return the correct column count
ASKER CERTIFIED SOLUTION
Avatar of Thomas4019
Thomas4019
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So basically when you change a column you will need to change

fireTableDataChanged();

instead of

fireTableStructureChanged()

both are inherited as part of AbstractTableModel
Oops got those backwards, fireTableStructureChanged() when you change a column.
Avatar of jstakk

ASKER

That did it... now it works. Thanks.

>>Oops got those backwards, fireTableStructureChanged() when you change a column.

Yes, that's right