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.
Related
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());
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.
I was re designing my codes with formerly using
JTable jtable = new JTable();
int selectedIndex = jtable.getSelectedColumn();
//implementations
Now, I need to use DefaultTableModel. Is there a method or implementation that is related to this method of JTable? Thanks!
A TableModel has nothing to do with the selected column of the JTable.
It doesn't matter what TableModel you use with the JTable, you still use
int selectedIndex = table.getSelectedColumn();
method to get the selected column.
Of course, there is no selected column when you first create the JTable. The user must select the column or you must set the selection on a particular cell of the table.
So you only use the getSelectedColumn() method in response to some kind of user event.
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.
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 );