In a swing application, I have a popup jDialog which pops up with a jlabel that says "Hang on 5 seconds."
After 5 Seconds, the label should change to "Okay, now I'm done." And a button should appear allowing the user to click continue.
In the example action below (linked to a button which causes the popup), the popup appears as it should but it is blank instead of saying "Hang on 5 seconds." Then after 5 seconds everything updates and the labels are there and the button too. So what's going on? Is the thread sleeping before a repaint or something?
@Action
public void popUp() {
popUpDialog.setSize(300,200);
popUpDialog.setLocationRelativeTo(null);
popUpDialog.setVisible(true);
popUpLabel.setVisible(true);
popUpLabel.setText("Working, hang on a sec....");
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
popUpLabel.setText("Okay Now I'm Done.");
popUpBut.setVisible(true);
}
EDIT: So I tried this, in an effort to use a swing timer in place of thread sleeping:
@Action
public void popUp() {
popUpDialog.setSize(300,200);
popUpDialog.setLocationRelativeTo(null);
popUpDialog.setVisible(true);
popUpLabel.setVisible(true);
popUpLabel.setText("Working, hang on a sec....");
Timer timer = new Timer(speed, this);
timer.setInitialDelay(pause);
timer.start();
popUpLabel.setText("Okay Now I'm Done.");
popUpBut.setVisible(true);
}
Obviously I'll need more code to finish the timer, but right off the bat I get a symbol not found, variable: timer
error. What's that all about? Am I doing it wrong?
Edit 2: I changed the timer declaration and solved one problem but created another. Now I am getting the symbol not found error in regards to speed
. I have never used a swing timer before and don't know how to use them. The java tutorial on the topic is convoluted and difficult to understand. Can any of you point me to a simple, clear example of a timer so I can learn from that and figure out what I need to do?