Problem When a button is clicked, it needs to pop up a loading GIF, while a very long process is executed. This pop up automatically closes when the process is completed.
My Approach
if (evt.getSource() == processButton) {
ImageIcon loading = new ImageIcon("img/loading.gif");
final JLabel loaderLabel = new JLabel("loading", loading, JLabel.CENTER);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
ProcessFrame.this.frame.add(loaderLabel);
}
});
process(); // long process
loaderLabel.setVisible(false);
}
ProcessFrame is the main JFrame which the user interacts with.
What problem am I running into?
The loading image never pops up (or atleast as a label). Followed some of the posts on SO, but in vain so far. Where am I getting wrong?
Alternative
Using SwingWorker. However, how do I know the SLEEP_TIME given the process is being executed? Or more precisely, when and how do I integrate the SwingWorker with the process() method running?
SwingWorker<Void, Void> mySwingWorker = new SwingWorker<Void, Void>(){
@Override
protected Void doInBackground() throws Exception {
Thread.sleep(SLEEP_TIME);
return null;
}
};