CROSP
CROSP

Reputation: 4617

How to insert/delete column to JTable java

I have no ideas what to do. I am creating an application. I need to work with the table, so I am using JTable. But I have a lot problems with it. It seems to work, but when I try to delete column it this column disappears (only in GUI), but all the information still exists. Also columncount doesn't change.
I have searched and tried a lot of different code, but nothing changed.

 public void addTblCol(JTable table,String name) {
    DefaultTableModel model = (DefaultTableModel)table.getModel();
     TableColumn col = new TableColumn(model.getColumnCount());

    col.setHeaderValue(name);
    table.addColumn(col);
    model.addColumn(name);
    this.realColCnt++;
      };
public void delTblCol(JTable table,int index) {
            DefaultTableModel model = (DefaultTableModel)table.getModel();
          TableColumn col = table.getColumnModel().getColumn(index);
    table.removeColumn(col);
    table.revalidate();
    this.realColCnt--;
      };

Upvotes: 6

Views: 27048

Answers (4)

Vivekanand Rajbhar
Vivekanand Rajbhar

Reputation: 31

jTable1.removeColumn(jTable1.getColumnModel().getColumn(0));
  1. replace jTable1 with your table name.
  2. change index number inside "getColumn(0)" according to your table.

Upvotes: 3

D.Kastier
D.Kastier

Reputation: 3015

Based on camickr's solution, I wrote this code to remove a column from the JTable.

public class CustomTableModel extends DefaultTableModel {
    public void removeColumn(int column) {
        // for each row, remove the column
        Vector rows = dataVector;
        for (Object row : rows) {
            ((Vector) row).remove(column);
        }

        // remove the header
        columnIdentifiers.remove(column);

        // notify
        fireTableStructureChanged();
    }
}

Note that it does not check if the column can or cannot be removed.

Upvotes: 2

camickr
camickr

Reputation: 324128

The DefaultTableModel supports a setColumnCount() method which effectively will allow you to remove columns from the end of the model only.

If you want to remove columns from the middle of the model, then you will need to:

  1. extend the DefaultTableModel and create your own removeColumn(int column) method.
  2. This method would need to loop through every row in the Vector and use the Vector.remove(int) method to remove the column for ever row.
  3. Finally once this is done you would need to invoke the fireTableStructureChanged() method to tell the table that a column has been removed so the table can be repainted.

Upvotes: 9

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Some general information related to your question.

The JTable API for public void removeColumn(TableColumn aColumn) explicitly states:

Removes aColumn from this JTable's array of columns. Note: this method does not remove the column of data from the model; it just removes the TableColumn that was responsible for displaying it.

So the behavior you're experiencing is to be expected. If you're trying to remove data from the model, then you're going to have to change your TableModel's data and ColumnModel. Again for more specific help, you'll need to tell us more.

Consider creating a custom table model and giving it a removeColumn(...) method that removes all the data from a single column, and then calls the appropriate fireXXX(...) method.


Edit
You state in comment:

THx for answer , I am newbie . This prog is such for studying I have wasted two day for creating it.And now again have problems with it. What is the easiest way ?

That all depends on what you want to do. If you want to just change the display, then remove the column as you're doing and leave the data alone.

Upvotes: 4

Related Questions