Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert/delete column to JTable java

Tags:

java

swing

jtable

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--;
      };
like image 901
CROSP Avatar asked Nov 03 '13 20:11

CROSP


2 Answers

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.
like image 116
camickr Avatar answered Oct 20 '22 02:10

camickr


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.

like image 45
Hovercraft Full Of Eels Avatar answered Oct 20 '22 01:10

Hovercraft Full Of Eels