Search code examples
javadatabaseswingjtablederby

Unable to add a row to my JTable with AbstractTableModel mapping to database


I have a JPA entity that I'm attempting to persist in a Derby database and display that database to the User.

Currently I am able to do that. However I would like to update the database for the user as soon as it is added to the database, as currently, The program has to exit and then be started again to show the updated JTable to the user.

I'm using an AbstractTableModel to map my database table to the JTable in the View.

I believe the problem lies somewhere within setValueAt() in my abstractTableModel but I really don't know.

Here is my AbstractTableModel:

public class DBModel extends AbstractTableModel {
    private Model SearchModel;
    private List<FileDoc> results;
    enum Col {ID, NAME, EXISTENCE, MODIFIED}

    public DBModel(){
    SearchModel = new Model();
    List<FileDoc> results = SearchModel.getFile();
    this.results = results;
    }

@Override
public int getColumnCount(){return Col.values().length;}
@Override
public int getRowCount(){return results.size();}
@Override
public Object getValueAt(int row,int col){
    Col column = Col.values()[col];
    FileDoc filedoc = results.get(row);
    switch(column) {
        case ID:        return filedoc.getFileID();
        case NAME:      return filedoc.getFileName();
        case EXISTENCE: return filedoc.isExistence();
        case MODIFIED:  return filedoc.getModified();
    }
return FileDoc.class;
}
@Override
public String getColumnName(int col){
    Col column = Col.values()[col];
    return column.toString();
}

public void addRow(FileDoc filedoc) {
    results.add(filedoc);
    fireTableRowsInserted(results.size()-1, results.size()-1);
    fireTableDataChanged();
}

@Override
public void setValueAt(Object value, int row, int col){
    Col column = Col.values()[col];
    FileDoc filedoc = results.get(row);
    switch(column) {
        case ID:        filedoc.setFileID((Integer)value);
        case NAME:      filedoc.setFileName((String)value);
        case EXISTENCE: filedoc.setExistence((Boolean)value);
        case MODIFIED:  filedoc.setModified((Date)value);
    }
    fireTableCellUpdated(row,col);
}}

and So. That's the model that's mapping the database to the Jtable. Kind of. I mean it takes in a resultList of fileDoc entitities and adds the entity to the resultList.

You can see how I invoke the JTable in The View.

When the user clicks on the add button in The View it sends them to the event handler in The Controller (line 55)

Then in The Model I call the addRow method from the AbstractTableModel. I know that the method is called successfully however no row is added for the user to see.

TL;DR: The user clicks the add button,it goes to the controller, the controller opens JFileChooser to select a file, then calls searchmodel.addfile which goes to the model and then calls the addRow from abstractTableModel, which it calls, and then nothing happens and I'm confused as to what I'm doing wrong and need help.


Solution

  • In the model I was creating a new instance of DBModel when I should have been pointing it to OuterFrame.dbmodel, which is where I had the JTable I needed to update.

    To fix my specific issue it was to delete my previous initialization of dbmodel in the model and then to point towards my dbmodel in the view instead:

    Model.java:13

    OuterFrame.dbmodel.addRow(Document);
    

    Which then is able to add a row to the JTable because it points toward the correct model rather than a newly created dbmodel.

    Solution was reached thanks to /u/chickenmeister's assistance.