Search code examples
javaswingjtabletablemodel

How come JTables aren't showing additions to them?


I am adding objects from one JTable to another, and I can see through debugging that in the CustomTableModel the objects are being added to the list of objects. Only the first object I add shows up in the new JTable.

So I can add many objects to the TableModel, but only the first one is showing up in the JTable.

here's my add method:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        // TODO add your handling code here:
       if(physPackageModel != null){
        int h = secRowSeat2.getSelectedRow();
        Physical_Package pp = PpackageList.get(h);
           if(physPackageModel2 != null){
                 physPackageModel2.addRow(pp);
                 physPackageModel.removeRow(h);

           }
           else{

                    physPackageModel2 = new tableModel2();
                    physPackageModel2.addRow(pp);
                    physPackageModel.removeRow(h);



           }
        secRowSeat1.setModel(physPackageModel2);
        }
       else{
          int h = secRowSeat2.getSelectedRow();
          EventSeat es = eventSeatList.get(h);

        if(eventSeatModel2 != null){
                eventSeatModel2.addRow(es);
                eventSeatModel.removeRow(h);
        }else{

                eventSeatModel2 = new EventTableModel2();
                eventSeatModel2.addRow(es);
                eventSeatModel.removeRow(h);
           }
        secRowSeat1.setModel(eventSeatModel2);
        secRowSeat2.setModel(eventSeatModel);
        repaint();
       }

    }

Let me know if you want to see my custom table model's.....

add and remove methods from customTableModel:

public void addRow(Physical_Package rowData)
    {
        insertRow(getRowCount(), rowData);
    }

    public void insertRow(int row, Physical_Package rowData)
    {
        modelData.add(row-1, rowData);
        fireTableRowsInserted(row, row);
                this.fireTableDataChanged();
    }
    public void removeRow(int row)
    {
        modelData.remove(row);
        fireTableRowsDeleted(row, row);

    }

Solution

  • public void insertRow(int row, Physical_Package rowData)     
    {
             modelData.add(row-1, rowData);
             fireTableRowsInserted(row, row);
             this.fireTableDataChanged();
    } 
    

    So much for copying the code from the working TableModel I gave you yesterday. That is not the code I gave you.

    1. I did not use fireTableDataChanged
    2. I did not use "row - 1" in the add(...) method. Of course the fireTableRowsInserted(...) method won't work if you add a row at postion 0, and then tell the table updated you inserted row 1, The table will attempt to repaint row 1.

    I don't see why you need any check for the existence of a table model. You should always create and display a table with a table model, even if the table contains no rows.