削除しようとしている特定の列の下にある実際のデータを削除するのに問題があります。私は実際に列とその基になるデータを削除したいと考えています。新しい列を挿入できますが、削除して再度挿入すると、以前に削除した古い列が再びポップアップします。
任意の並べ替えのヘルプをいただければ幸いです。
よろしくお願いします。
データは に保存されますTableModel。
から列を削除してもColumnModel、ビュー ( JTable) に列が表示されなくなるだけです。
それを削除するにTableModelは、列データも削除するように に指示する必要があります。
実装に応じて、JTable.setValueAt(value, row, column)またはTableModel.setValueAt(value, row, column)のどちらか便利な方を使用できます。
もちろん、これはsetValueAtメソッドを実装したことを前提としています
public void removeColumnAndData(JTable table、int vColIndex){MyTableModel model =(MyTableModel)table.getModel();
     TableColumn col =table.getColumnModel().getColumn(vColIndex);
     int columnModelIndex = col.getModelIndex();
     Vector data = model.getDataVector();
     Vector colIds = model.getColumnIdentifiers();
  // Remove the column from the table
     table.removeColumn(col);
  // Remove the column header from the table model
     colIds.removeElementAt(columnModelIndex);
  // Remove the column data
     for (int r=0; r<data.size(); r++) {
        Vector row = (Vector)data.get(r);
        row.removeElementAt(columnModelIndex);
     }
     model.setDataVector(data, colIds);
  // Correct the model indices in the TableColumn objects
  // by decrementing those indices that follow the deleted column
     Enumeration<TableColumn> enum1 = table.getColumnModel().getColumns();
     for (; enum1.hasMoreElements(); ) {
        TableColumn c = (TableColumn)enum1.nextElement();
        if (c.getModelIndex() >= columnModelIndex) {
           c.setModelIndex(c.getModelIndex()-1);
        }
     }
     model.fireTableStructureChanged();
  }
/ * MyDefaultTableModelクラス** /
   class MyTableModel extends DefaultTableModel
  {
     String columns[];
     int size;
      public MyTableModel(String col[],int size)
     {
        super(col,size);
        columns = col;
        this.size=size;
     }
      public Vector getColumnIdentifiers()
     {
        return columnIdentifiers;
     }
  }