1

I have a vector with HashMap elements. I want to put it in a table and every HashTable value must be in column with HashTable key column-title. So elements with key "key1" must appear on table column with name "key1".

The problem is when I try to add/remove columns of table with setHash() function. I pass a String[] with more/fewer elements and when this function run the fireTableStructureChanged() java throws like crazy.

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();
  }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Chameleon
  • 1,804
  • 2
  • 15
  • 21
  • HashMap missing indexing, have to use List instead – mKorbel Apr 10 '13 at 08:23
  • _I don't understand where is the problem._ And it will be hard for anyone to find out where the problem is since your code is incomplete and you don't tell us what exceptions you are getting nor on which line it occurs. Consider posting an [SSCCE](http://sscce.org) – Guillaume Polet Apr 10 '13 at 08:30

1 Answers1

3
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • But `JTable` was created from `XxxTableModel`. There is no way to "regenerate" completely the `JTable` from `XxxTableModel` without replace with a new `JTable`? – Chameleon Apr 10 '13 at 10:44
  • 1
    @Chameleon not this is basic property, by default there no issue add/remove/modify row/column/cell, I'd be to suggest to start with DefaultTableModel, then there are reduced impact from outside, all methods are accesible from JTable, same as from DeafultTableModel, required usage premature array or Vector, but there no difference with HashMap, usage of AbstractTableModel required deepest knowledge about TableModel and used array too, reduce this bothering to the required minimum, then to switch from DefaultTableModel to AbstractTableModel (a few code lines moreover) – mKorbel Apr 10 '13 at 11:22