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 MyJtab开发者_运维问答le();
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
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.
Check that you override setValueAt
in your concrete implementation of AbstractTableModel
. The default implementation is empty.
I think Andreas_D has identified a core issue. You might also have problems because you are overriding AbstractTableModel
instead of DefaultTableModel
. The DefaultTableModel
has many of the event firing methods already completed.
This my new MyJtable
That is wrong. The table should NOT have an addWidget() method.
All updates to the data should be done directly through the TableModel. Then the TableModel will notify the table to repaint itself when the data changes.
You should never access the data storage used by the TableModel outside of the TableModel itself. I gave you a complete solution when you asked your other question yesterday.
精彩评论