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?
LVL 2
jstakkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gibu GeorgeChief Technology OfficerCommented:
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
0
CEHJCommented:
>>I'm calling the fireTableDataChanged() method on the table model.

That should be OK. What's the problem with doing that?
0
jstakkAuthor Commented:
>>>>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?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

CEHJCommented:
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
0
Thomas4019Commented:
Could you post your code for the model?
0
jstakkAuthor Commented:
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

0
CEHJCommented:
You should be OK with that - it should return the correct column count
0
Thomas4019Commented:
Fire this method when a large change occurs in your data (such as a new column)

fireTableStructureChanged()
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Thomas4019Commented:
So basically when you change a column you will need to change

fireTableDataChanged();

instead of

fireTableStructureChanged()

both are inherited as part of AbstractTableModel
0
Thomas4019Commented:
Oops got those backwards, fireTableStructureChanged() when you change a column.
0
jstakkAuthor Commented:
That did it... now it works. Thanks.

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

Yes, that's right
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.