tags:

views:

1849

answers:

3

Whats the relationship between a JTable, TableModel and TableData. If i just modify the TableData, does that also change the data display on the JTable component or i have to call some method to sync the two. I have looked at this, but it does not explicitly tell or show me the relationship in terms of updates

+2  A: 

When you change a value in the TableModel data, you must fire this event. eg. changing the value of one cell, you call fireTableCellUpdated(row, col);.

Look at this more specifically.

Jérôme
fireTableCellUpdated(row, col) only applies to TableModels which are subclasses of the AbstractTableModel.
Peter Walser
+1  A: 

I presume you're using the phrase "TableData" to just mean the data being shown in the JTable. To answer your question, whether you see automatic updates or not depends upon your TableModel and how it interacts with your data.

A TableModel should fire events when the underlying data model has changed, this in turn will notify the JTable that a change has occurred and it should be redrawn. So in terms of a relationship, the JTable listens to event changes on the TableModel; the TableModel has no knowledge of the JTable.

Depending on how your model is organised, the data underneath it could change without the TableModel knowing. In this instance, if you have a direct passthrough from the TableModel to the data, the values onscreen will change when a repaint naturally occurs (screen resize, mouse moving over etc.), but it you want to force the event you should get the TableModel to notify the JTable through the aforementioned events. In terms of relationship, the TableModel knows about the table data, but the table data has no knowledge of model (usually). The TableModel may or may not be listening on the data for changes.

MrWiggles
+1  A: 

When setting the TableModel in a JTable, the table adds an observer (TableModelListener) to get informed about changes in the model.

When changing data in the model, the model is expected to notify the registered listeners by firing a TableModelEvent. The event itself can contain supplemental information about the granularity of the change (whether the structure significantly changed, or only some cell data is changed) to allow the JTable to do an optimized view update.

Peter Walser