JTable missing .addRow()? - java

I have been wondering why a jTable hasn't got an .addRow() method by default. Why do you have to set a Model before this is possible?
JTable table = new JTable();
table.addRow();
The above is not possible, however:
JTable table2 = new JTable();
table2.setModel(new DefaultTableModel());
table2.addRow(...);
After setting the new model, it IS possible - why?

First of all, by default, the TableModel is not mutable (other then being able to, potentially, modify the existing data), that is, there are no methods within TableModel that provide any means to add or delete rows.
It is up to implementations of TableModel to decide if that functionality is possible. Take a look at TableModel for details about what the default interface provides
Secondly, it is the responsibility of the model to manage the data. It makes no sense for the table to suddenly provide add/delete functionality, when that functionality may or may not exist. Modifications to the data should be done directly via the model - IMHO
Thirdly, there is no JTable#addRow method

Related

How to display data in a jTable using linked lists

I have a table from my SQL server and I have connected to and and stored all the data in a linked list. I now want to take that data from the linked list created (everything works fine) but when I try to store it in my table named customerTable it doesn't recognize my table. (My GUI is on a different class, so is my linked list and my customer object that I use to initialize get, set methods, so I think it has something to do with that)
Can anybody help me understand how I do this?
I use this code:
DefaultTableModel model = (DefaultTableModel) customerTable.getModel();
You should implement interface TableModel, to use your existing list as the back of the table model.
See How to Use Tables of the Java Tutorials, on creating table models.
so is my linked list and my customer object that I use to initialize get, set methods
If you are storing the data in a custom Object, then you need to create a custom TableModel for that Object.
Check out Row Table Model for a step-by-step example on how to create this custom TableModel for your object.

What are the benefits of using a ResultSetTableModel for a JTable?

I am writing a desktop application in Java and it uses two JTables as the main output.
I have a working version of the program where I use DefaultTableModels to manage the underlying data of the JTables and I use an ArrayList to store the data in each model.
However, I've come across the ResultSetTableModel which stores the data in a ResultSet and a ResultSetMetaData object to implement the TableModel methods.
But I am not clear on the benefits of using a ResultSetTableModel as opposed to a DefaultTableModel or AbstractTableModel. I have searched and I cannot find any discussion on this.
Does anyone know why one would favor a ResultSetTableModel over the other options?
if you use DefaultTableModel you have to store the data into model manually, and if you ResultSetTableModel data automatically loads into JTable this is the difference.

How to time refresh of dynamic JTable that use data from mysql data base

Hello every one I m lost with this problem :
I have a Jtable and I fill data from mysql database ,
but I want the table to be refresh by a timer every
xx seconds. Really i don't know how to do ?
I would appreciate some direction/help.
Use a javax.swing.Timer, with setRepeats(true) so that it repeatedly fires an ActionEvent. In your ActionListener, run the query against the database. The easiest approach is to throw away your old table model, generate a new one from the new database results, and call setTableModel on your JTable. (A more sophisticated approach is to work out the differences to the previous query results and only update those table cells that have changed, but you may not need this refinement.)
So far, I talked about an ActionListener doing the work of regenerating the table model, but a more elegant solution might be to have a custom table model that knows how to update its own data from the database - and will do so, for example, on invocation of (say) an updateFromDatabase() method. So the TableModel has the responsibility of updating itself and the Timer/ActionListener has the responsibility of deciding when these updates should happen.
As already mentioned by Kalathoki, changes to the model layer are not seen in the JTable view component unless the model layer notifies the JTable that the model has changed. If you're calling setTableModel on JTable, then the JTable knows that the model has changed, but if you have a custom table model that (from the point of view of the JTable) 'secretly' refreshes itself from the database, then you need to make sure that the JTable is notified by calling the fireTableDataChanged() method.
Another consideration is how long the database refresh takes. You would not want your UI to freeze up for long periods while the JTable refreshes, so a further refinement would be to use a SwingWorker so that the database query is run in a background thread. In this case the Swing timer would be used to initiate the SwingWorker, and the SwingWorker would use its done() method to update the table model and fire an event to the JTable view component.
Use fireTableDataChanged() Method. Click Here fore more details

Why do we have to use a TableModel for a JTable?

If I want to use a JTable in Java it seems to me for adding rows and doing alters from behind a button or so I always have to use a TableModel (this could be the default one or one created by your own) But my question is: Why do we have to use this. I can't find this in any of the posts I saw. Can someone explain how this works and why it is necessary? And why we can't just add rows to the JTable without a model.
It seems to me that if you want to just show a few records but at creation you don't know all the rows yet it would be easier to just do something like a table.add() to add the row.
You can create the table with data inside without a model attached to it. So why not add data?
Or am I just wrong and can you add also data without a model?
The TableModel interface defines the minimum methods needed by a JTable (view) to render its content (model). AbstractTableModel is an abstract implementation that provides the event plumbing and leaves just three methods that must be overridden. DefaultTableModel goes on to include an internal data model based on Vector and convenient methods to alter that internal model. See Creating a Table Model for a comparison and these contrasting examples.

Adding rows to a JTable

We have a simple project where we read data from a socket and we want to populate a table with the coming data, but we can't find a way to add rows to a yet created JTable object, we can only find how to add rows at creation time of the table.
Is it possible to add rows dynamically to a JTable, or there is a better alternative object to deal with this way of showing data?
EDIT: Thanks a lot for your answers.
All three of them look very promising, but I have to choose only one and I think the best is Guillaume's.
You should create a custom TableModel. A JTable doesn't actually store the rows, it always delegates that to a TableModel. To help you implementing it, you should make use of AbstractTableModel. Don't forget to call fireTableRowsInserted() every time you add rows. For better performances if you add a lot of rows, try to batch the updates and add many rows at a time.
If you use the default table model for a JTable then you can add rows with following code
if ( dest+1 < table.getRowCount()-1 )
( (DefaultTableModel) table.getModel() ).insertRow(dest+1, getValuesForNewRow());
else
( (DefaultTableModel) table.getModel() ).addRow(getValuesForNewRow());
Once you start dynamically adding and removing elements from a JTable, you really need to start using a TableModel.
See the Java Tutorial for more details.

Categories