TITLE:JTableの行を全削除

Posted by at 2005-04-11

JTableの行を全削除

`JTable`の行を一括で全削除します。

  • &jnlp;
  • &jar;
  • &zip;
ClearTable.png

サンプルコード

button.addActionListener(new AbstractAction("clear") {
  @Override public void actionPerformed(ActionEvent e) {
    DefaultTableModel model = (DefaultTableModel)table.getModel();
    //ArrayIndexOutOfBoundsException:  0 >= 0
    //Bug ID: JDK-6967479 JTable sorter fires even if the model is empty
    //http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6967479
    table.setRowSorter(null);
    table.getTableHeader().repaint();
    model.setRowCount(0);
  }
});
View in GitHub: Java, Kotlin

解説

モデルが`DefaultTableModelを継承しているなら、setRowCount(0)`ですべての行を削除することができます。この場合、モデルを作り直している訳ではないので、カラムの幅などは削除する前と同じ値を保っています。

`DefaultTableModelを継承していない場合は、モデルに以下の要領(詳細はDefaultTableModel.java`のソースを参照)で行を全削除するメソッドを実装します。

public void clear() {
  //以下のdataVectorは実装に合わせて変更する
  int size = dataVector.size();
  dataVector.clear();
  fireTableRowsDeleted(0, size-1);
  //fireTableDataChanged();
}

`JTable#setAutoCreateColumnsFromModel(false)としておけば、TableModel`を入れ替えても、上記の方法と同じように既存の列はクリアされません。

//((DefaultTableModel)table.getModel()).setRowCount(0);
table.setAutoCreateColumnsFromModel(false);
table.setModel(new DefaultTableModel());

コメントにあるBug ID: JDK-6967479 JTable sorter fires even if the model is emptyのバグに一時的に対応するため、`JTable`が空の場合は、ソートしないように行の追加削除も以下のように修正しています。

private class TestCreateAction extends AbstractAction{
  public TestCreateAction(String label, Icon icon) {
    super(label,icon);
  }
  @Override public void actionPerformed(ActionEvent evt) {
    if(model.getRowCount()==0) {
      //table.setRowSorter(new TableRowSorter<TableModel>(model));
      table.setRowSorter(sorter);
      model.fireTableDataChanged();
    }
    model.addRow(new Object[] {"", model.getRowCount(), false});
    Rectangle r = table.getCellRect(model.getRowCount()-1, 0, true);
    table.scrollRectToVisible(r);
  }
}

private class DeleteAction extends AbstractAction{
  public DeleteAction(String label, Icon icon) {
    super(label,icon);
  }
  @Override public void actionPerformed(ActionEvent evt) {
    int[] selection = table.getSelectedRows();
    if(selection==null || selection.length<=0) return;
    for(int i=selection.length-1;i>=0;i--) {
      model.removeRow(table.convertRowIndexToModel(selection[i]));
    }
    if(model.getRowCount()==0) {
      table.setRowSorter(null);
      table.getTableHeader().repaint();
    }
  }
}

参考リンク

コメント

  • ソートした状態で「remove all rows」を押すと例外が発生しますよ -- tohrisugari
    • ご指摘ありがとうございます。確かに`ArrayIndexOutOfBoundsException: 0 >= 0が発生していますね。以前は正常だったはずと思って調べてみたら、Bug ID: JDK-6967479 JTable sorter fires even if the model is emptyが原因のようです。6u10,6u20で発生?して、8`で修正される予定?みたいなので、しばらくは別の方法を使用するように修正し 何時修正されるか分からないので回避方法を考えてみようと思います。 -- aterai
    • `model.setRowCount(0);の前に、table.setRowSorter(null);`とソートを不可にする修正などを追加しました。 -- aterai