Question

I have made a custom AbstractTableModel. The constructor initialises the model with data from a file. However, I wish to add an extra column to the model (this is because of SQL limitations in its columns).

I seek to achieve this by adding to the initialisation code a call to an addColumn(String columnName, Vector columnData) method.

This addColumn method in my custom AbstractTableModel is derived directly from DefaultTableModel's addColumn method, including "fireTableStructureChanged()".

Yet when I run this code, fireTableStructureChanged() appears not to add my new Column and the JTable displays only with data from the file. Why might this be?

Here is a short indication of the code I am 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 it a listener problem - is nothing listening at this point in time to fireTableStructureChanged()?

Was it helpful?

Solution

You'll have to reveal your implementation of the three required (i.e. unimplemented) methods specified by the TableModel interface in AbstractTableModel. In particular, getColumnCount() and getRowCount() must return the updated values. The fireXxx() methods simply instruct the view to query the model via getValueAt(). The data has to be there waiting for getValueAt() to retrieve. EnvTableTest is a simple example. Also, consider a more modern alternative to Vector, which includes possibly uneeded synchronization code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top