简体   繁体   English

从自定义AbstractTableModel向JTable添加/删除更多列

[英]Add/Remove more columns to a JTable from a custom AbstractTableModel

I have a vector with HashMap elements. 我有一个带HashMap元素的向量。 I want to put it in a table and every HashTable value must be in column with HashTable key column-title. 我想把它放在一个表中,每个HashTable值必须在HashTable键列标题的列中。 So elements with key "key1" must appear on table column with name "key1". 因此,具有键“key1”的元素必须出现在名为“key1”的表列上。

The problem is when I try to add/remove columns of table with setHash() function. 问题是当我尝试使用setHash()函数添加/删除表的列时。 I pass a String[] with more/fewer elements and when this function run the fireTableStructureChanged() java throws like crazy. 我传递一个包含更多/更少元素的String [],当这个函数运行fireTableStructureChanged() java会像疯了一样抛出。

I don't understand where is the problem. 我不明白问题出在哪里。 Can you help me please? 你能帮我吗?

The implementation of Table Model is here: 表模型的实现在这里:

public class ResizableTableModel extends AbstractTableModel {
  protected DataSource src;
  protected String[] hash;

  //......................

  public void setHash(String[] hash) {
        this.hash = hash;
        fireTableStructureChanged();  // THROWS!
  }

  public ArrayList getData() { return src.getData(); }
  public int getColumnCount() { return hash.length; }
  public int getRowCount() { return getData() == null ? 0 : getData().size(); }
  public String getColumnName(int col) { return hash[col]; }
  public boolean isCellEditable(int row, int col) { return true; }
  public Object getValueAt(int row, int col) {
    try {
      return ((HashMap) getData().get(row)).get(hash[col]);
    } catch (Exception e) {
      return null;
    }
  }
  public void setValueAt(Object obj, int row, int col) {
    try {
      //...................
    } catch (Exception e) {}
    fireTableDataChanged();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM