An application should display a modal information dialog (JDialog class) after the user has entered their login credentials. This is needed because, in the case of first login or reset profile, the application needs to set up a few things, while this is done, the user should not be able to click anything and see the dialog.
My first approach was the following:
if(hasLoggedIn){
myInfoDialog.displayLogin();
try {
TimeUnit.SECONDS.sleep(5);
} catch (Exception e){}
}
The class "myInfoDialog" will call an info dialog:
public void displayLogin(){
myJDialog.setLogin()
}
And the setLogin Metod of the JDialog will do:
public void setLogin(){
okButton.setVisible(false);
okButton.setEnabled(false);
cancelButton.setVisible(false);
cancelButton.setEnabled(false);
iconLabel.setIcon(infoIcon);
dialogTextField.setText(initMsg);
this.setVisible(true);
}
The issue is, that myInfoDialog.displayLogin(); will only show the dialog frame and title, and an empty white body looks like the rendering was incomplete. After the 5s of sleep, which I used to simulate the background operation after login, the dialog will finish rendering. But in the final case, this would be the moment where the dialog would be set to invisible.
As a first try to solve the problem, I've implemented a swing worker:
SwingWorker<Void, Void> mySwingWorker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
//Here you put your long-running process...
myInfoDialog.displayLogin();
return null;
}
};
And used like this:
if(hasLoggedIn){
mySwingWorker.execute();
try {
TimeUnit.SECONDS.sleep(5);
} catch (Exception e){}
}
But the issue still persists. What method could be used to display and render the dialog, regardless of other code execution?