javaabstracttablemodel

Update Table GUI that extends custom AbstractTableModel


I created a Java GUI that displays the table using the following syntax:

table = new JTable(new MyTableModel(columnNames,
                                    updateTable(cmbAdversary.getSelectedItem().toString(),
                                                cmbdataType.getSelectedItem().toString())));

where columnNames is a Vector of Strings cmbadversary and smbdataType are the selection od combo boxes.

and updateTable is a method that returns a Vector of Vectors depending on the combo box selection as follows:

static Vector updateTable(String FilterVal1 , String FilterVal2) 
{
try {
    myVector = tssc.testSeverityFunctionService(FilterVal1,FilterVal2);
} catch (Exception e) {
e.printStackTrace();}
return myVector;
}

This is how my custom class MyTableModel that extends AbstractTableModel looks like:

class MyTableModel extends AbstractTableModel 
{
    Vector columnNames = new Vector();
    Vector Fdb = new Vector();

    public MyTableModel(Vector cName,Vector rName){
        this.columnNames = cName;
        this.Fdb = rName;}
    public int getColumnCount() { // number of columns in the model.
        return columnNames.size();
    }
    public int getRowCount() { // number of rows in the model.
        return Fdb.size();
    }
    @Override
    public String getColumnName(int col) {
        return columnNames.get(col).toString();
    }
    public Object getValueAt(int row, int col) {
        Vector v = (Vector) this.Fdb.get(row);
        return v.get(col);
    }
    @Override
    public Class getColumnClass(int c) {
        Vector v = (Vector) Fdb.get(0);
        return v.get(c).getClass();}

    public boolean isCellEditable(int row, int col)
    {       return true;    }

    public void setValueAt(Vector value, int row, int col) 
    {
        for(int i=0;i<value.size();i++)
        { for(int j=0;j<columnNames.size();j++) {
                    Fdb.setElementAt(value.get(j),j);   }
        }
        fireTableCellUpdated(row, col);
    }

}

The problem is that when I run the code, the table GUI show me initial values but fails to update when I change the selection in the 2 comboboxes and click the selection button. The Selection button, btw, calls a method which implements the action listener.

Please help me out. Am no pro in Java, but willing to learn. If you have any followup qs., I'll be happy to provide details.


Solution

  • Your solution seems overly complicated. If I understand the basics, the user chooses a value from a combo box, then based on the selection some data is loaded into the table.

    There is no need to create a custom table model to do this.

    A TableModel contains data. If you want to change the data, then one way to do this is to simply create a new TableModel. So you add an ActionListener to your combo box. When an item is selected you retrive your data and load the data into an Vector or an Array. Using this data you can create a new TableModel and update the JTable in two lines of code:

    DefaultTableModel model = new DefaultTableModel(...);
    table.setModel( model );
    

    If you need to customize the model to override the getColumnClass() or isCellEditable() methods, then you should extend the DefaultTableModel. I don't see any need to implement the whole model.