Search code examples
javaswingjtableabstracttablemodel

How to update Jtable propertly using a class extending AbstractTableModel?


This class extends AbstractTableModel. It has an ArrayList arr but I don't know how to use the fire methods.

 public void adiciona(Entidad e) {
    if(DEBUG)
            System.out.println( " tamaño: "+tamaño()+" columas: "+getColumnCount()+"" +        Arrays.deepToString(columnNames) ); //size 6

    arr.add(e);//adds
    if(DEBUG)
            System.out.println( " tamaño: "+tamaño()+" columas: "+getColumnCount()+"" +   Arrays.deepToString(columnNames) ); //size 7

    //setNumRows( getRowCount() - 1 );
    //System.out.println( "hola");

    fireTableStructureChanged();//BOOOOm EXEPTION NULL POINTER ARRAY
    fireTableDataChanged();
    System.out.println( "hola");
 }

i am checking the java source on how java has the default table model ... what would be the equivalent for

 private void justifyRows(int from, int to) {
    arr.setSize(getRowCount());

    for (int i = from; i < to; i++) {
        if (dataVector.elementAt(i) == null) {
            dataVector.setElementAt(new Vector(), i);
        }
        ((Vector)dataVector.elementAt(i)).setSize(getColumnCount());
    }
}

in an ArrayList


Solution

  • It has an ArrayList

    Then use a custom TableModel that supports an ArrayList containing any custom Object.

    For example you could use the Row Table Model. It provides a custom TableModel that stores custom Objects in an ArrayList. You will need to implement the getValueAt() and setValueAt() methods as they related to your custom Object. The JButtonTableModel example shows you how this can be done.