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).
When i click on the "Ascending" checkbox it reverses the rows just as it is supposed to do.
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.
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.
Aucun commentaire:
Enregistrer un commentaire