Search code examples
javaswinguser-interfacejtableabstracttablemodel

JTable extended from AbtractTableModel is not updating GUI when i add row


This my new MyJtable

public void addWidget(Book w) {
    datalist.add(w);
    fireTableRowsInserted(datalist.size()-1, datalist.size()-1);

   }

calling class

  MyJtable tv = new MyJtable(a);
        table = new JTable(tv);
        //tv.addWidget(b3);
        JScrollPane pane2 = new JScrollPane(table);

button CLick function

 public void actionPerformed(ActionEvent e)
   {
    MyJtable tv1 = new MyJtable();
    Book b3 = new Book ("Java nutshell-299", "Ajfdfdfdingya2") ;
    if("Add".equals(e.getActionCommand()))
  {
  JOptionPane.showMessageDialog(null,"Add button is clicked");
  tv1.addWidget(b3);
  }

when i click button then i don't see any GUI chnage but if call

tv1.addWidget(b3);

   }

before , i mean on load then i can see the new book but not on button click


Solution

  • I see that you add the row to a new table that you've just created inside the actionPerformed method. Usually, we use actions to change/alter already existing GUI components. This might be a reason why you don't see any change on the GUI.

    I guess, the table that is displayed in the scroll pane is created with

    MyJtable tv = new MyJtable(a);
    table = new JTable(tv);
    

    Try adding the row to table (via tv which has to be made an instance variable first) instead of the newly created table.