Reputation: 15
I am using Netbeans for developing a java application. I am trying to remove columns on a jTable, and than adding new columns to it with the data that I am extracting. The problem is, I am able to remove the columns easily, using the code snippet below. But when I am adding new columns, the old columns that I had deleted return. Will really appreciate any help.
The code snippet below is for an event handler, where I pick a date using date picker.
d = jXDatePicker1.getDate();
int i=jTable1.getColumnCount();
if(i>1){
for (;i > 1;i--) {
TableColumn colToDelete = jTable1.getColumnModel().getColumn(jTable1.getColumnCount() - 1);
jTable1.removeColumn(colToDelete);
jTable1.validate();
}
The code above removes the columns from jTable1.
}
emp1 = ControllerClass.searchEmpAvalibility(String.valueOf(d.getDay()));
DefaultTableModel model=(DefaultTableModel)jTable1.getModel();
for (Employee e : emp1) {
if (e != null) {
model.addColumn(e.getEmployeeId() + "," + e.getFirstName() + " " + e.getLastName());
}
}
Now I want to add, extra columns with a new column for an employee. unfortunately, the columns that I have deleted earlier return, when I add a column. I have even trien model.fireTableDataChange(); but it is not working.
In the first picture, the Table is initialized with two columns added. When I click on the date, The code snippet above executes. Please keep in mind, this is for a scheduling system, and as an example I currently have two employees in the system.
In the third picture, I have selected a date where no employee is on the schedule, thus there are no columns to be added. As seen in the picture, the first part of the code snippet is working perfectly, with the columns removed and the only required first column with the time values being displayed.
In the fourth picture, I chose a date where both of the employees are working. Now not only the last last two columns return that I had presumed to be deleted, but it also executes the second part of the code snippet above and adds two more columns to the table.
Upvotes: 0
Views: 870
Reputation: 324207
jTable1.removeColumn(colToDelete);
That method just removes the column from the JTable view. The data is still contained in the TableModel.
If you want to remove data from the model then DefaultTableModel
supports a setColumnCount()
method, but this will only remove the data from columns at the end of the model, which may, or may not satisfy your requirement.
model.addColumn(e.getEmployeeId() + "," + e.getFirstName() + " " + e.getLastName
When you add a column to the model a fireTableStructureChanged() event is generated which causes the JTable to recreate the TableColumnModel based on all the columns in the TableModel, so the column you removed from the view are no visible again.
If you don't want the TableColumnModel to be recreated automatically, then after you create the JTable the first time you can use:
table.setAutoCreateColumnsFromModel(false);
However, now you will be responsible for using the JTable.addColumn(...)
method to add the new TableColumn
to the table. Don't forget to specify the proper TableModel column index when you create the TableColumn.
I can't say that I understand your requirement, so I can't give specific code only general advice.
Upvotes: 1