Removing a column from TableModel in Java

In Java, I use DefaultTableModel to dynamically add a column to JTable.

//create DefaultTableModel with columns and no rows DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0); JTable table = new JTable(tableModel); 

The variable columnNames is a string array with column names. Thus, after starting the program, the user can add additional columns. I do it as follows

 tableModel.addColumn("New column name"); 

Which dynamically adds a column to the table as desired. The user can also delete the added columns. For this, I use the following code:

  TableColumn tcol = table.getColumnModel().getColumn(0); table.getColumnModel().removeColumn(tcol); 

which should remove the column at the specified index, I also tried:

 table.removeColumn(sheet.getColumn(assessmentName)); 

Both of them work (visually), but here is the problem. After deleting the added column, if another column is added and the table is updated, the previously deleted column will reappear. Therefore, when it visually deletes a column, none of the last two code fragments actually removes it from the model. I assume that since the column was added to the model, where should it be deleted? Is there a specific method that I need to call or some kind of logic that I need to implement in order to remove the column?

+8
java swing tablemodel
source share
4 answers

For your table, try calling table.setAutoCreateColumnsFromModel(false);

This post has a good example of how to remove a column and underlying data.

+6
source share

Acting at the TableColumn level, as you show, has only a visual effect, but does not affect the TableModel .

If you want to really remove the column from the DefaultTableModel , then you need to subclass it, and then in your subclass:

 public class MyTableModel extends DefaultTableModel { public void removeColumn(int column) { columnIdentifiers.remove(column); for (Object row: dataVector) { ((Vector) row).remove(column); } fireTableStructureChanged(); } } 

I have not tested it, but it should work in your case.

Of course, removeColumn() should only be called from EDT.

Please note that I would not encourage anyone to create such code; in particular, using or DefaultTableModel from DefaultTableModel not the best solution for defining a TableModel .

+4
source share

I assume that since the column was added to the model, where should it be removed?

Yes.

Is there a special method that I need to call or some kind of logic that I need to implement in order to delete a column?

No, but you can make your own method:

 moveColumn(...); // to move the column to the end setColumnCount(...); // to remove the last column 

As an additional note, if you want to give users the ability to hide / show columns, see Table Column Manager .

+3
source share

DefaultDataModel does not really have a removeColumn () function, so I myself wrote a function that can really solve the problem.

 private void removeColumn(int index, JTable myTable){ int nRow= myTable.getRowCount(); int nCol= myTable.getColumnCount()-1; Object[][] cells= new Object[nRow][nCol]; String[] names= new String[nCol]; for(int j=0; j<nCol; j++){ if(j<index){ names[j]= myTable.getColumnName(j); for(int i=0; i<nRow; i++){ cells[i][j]= myTable.getValueAt(i, j); } }else{ names[j]= myTable.getColumnName(j+1); for(int i=0; i<nRow; i++){ cells[i][j]= myTable.getValueAt(i, j+1); } } } DefaultTableModel newModel= new DefaultTableModel(cells, names); myTable.setModel(newModel); } 
0
source share

All Articles