0

i'm creating a Java Project using JFrame.

I have created a JFrame A (in the class mainPanel) that that contain a myTable tbl object which extends JTable (public class myTable extends JTable). I made this because this extension must have a public method that populate the table tbl getting values from DB. This table is populated every time i launch the project.

The frame A contain also a JButton (JButton b_partita). This component (b_partita) start a new JFrame B (another "class aggiungiPartita implements ActionListener") using:

b_partita.addActionListener(new aggiungiPartita());

The Frame B is used essentially for populate my database. If everthing goes well, when i press the save button in B, the JFrame will close automatically.

The question is:

is there a way to "refresh" the table tbl (in A) immediately after the end of B's ​​activity?

Thank you all, Stefano

  • 1
    Use a modal dialog; use some kind of observer pattern – MadProgrammer Jun 19 '17 at 22:37
  • Use a `SwingWorker` to populate the table model in the background [for example](https://stackoverflow.com/questions/42969696/dynamically-loading-large-data-in-jtable-using-swingworker/42970185#42970185) and [example](https://stackoverflow.com/questions/17414109/populate-jtable-with-large-number-of-rows/17415635#17415635) – MadProgrammer Jun 19 '17 at 23:02

1 Answers1

0

When you press 'save' and (I assume) save new data to the database you need to notify the table that the underlying data has changed.

It seems from your description that you have extended JTable to populate data from the database at construction. This isn't what you want to do. Instead you need to create your own table model that implements methods to get the data when requested. Create an object of your class and then pass it to the JTable constructor. You rarely need to extend the JTable class itself. See the Java Tutorial for details on how to do this.

I suggest that your table model class extends AbstractTableModel as this does a lot of the work for you. It provides a fireTableDataChanged method (as well as more granular examples) that takes care of notifying the table that it should refresh itself.

If you are implementing your own model without extending the abstract one then it's a bit more work. You will need to call all the table model listeners' tableChanged methods with an appropriately constructed TableModelEvent.

sprinter
  • 27,148
  • 6
  • 47
  • 78