Java: how to make fireTableStructureChanged change AbstractTableModel?

I created a custom AbstractTableModel. The constructor initializes the model with data from a file. However, I want to add an extra column to the model (this is due to SQL constraints on its columns).

I'm trying to achieve this by adding a call to the addColumn (String columnName, Vector columnData) method in the init code.

This addColumn method in my usual AbstractTableModel derives directly from the DefaultTableModel addColumn method, including "fireTableStructureChanged ()".

However, when I run this code, fireTableStructureChanged () does not add my new column and the JTable only displays the data from the file. Why might this be?

Here's a quick indication of the code I'm using:

public class Dummy extends AbstractTableModel {
    public Dummy() {
        //load data from SQL file into ResultSets
        //transfer ResultSet.metadata into columnHeaders Vector<String>
        //transfer ResultSet.data into columnDatums Vector<String>
        fireTableChanged(null);
        addColumn("Added Heading", (Vector)null);
    }

    public addColumn(String columnHeader, Vector columnData) {
         columnHeaders.add(columnHeader);
         // transfer columnData into columnDatums
         fireTableStructureChanged();
    }
 }

      

Is this a listener issue - nothing is listening at this point in time for fireTableStructureChanged ()?

+3


source to share


1 answer


You will need to expose your implementation of the three required (i.e. not implemented) methods defined by the interface TableModel

in AbstractTableModel

. In particular, getColumnCount()

and getRowCount()

should return updated values. The methods fireXxx()

simply instruct the view to query the model through getValueAt()

. Data must be pending getValueAt()

to be retrieved. EnvTableTest

is a simple example. Also, consider a more modern alternative Vector

that includes possibly raw sync code.



+4


source







All Articles