Java 如何清除 jTable 的内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3879610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 06:16:35  来源:igfitidea点击:

How to clear contents of a jTable ?

javaswingjtable

提问by tom

I have a jTable and it's got a table model defined like this:

我有一个 jTable,它有一个如下定义的表模型:

javax.swing.table.TableModel dataModel = 
     new javax.swing.table.DefaultTableModel(data, columns);
tblCompounds.setModel(dataModel);

Does anyone know how I can clear its contents ? Just so it returns to an empty table ?

有谁知道我如何清除它的内容?就这样它返回到一个空表?

采纳答案by locka

Easiest way:

最简单的方法:

//private TableModel dataModel;
private DefaultTableModel dataModel;


void setModel() {
  Vector data = makeData();
  Vector columns = makeColumns();
  dataModel = new DefaultTableModel(data, columns);
  table.setModel(dataModel);
}

void reset() {
  dataModel.setRowCount(0);
}

i.e. your reset method tell the model to have 0 rows of data The model will fire the appropriate data change events to the table which will rebuild itself.

即您的重置方法告诉模型有 0 行数据模型将触发适当的数据更改事件到将重建自身的表。

回答by eee

If you mean to remove the content but its cells remain intact, then:

如果您要删除内容但其单元格保持完整,则:

public static void clearTable(final JTable table) {
   for (int i = 0; i < table.getRowCount(); i++) {
      for(int j = 0; j < table.getColumnCount(); j++) {
          table.setValueAt("", i, j);
      }
   }
}

OK, if you mean to remove all the cells but maintain its headers:

好的,如果您要删除所有单元格但保留其标题:

public static void deleteAllRows(final DefaultTableModel model) {
    for( int i = model.getRowCount() - 1; i >= 0; i-- ) {
        model.removeRow(i);
    }
}

回答by willcodejavaforfood

You have a couple of options:

你有几个选择:

  1. Create a new DefaultTableModel(), but remember to re-attach any listeners.
  2. Iterate over the model.removeRow(index)to remove.
  3. Define your own model that wraps a List/Set and expose the clearmethod.
  1. 创建一个new DefaultTableModel(),但请记住重新附加任何侦听器。
  2. 迭代model.removeRow(index)删除。
  3. 定义您自己的模型来包装 List/Set 并公开该clear方法。

回答by james.garriss

Another easy answer:

另一个简单的答案:

defaultTableModel.getDataVector().removeAllElements();

回答by Gebreigziabher Abadi

    //To clear the Contents of Java JTable

    DefaultTableModel dm = (DefaultTableModel) JTable1.getModel();

    for (int i = 0; i < dm.getRowCount(); i++) {
        for (int j = 0; j < dm.getColumnCount(); j++) {
            dm.setValueAt("", i, j);
        }
    }

回答by Malith

I think you meant that you want to clear all the cells in the jTable and make it just like a new blank jTable. For an example, if your table contains 40 raws, you can do following.

我想你的意思是你想清除 jTable 中的所有单元格,让它就像一个新的空白 jTable。例如,如果您的表包含 40 个原始数据,您可以执行以下操作。

DefaultTableModel model = (DefaultTableModel)this.jTable.getModel();
model.setRowCount(0);
model.setRowCount(40);

回答by Garrett Bates

public void deleteAllRows() {
    DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
    model.setRowCount(0);
}

回答by Farruh Habibullaev

One of the trivial methods is to use the following option.

一种简单的方法是使用以下选项。

dataModel.setRowCount(0);

dataModel is the model which you would like clear the content on

dataModel 是您想要清除内容的模型

However, it is not optiomal solution.

但是,这不是最佳解决方案。