0

Java, How to refresh JTable in one frame from another frame

How to refresh a JTable after database operations without frame reload?

I am aware that very similar questions have been asked. However none of the answers provided solved my problem.

So i have an application in Java that handles interaction with several databases. Within the program you can add a record, change the criteria for what is to be displayed in the Jtable and edit records that already exists(For example changing the name of a client).

Screen shot of my program

When i click on the "Ascending" checkbox it reverses the rows just as it is supposed to do.

It now orders by ASC not DESC as previously.

So here the JTable is updated as i press on a checkbox or any of the other buttons shown.

When you press on a row, Another frame containing more information on the row shows up. Where you are supposed to be able to edit the name/set as finished and so forth. And this works because when i check the database i see that that changes i made have been applied. Likewise if i click on the refresh button.

The external frame that is summoned so you can edit the row

However when i press on "Finished/edit" the JTable will not update at all until i manually do something(like pressing the refresh button).

Here is the code used in the external JFrame.

    private void AlterTransactionMousePressed(java.awt.event.MouseEvent evt) {                                              
    EditTransaction();

}


private void EditTransaction(){
     GUImannager.RefreshServTable();
}

//In the GUImannager class

    public static void RefreshServTable(){
    System.out.println("RefreshServTable Method has been summoned.\n");
    MainGUI.TableRefresher();
    //The MainGUI is a static variable which contains the JFrame object of the main frame that is summoned that contains the core of the programs such as the pictures as i used.
}

The TableRefresher method is the exactly same method that is used when clicking the refresh button. What it does is start a thread where this code is summoned.

//Gets the data from the database based on conditions.
                TableModel model = new DefaultTableModel(TransData, Colnombs);
            ServiceTable.setModel(model);
            ServiceTable.revalidate();
            ServiceTable.repaint();
            TableContainer.setViewportView(ServiceTable);

And this code ussually works. But whenever it is summoned from an external source it does not work at all. It prints out text stating it has been summoned but it does not refresh the table when summoned externally, only when summoned by something in the same JFrame such as the "refresh" button will it work.

I have tried having code that "listnens" to changes and then updates but while this was apparently summoned according to the system out put it did not effect the program in the way i wanted.

The second JFrame is in another class, but even when i had it as a subclass or within the same file it still didn't work, so i suspect the problem has to do with handling more than one JFrame.

I know this is a very complicated questions, and although almost identical questions have been asked they did not contain any answer that worked for me.

The data is retrieved manually and thus the table is not "bound" to any database(This is because i switch between a SQLite source and a MySQL source).

If any more information/code is necessary i will happily provide it, Any attempts to solve this problem will be very appreciated. If this was an easy question I am sorry as i have not gotten that much experience in software programming yet.

Sincerly...

//Orville Nordström.

Community
  • 1
  • 1
Orvil Nordström
  • 325
  • 6
  • 18

1 Answers1

1

The tutorial says (http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#fire) that you have to fire Events if DataModel is changed. As far as I can see you make a fully new DefaultModel each time, this is not the solution. The Table is waiting for events if something is changed. Like this:

  • fireTableCellUpdated
  • fireTableRowsUpdated
  • fireTableDataChanged
  • fireTableRowsInserted
  • fireTableRowsDeleted
  • fireTableStructureChanged

If you fire one of these event to the registered listeners, than your table will show the correct values.

I believe it is not an issue of two frames, it's an issue of not telling the gui (JTable) to update it after change of data. Maybe have a look here: create TableModel and populate jTable dynamically

Community
  • 1
  • 1
aw-think
  • 4,723
  • 2
  • 21
  • 42
  • I tried using the fireTableDataChanged method but it didn't work. Does it make any difference that i summon a new thread for this ? – Orvil Nordström Feb 11 '15 at 14:26
  • New Thread or new or another JFrame? That's a difference. If it is a only an other JFrame that makes no difference at all. In your TableRefresh do not create a complete new model, just call the fireTableDataChanged() method on your model and the GUI will update itself (if the model is the actual underlying model of the JTable). If it is another Thread than maybe any side effects can happen, cause Swing isn't thread safe. But exactly I can't tell you. Maybe this will help you: http://stackoverflow.com/questions/13407418/threads-and-jtable – aw-think Feb 11 '15 at 14:38
  • Thanks so much for trying but i don't seem to have any luck at all. – Orvil Nordström Feb 13 '15 at 10:25
  • Well I think it's a threading issue. Look for your threads and take care for the EDT (Event Dispatch Thread) in Swing. You should only update the UI in the EDT, loading data and creating a model should be resist in another thread. After the other thread finishes, you should invoke the repaint() and revalidate() methods in the EDT thread. – aw-think Feb 13 '15 at 10:48
  • Well i used the correct thread for GUI's etc "invoke later" and i did a boolean check to see if the program ran in that thread and it returned true. Yet when i use the same actual code to refresh from another tab within the same JFrame then it works flawlessly. Any ideas :) ? – Orvil Nordström Feb 18 '15 at 10:55
  • Nope, you have to show more source code and how the classes and methods work together. Otherwise it is difficult to help you. The only thing I know is: The application have a frame with tabs, on one tab a table is shown with data from a database. When one double click on a row a detail frame is showing the details of the selected row and you are able to edit. After the detail window will be closed with OK, the table should refresh so that all changed data will be displayed. Is that correct? So, we need here the complete calling tree of involved methods and classes. – aw-think Feb 19 '15 at 06:45
  • The point is that when you click on a row in the JTable, Another separate JFrame will be summoned that will show the user details from that table, when you make a change in the new JFrame and press "edit" it closes and is supposed to update the JTable that it "came from". Yet it didn't work at all for some reason, while the same method worked perfectly if i used a button inside the original JFrame. – Orvil Nordström Feb 19 '15 at 11:56
  • However i did find a solution in that you can use "mouse moved" events along with boolean values to activate it from within the Jframe instead. Not the best way but it will have to make do. Thanks so much for your time and effort. – Orvil Nordström Feb 19 '15 at 11:57