6

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--;
      };
yoozer8
  • 7,361
  • 7
  • 58
  • 93
CROSP
  • 4,499
  • 4
  • 38
  • 89
  • You're asking us to guess what is wrong without code, without details. We may be able to give you general advice, but for specific advice, you'll want to show and tell us more. Best would be for you to create and post an [sscce](http://sscce.org). – Hovercraft Full Of Eels Nov 03 '13 at 20:56
  • It helps to remember that a JTable is not a spreadsheet, and does not behave like a spreadsheet. – Gilbert Le Blanc Nov 03 '13 at 21:05

4 Answers4

9

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.
camickr
  • 321,443
  • 19
  • 166
  • 288
  • @AlexandrCrospov, Or you could try writing the code yourself. It would probably be 3-5 lines of code. You need a loop. Inside the loop one statement to remove the item from the Vector. Outside the loop another line of code to invoke the fireXXX method. If you make an effort and post your [SSCCE](http://sscce.org/) the shows what you have tried then we can offer more help, but we are not here to write the code for you. – camickr Nov 04 '13 at 04:54
  • I unfortunately cannot re-up-vote this useful answer. – Hovercraft Full Of Eels Dec 04 '22 at 02:00
4

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 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. Please suggest,what is the easiest way to solve this problem ? – CROSP Nov 03 '13 at 21:08
  • @AlexandrCrospov: see Edit to answer please. – Hovercraft Full Of Eels Nov 03 '13 at 21:10
  • 1
    I need to remove all data from column or just replace column with next column . May be it easier to replace column with desired index ? – CROSP Nov 03 '13 at 21:14
3
jTable1.removeColumn(jTable1.getColumnModel().getColumn(0));
  1. replace jTable1 with your table name.
  2. change index number inside "getColumn(0)" according to your table.
2

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.

Community
  • 1
  • 1
D.Kastier
  • 2,640
  • 3
  • 25
  • 40