Adding rows to a JTable - java

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.

Related

Load many data to a Jtable efficient

I have a file and i read data from this and put them in a jTable. The problem is that when the file has many data (e.g 300.000 rows), my application needs a lot of memory (350MB). Is there any efficient way in order to load many rows in a JTable?
I created a Default Model and a Jtable like that:
DefaultTableModel model = new DefaultTableModel(array, colNames);
JTable data_table = new JTable();
data_table.setModel(model);
The array 'array' contains the data and the array 'colNames' the names of the Columns.
Use an embedded database, and store only the record ID in the model and then only if you need to filter/sort rows.
Absent enough information to offer a particular solution, you may be able to identify a reasonable partition function for your data's primary key, e.g. a String prefix or a Date range. Use an adjacent control to update the TableModel based on the selection. In this example, buttons are used to change a chart's data model. To minimize latency, Use SwingWorker. Aside from the memory problem, you may want to filter the table or load the file into an in-memory database such as H2 Database.
If you want to reduce the memory consuption of your program.
You could use java.nio.channels.FileChannel and map parts of the file to your main memory.
But you would have to reload/change current mapping, depending on your view.
Take a look at the javadoc of the map method.
I don' know what exactly your file is, but if you just use it as a simplistic replacement for an actual database. You're better off with using a real one.

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.

Auto-change TableModel based on Table view?

So I am working on a GUI that involves working with tables that can be sorted. I am noticing that when I sort a table, and I select a row from the newly sorted table, the selected row index of that row points to the data row from before the sort. I understand that this is because the view has changed, but the model has not; thus, you have the need for convertRowIndexToModel. It is also to my understanding that one can automatically update the view based on changes to the model by firing TableModelEvent's.
So here is my question: is it possible to automatically update the TableModel, based on changes to the view, so that I would not have to worry about converting the view index to table index?
All the default table code does this automatically so you don't need to worry about this. That is if you reference the model by using the table.getValueAt(...) and table.setValueAt(...) methods then you won't have a problem.
Only code that you write that tries to access the TableModel directly will have a problem. In this case thats what the convertXXX(...) methods are for.

Display a dynamically changing arrayList of objects with 4 string fields

I would assume that I should use a jTable. I tried this, but I can't for the life of me figure out how to append, insert and delete rows without a ton of overrides and complicated code. I find it hard to believe that Oracle doesn't have an easier way to do it.
Here's the premise. I have a few arrayLists. Each contain n amount of items and I want to be able to add these items' properties in the form of strings to the jtable and once i surpass a certain number of rows, I want the jTable to scroll.
So that's the reason I need to be able to add and remove rows.
As discussed in How to Use Tables: Creating a Table Model, DefaultTableModel has convenient methods to add, insert and remove rows. Simply update your model using any of these methods and your view will be updated accordingly.
Addendum: There's an example here.
Take a look at GlazedLists. It makes working with dynamically changing data and sowing it in JTables/JLists/JTrees, etc, very simple.

JTable - fire row changed when row deleted

In my system we use JTable with data model.
when data changes we remove it from the model, iterate the model and fire for each row listElementPropertyChanged (I think its intellij's). In this way removed lines are not deleted cause they are not in the model.
How do I refresh the whole table according to the model?
Simply use fireTableDataChanged(). This way, all listeners will now that all data may have changed.
However, use it with care, as usual behaviour for listeners will be to refresh the whole table.
You would have better using fireTableRowsDeleted(int, int) with the removed rows indexes.

Categories