Updating a GUI While Loading
The program starts with Main.java. My program displays a Swing splash screen (Splash.java), then runs a Database class that loads up all the information from a file.
The Database class loads in stages, reading from each table until all the information is in memory. What I want is while each stage is completed, the Database updates the Splash screen with that information. This is what I have so far:
Code:
//Main.java
final Splash splash = new Splash();
splash.setLocationRelativeTo(null);
splash.setVisible(true);
new Thread(new Runnable() {
public void run() {
// Create MainFrame here
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//new Database(splash);
try{
new Database(splash);
}catch(Exception e){
System.out.println("Critical software fault: "+e);
e.printStackTrace();
}
}
});
}
}).start();
and this is how Database starts...
Code:
// Database.java
Splash screen;
Database(Splash splash) {
screen = splash;
initializeDatabase(); // this is the method that reads the database from file
screen.setVisible(false); // discard the splash screen
}
private void initializeDatabase() {
loadDriver();
try {
Properties props = new Properties();
String dbName = "database"; // the name of the database
g.conn = DriverManager.getConnection(protocol + dbName
+ ";create=true", props); // connect to the database
------->*** this is where I want to tell Splash to update a JLabel that the database has been loaded ***
// continue loading tables
// show the main window, and allow the splash to be closed.
}
No matter where I try to put the invokeLater code, it just won't update the GUI while loading. Any help is greatly appreciated. Thanks.