wood burning stoves*
The moose likes Swing / AWT / SWT and the fly likes What type of value is passed to a Table Model? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "What type of value is passed to a Table Model?" Watch "What type of value is passed to a Table Model?" New topic
Author

What type of value is passed to a Table Model?

Korben Dallas
Greenhorn

Joined: Feb 08, 2013
Posts: 5
Hi all,

I've just realized when data source (in this case an ArrayList) is passed to a TableModel, actually is passing a copy of that data and not a reference of the object. Could someone please explain me why is this??

Thanks, I'm a long time viewer of this forum. But this is my first time posting!
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2189
    
    7
I'm not sure why you say this. None of the default classes take an ArrayList as a parameter for a TableModel. So maybe your custom class does copy the contents of the ArrayList to some other data structure like a Vector.

Or maybe you have a custom TableModel that does use the ArrayList as the data structure to hold the data. In this case you can't just change an element in the ArrayList. You also need to invoke the fireTableCellUpated(....) method of the AbstractTableModel which in turn tells the JTable that data has changed and the cell will be repainted.
Korben Dallas
Greenhorn

Joined: Feb 08, 2013
Posts: 5
Rob Camick wrote:I'm not sure why you say this. None of the default classes take an ArrayList as a parameter for a TableModel. So maybe your custom class does copy the contents of the ArrayList to some other data structure like a Vector.

Or maybe you have a custom TableModel that does use the ArrayList as the data structure to hold the data. In this case you can't just change an element in the ArrayList. You also need to invoke the fireTableCellUpated(....) method of the AbstractTableModel which in turn tells the JTable that data has changed and the cell will be repainted.


This is what I've got so far...

Here the variable resultados receives a list of names from a MySql database. Then pass it to the custom class TableModelRegistroProveedores which extends AbstractTableModel, and load this model to the table.



This is my custom table model...




I add a table model listener to update my database once a value has changed in the table...



Now the thing is... when i fill the ArrayList resultados with the new data added to the database, why jtables values are the same? I mean when I pass resultados to my custom table model it's a reference I'm passing, so any change to any data in resultados should be visible on the table model data rigth?




Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Rob Camick wrote:Or maybe you have a custom TableModel that does use the ArrayList as the data structure to hold the data. In this case you can't just change an element in the ArrayList. You also need to invoke the fireTableCellUpated(....) method of the AbstractTableModel which in turn tells the JTable that data has changed and the cell will be repainted.


So if I understand your reply correctly, Korben, Rob Camick's guess is correct.

And so if you are just changing the data in the ArrayList (instead of calling the setValueAt method), then you have to tell the TableModel that you are changing the data. As Rob said, you do that by invoking the fireTableCellUpdated method. But really you're better off just calling the setValueAt method, which (correctly) does that for you.
Korben Dallas
Greenhorn

Joined: Feb 08, 2013
Posts: 5
But where should i call the setVauleAt method?? in the main class?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2189
    
    7
I mean when I pass resultados to my custom table model it's a reference I'm passing, so any change to any data in resultados should be visible on the table model data rigth?


Sort of. Yes the model should have access to the new data. However. as I told you above the table doesn't magically know when you changed the data in the model. The model has to tell the table that the data has changed. The model does this by invoking the appropriate fireXXX method from the AbstractTableModel.



I don't even know how that line of code works. "resultados" is a private variable in your custom TableModel so you can't just access the variable directly from code outside the TableModel.

What you want to do is create a custom method in your TableModel that you can invoke whenever you change the entire contents of the ArrayList. Something like:



However, I don't even understand why you need to do this. You update the database when the TableModel is changed. So the TableModel should already contain the updates. I'm not sure why you need to recreate the whole model when the contents of one cell changes.
Korben Dallas
Greenhorn

Joined: Feb 08, 2013
Posts: 5
Well tha variable resultados holds an ordered data source (from A to Z), this is obtained from a connection to the database which makes a query to obtain tha names in ascendant order...... let's say it holds tha names Andrew, Mike and Norm. This data is passed to the model, and shown up in the table. So far so good, but what happens if I change the name Andrew for Peter. The database is updated, and so the data source, but this time it holds Peter, Mike and Norm when it should be Mike, Norm and Peter.

Now, to achieve this I have filled again my variable resultados with the same query as before, and pass it again to my table model, and then use the setModel() method to show the new ordered data.

Is there a way i could do this without setting again the tablemodel?

Thanks,
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2189
    
    7
JTable supports sorting of data in a column. See the section from the Swing tutorial on Sorting and Filtering.
Korben Dallas
Greenhorn

Joined: Feb 08, 2013
Posts: 5
I got it now... thanks Rob!
 
 
subject: What type of value is passed to a Table Model?