java JTable, Say I have a huge JTable (800*50) with AbstractTableModel. Now I want to remove all table rows and put new data rows into that table. Which way is easiest and high-performance way to achieve this ?
Thanks.
The AbstractTableMoeel doesn't support this. If you extend the AbstractTableModel to create a custom model then you need to implement this method yourself.
Or you can use the DefaultTableModel which implements a setRowCount() method. So you can reset the rows to 0. You can then use the insertRow(...) method to add new rows.
However the easier way is to probably just create a new TableModel. Then you can refresh the table by using:
table.setModel( newlyCreatedModel );
Related
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 am new with JAVA. I have started using JTable. I could not understand how to use tablemodel with JTable. Objective is to understand the use of tablemodel.
As it's name suggests... the TableModel is a model for the table. The JTable is a generic utility which can display data from any source. By implementing a concrete TableModel you are giving the JTable all it needs to populate itself.
I want to know how to give input to the cell in a jtable from keyboard.
And when I try to do this when I move on to next cell the previous enter data is removed or erased automatically.
I am using abstracttable model for creating jtable.
iam using abstracttable model for creating jtable....
and when i try to do dis when i move on to next cell the previous enter data is removed or erased automatically....
The AbstractTableModel doesn't implement the setValueAt(...) method. So unless your custom model implements this correctly you will lose the data entered in the editor.
I suggest you keep it simple and use the DefaultTableModel until you better understand how a JTable works. The code would be:
DefaultTableModel model = new DefaultTableModel(...);
JTable table = new JTable( model );
As discussed in How to Use Tables, you can specify a renderer and editor for each column in a JTable, or you can override getColumnClass() to obtain the default for any of the listed data types. In addition, you might compare what you're doing with one of the examples listed there or edit your question to include an sscce.
Sounds like you are not saving the data entered into your model. Secondly, I would suggest extending DefaultTableModel instead of AbstractTableModel unless you have a good reason.
I have a JTable that I want to use to display some data (a String and a Boolean in each row). The data is maintained by my own class. Is there some way to bind the data model to the JTable, so that when I add to the model, the JTable is dynamically updated and when I remove something from the model, the row is removed from the JTable?
I have previously worked with Flex and Actionscript, and this is very easy to do there with data binding, so I'm just wondering how it's done in Java.
Thanks.
You will need to have your dataset implement the TableModel interface. if you do that then you can apply it to the JTable. If you extend AbstractTableModel you will inherit some event firing methods that your table will handle and will update the view. see this tutorial. Note that the default implementation of JTable will renderer your data for you, and if a Boolean is found, it will show up as a check box.
You'll probably find both the Java JTable tutorial and the JTable API documentation helpful in understanding how JTable works, but otherwise here's a quick rundown.
The premise of a JTable is that it is paired with an object that implements the TableModel interface, which by default is an instance of DefaultTableModel. The table model object is made up of a list of columns, each of which has its own data type (String and Boolean in your case), and a list of rows containing the actual data for the table.
Whenever the JTable is drawn by the swing drawing code, it repeatedly calls the method:
public Object getValueAt(int row, int col)
Thus, when you add data to the table model, it is always rendered as you expect in the next screen refresh (dynamically).
The only thing you really need to worry about, then, is getting the data from your object into the table model and back out again. Other than that, JTable takes care off all the heavy lifting.
While implementing TableModel is easy enough for simple cases, you might want to consider a true binding approach (my favorite is Glazed Lists - watch the 30 second video on how easy this is and you'll be won over). Beans Binding (now Better Beans Binding) also has an implementation of observable lists that might be useful (although I much prefer the Glazed Lists approach)
I know that JTable can sort by a single column. But is it possible to allow for multiple column sort or do I need to write the code myself?
You can sort by multiple columns by specifying more than one sort key when calling setSortKeys in the RowSorter you're using.
Look into JXTable. JXTable is an extension of JTable that supports multi-column sorting, as well as other functions that JTable doesn't provide. It's freely available from JDNC / SwingLabs.
You should be able to set the TableRowSorter and the Comparator associated with it.
Example:
TableModel myModel = createMyTableModel();
JTable table = new JTable(myModel);
TableRowSorter t = new TableRowSorter(myModel);
t.setComparator(column that the comparator works against, Comparator<?> comparator);
table.setRowSorter(new TableRowSorter(myModel));
ETable from the netbeans collection.
It is part of org-netbeans-swing-outline.jar
A google search aught to turn it up.
The ETable is primarily a foundation for Outline (a TreeTable) but it has multi-column ordering built in as well as many other nice features
"I know that Jtable can sort by a single column. But is it possible to allow for multiple column sort or do i need to write the code myself? "
Table sorting and filtering is managed by a sorter object. The easiest way to provide a sorter object is to set autoCreateRowSorter bound property to true;
JTable table = new JTable();
table.setAutoCreateRowSorter(true);
This action defines a row sorter that is an instance of javax.swing.table.TableRowSorter.