开发者

Removing Column from TableModel in Java

开发者 https://www.devze.com 2023-03-04 04:33 出处:网络
In Java I\'m using the DefaultTableModel to dynamically add a column to a JTable. //create DefaultTableModel with columns and no rows

In Java I'm using the DefaultTableModel to dynamically add a column to a JTable.

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

The columnNames variable is a string array with the column names. So after the program is up and running the user has the option to add additional columns. I do so as follows

tableModel.addColumn("New column name");

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

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

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

table.removeColumn(sheet.getColumn(assessmentName));
开发者_JS百科

Both of them work (visually), but here's the problem. After deleting an added column, if another column is added and the table refreshes, the previously deleted column is there again. So while it is removing the column visually, neither of the last two code snippets actually removes it from the model. I'm assuming here that since the column was added to the model that is where it needs to be removed from? Is there a specific method that I need to call or some logic that I need to implement to remove the column?


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

This post has a good example as to how to delete column and the underlying data.


I'm assuming here that since the column was added to the model that is where it needs to be removed from?

Yes.

Is there a specific method that I need to call or some logic that I need to implement to remove the column?

No, but you can make up your own method:

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

As a side note if you want to give the users the ability to hide/show columns check out the Table Column Manager.


Acting at the TableColumn level, as you show, has only a visual impact but no impact on the TableModel whatsoever.

If you want to really remove a column from DefaultTableModel then you'll 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 haven't checked it, but it should work in your case.

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

Note that I wouldn't encourage anyone to produce this kind of code; in particular, using, or deriving from, DefaultTableModel is not the best solution to define a TableModel.


The DefaultDataModel doesn't have a really removeColumn() function, so I wrote a function myself, which can actually 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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号