Recreate Jtable or remove all rows and add new rows? - java

I have a JTable and a DefaultTableModel, I want to delete all the rows of JTable every 3 secondes and add new rows to the JTable. Considering the performance, What's the best choice to do that, recreate Jtable or remove all rows and add rows?

What's the best choice to do that, renew Jtable or remove all rows and addrows?
If you recreate the JTable, all the renderers and editors will be recreated for the table.
If you change the TableModel, then the table will need to recreate the TableColumnModel, and all the TableColumns based on the new TableModel.
There will be many more objects that need to be created to support the JTable/TableModel relationship.
Removing rows and adding rows, will just cause the table to repaint the new data using all the current renderers. I would say this is more efficient. And instead of adding rows one at a time, you should add all the rows at once

You can create a tablemodel that digs into your data object. As soon as you change the underlying data object, you call fireTableDataChanged().
From the JavaDoc for AbstractTableModel#fireTableDataChanged():
Notifies all listeners that all cell values in the table's rows may have changed. The number of rows may also have changed and the JTable should redraw the table from scratch. The structure of the table (as in the order of the columns) is assumed to be the same.

Related

JTable clone / not the model

I have a JTable and delete some columns of it. Once I deleted the columns, I need to copy this JTable.
I can not copy the model, because the model still include the deleted columns. So how can I copy the "Visible Model"?
I do not really need to copy the graphical Swing-Component of a JTable, I just need the "Visible Model" of it.
TableColumn col = jtable.getColumnModel().getColumn(0);
jtable.getColumnModel().removeColumn(col);
jtable.setAutoCreateColumnsFromModel(false);
jtable.getmodel();
the thing is you have to call jtable.setAutoCreateColumnsFromModel(false);
other wise you'll get a new column again after deleting the column. try this
way. then you can pass the model the it wont contain the removed column.note that its a property in the jtable so calling the method setAutoCreateColumnsFromModel(false) one time is enough.
OMG, it´s so simple. You need to place also the ColumnModel in the new JTable:
JTable jtb = new JTable(ptable.getModel(), ptable.getColumnModel());

Javax Swing JTable::getModel vs JTable::getColumnModel

Hello I have been working with javax swing and I came across a weird problem and got me questioning.
I can for example:
JTable table = new JTable();
// Indeed, 2 different objects:
// The TableModel (which, i think is supposed to contain rows and columns?
DefaultTableModel dtm = (DefaultTableModel) table.getModel();
// And the column model, supposed to define the columns of a table?
TableColumnModel tcm = table.getColumnModel();
// I can add columns to my table in two different manners
dtm.addColumn("A Column");
// or
TableColumn column = new TableColumn();
column.setHeaderValue("Another column");
column.setWidth(120);
column.setMinWidth(120);
column.setMaxWidth(120);
tcm.addColumn(column);
// And notice that both commands will add a column in the table
// So our table model should now have 2 columns.
// But does it?
System.out.println(dtm.getColumnCount()); // outputs 1;
System.out.println(tcm.getColumnCount()); // outputs 2;
System.out.println(table.getColumnCount()); // outputs 2;
// The visual shows 2 columns, but the model has only 1.
From that I can tell JTable uses tableColumnModel and tableColumnModel gets all the columns added into tableModel, but, when I add a column to the TableModel it gets added to the table, but the tableModel remains outdated.
Now, the problem is: it's really interesting to add a column via columnModel because I can define the sizes, layout, editable options there, but in this way I cannot add any data to it from the tableModel, since that column doesn't appear on the tableModel.
Any thoughts on this?
The TableModel is used to contain data. The data is accessible in row/columns.
The TableColumnModel is used by JTable to control the View of the data. That is it controls the columns that are displayed in the JTable. You can also reorder the columns to display the data in a different order.
...but in this way I cannot add any data to it from the tableModel, since that column doesn't appear on the tableModel
That is correct. The purpose of the TableColumnModel is to simply customize the view, not manipulate data.
Maybe you have an application that contains many columns of data, but access to specific columns is limited by "security level". In this case the data is always stored in the TableModel, but you need to change the view to control which columns of data are visible. So you can remove/add columns from the TableColumnModel.
When you add a column to the TableModel, the JTable gets notified and it recreates all the TableColumns for you. This can be a good or bad thing because when the TableColumnModel is recreated you lose any custom renderers and editor that you may have added to a TableColumn. You can prevent this from happening buy using:
table.setAutoCreateColumnsFromModel( false );
Now the TableColumnModel will not be updated and it is your responsibility to manually create and add the TableColumn to the TableColumnModel.
But in general you:
add/change data through the TableModel.
change the view through the TableColumnModel.

Which editing and removing row method is better for JTable?

I wanna ask that which method to edit a JTable is better than the other?
use setValueAt() function.
Make a new model of JTable and feed its object to JTable.
And also, how to remove a Row from JTable without using method 2 described above?
You would use the setValueAt method when you're changing one value at a time.
You would make a new TableModel when you're changing 25% or more of the rows at one time.
If you use a DefaultTableModel as your TableModel, you can use the addRow or setValueAt methods to add a row and change a row value. You can use the removeRow method to remove a row.
Since the TableModel is associated with the JTable, any changes to the TableModel are reflected in the JTable. Any changes to the JTable by the user are reflected in the TableModel.

Removing unused Rows from jTable

How do I remove unused rows from a JTable?
I first create a table of a fixed size in a JPanel and then fill elements as needed.
Now I don't want unused rows to be displayed in my table. Please help.
I presently get a table with lots of empty rows in bottom, only top 5-6 rows being used, with rest blank. I want to hide them or remove them somehow
Work the other way around. Start with an empty DefaultTableModel. DefaultTableModel supports an addRow() method. So, as you get data to add to the model use:
model.addRow(...);
Don't let the GUI editor determine how you code your GUI. Adding cleanup code to remove rows is a bad design.
DefaultTableModel has a method removeRow which removes a row from the table. Pass the row index which you want to remove from the table.
Now I don't want unused rows to be displayed in my table.
This I am not sure please post an SSCCE to show us what is the flag that says these rows are not used.

Moving JTable rows downwards when new row is added to the table

I have to maintain list of events generated by the user in a JTable.The JTable consists of 2 columns Time and event and 5 rows. When 1st event is generated it's data must be placed in 1st row of JTable, when 2nd event is generated the data in 1st row must be moved to 2nd row and the data of 2nd event must be place in the 1st row of the JTabel and so on up to 5th row. There is no limit to the events(n events can be generated). But i have to display only 5 events in JTable and older once will be stored in text file. I have tried some logics but they are failed to work.
This is my problem. Can any one give me an idea how to move rows downwards...
Regards...
Upendra.S
You simply add your new row at index 0 in your TableModel. If your TableModel extends from DefaultTableModel, you can even use the available insertRow method.
For the requirement to only show 5 rows, you simple remove the extra row from your TableModel.
Consult the JTable tutorial for more information
have look at JTable tutorial
part TableModel
method from DefaultTableModel insertRow()

Categories

Resources