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.
Related
I didn't write any code yet, so sorry about that but need some direction and clarification before proceeding. Can I create a dynamic link between with a JTable and Hashmap? So when ever my listenrs add something new or delete something, it will update the Hashmap and that would update the JTable, I was thinking of re-creating the JTable everytime a change happens? That is one of my buttons being pressed.
Any suggestions?
A JTable is just the visible component, the data is contained in a TableModel. Any changes inside the model will be reflected in the JTable itself. So you can create a TableModel that uses a HashMap internally.
Here's Oracle's table tutorial for more info
https://docs.oracle.com/javase/tutorial/uiswing/components/table.html
I am displaying array list in JTable.
I have a very simple removing method:
#Override
public void DeleteContact(int id) {
Database c = GetSingleContact(id);
contactsList.remove(c);
Then it is saved to a file.
Everything works correctly but when I remove one element, it leaves empty space filled with 0 in JTable, I cannot get rid of it, size calculation methods still count it as element, and other elements are not renumbered (technically arraylist.remove(x) should do it automatically).
(technically arraylist.remove(x) should do it automatically).
No it should not.
JTable uses a TableModel to hold the data. Any changes to the data need to by done on the TableModel. The TableModel will then notify the table to repaint itself.
If the ArrayList is used as the data store for the TableModel then in the deleteContact() method you need to invoke the:
fireTableRowsDeleted( ...);
method of your custom TableModel.
See: Row Table Model for a step-by-step example of how to create a custom TableModel for a custom object.
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.
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());
It means I need setData and setHeaders functions for JTable
Like Gilbert says, there is no method of the AbstractTableModel that will allow you to set the values of the headers after the JTable is up, since it is not a very common requirement. Still there is a simple work-around to that. I can not say if it is the best way to do it, but it will get you there...
columnNumber is the number of the column you want to change and newHeaderString is the new String you want to use.
jTable1.getColumnModel().getColumn(columnNumber).setHeaderValue(newHeaderString);
jScrollPane1.setViewportView(jTable1);
Since there is a getTableHeader() method, you could call it if you need to further modify your table header properties.
As for setting new data in any row, use jTable1.setValueAt(newObject, row, col);
if you use a TableModel, you can change the data contents, but not the column headers.
TableModel model = new DefaultTableModel(rowData, columnNames);
JTable table = new JTable(model)
rowData and columnNames have to be defined before you create the table.
The TableModel interface has a setValueAt method. The TableModel interface has no method for setting column names.