Results 1 to 7 of 7
Thread: TableModelListener
- 03-12-2010, 10:04 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 2
- Rep Power
- 0
TableModelListener
How do I exactly use TableModelListener? I think I misunderstood something I I can't figure out what I'm doing wrong.
When is the table event issued? Are the fireTable* methods fired automatically?
Very simple example of my code (one of many):
Java Code:public class MyTableModel extends AbstractTableModel implements TableModel, TableModelListener{ public MyTableModel() { this.addTableModelListener(this); } public int getRowCount() { return 5; } public int getColumnCount() { return 3; } @Override public boolean isCellEditable(int rowIndex, int columnIndex) { return true; } public Object getValueAt(int rowIndex, int columnIndex) { return "value"; } public void tableChanged(TableModelEvent e) { System.out.println("tChanged"); } }
- 03-12-2010, 04:46 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
The TableModel is responsible for invoking the fireXXX methods. If you want to see how this is done then look at the source code for the DefaultTableModel. The source code comes with the JDK in a src.zip file.When is the table event issued? Are the fireTable* methods fired automatically?
Generally you can use the DefaultTableModel until you are more familiar with how the table and model work together.
For an example of using a TableModelListener see: Swing - JTable cell editing
- 03-13-2010, 03:49 AM #3
Typically, you implement the TableModel, providing methods to update the data. These methods should call the fireTable* methods whenever the data changes. Note that you can use the most simple version of these methods all the time in most cases.
- 03-13-2010, 01:15 PM #4
Member
- Join Date
- Mar 2010
- Posts
- 2
- Rep Power
- 0
I see, I somehow forget about DefaultTableModel. Shame on me.
Still, I am not sure how to efficiently use TableModel without the need of duplicating of user data. Storing them inside of TableModel seems quiet wrong and having TableModel inside of data object is also strange in certain applications.
-
- 03-13-2010, 04:41 PM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Why do you think you have to duplicate user data? The DefaultTableModel stores its data in a Vector of Vectors. So if you initially load your data (where ever it comes from) into the Vectors then you are not duplicating data.Still, I am not sure how to efficiently use TableModel without the need of duplicating of user data
What is your specific requirement?
- 03-15-2010, 08:52 PM #7
Actually, duplication of data may be the "right" solution. The reason is that view components, like a table, must hold their own data. Each component implements its own MVC model. However, there is no reason you have to base your model on the models of each component. Sometimes, it's better to implement the component model separately and to let the controller shuttle data between the view component and your model.
In other words, your data model holds the "real" data. The controller copies some of the data to the view component for display. Later, the controller can copy data from the view component back to the data model, which should validate the changes and either persist them or issue error messages.
You can build the view component model around the actual data model, so that there is no duplication. However, for larger applications, this approach can become very difficult to work with.


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks