I'm trying to implement a global loading dialog... I want to call some static function to show the dialog and some static function to close it. In the meanwhile I'm doing some work in the main thread or in a sub thread...
I tried following, but the dialog is not updating... Just once at the end, before hiding it again, it updates...
private static Runnable getLoadingRunable()
{
if (loadingRunnable != null)
{
loadingFrame.setLocationRelativeTo(null);
loadingFrame.setVisible(true);
return loadingRunnable;
}
loadingFrame = new JFrame("Updating...");
final JProgressBar progressBar = new JProgressBar();
progressBar.setIndeterminate(true);
final JPanel contentPane = new JPanel();
contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
contentPane.setLayout(new BorderLayout());
contentPane.add(new JLabel("Updating..."), BorderLayout.NORTH);
contentPane.add(progressBar, BorderLayout.CENTER);
loadingFrame.setContentPane(contentPane);
loadingFrame.pack();
loadingFrame.setLocationRelativeTo(null);
loadingFrame.setVisible(true);
loadingRunnable = new Runnable() {
public void run() {
try {
while (running) {
Thread.sleep(100);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
loadingFrame.setVisible(false);
}
});
}
};
return loadingRunnable;
}
public static void showLoadingBar() {
System.out.println("showLoadingBar");
running = true;
threadLoadingBar = new Thread(getLoadingRunable());
threadLoadingBar.start();
}
public static void hideLoadingBar() {
System.out.println("hideLoadingBar");
running = false;
threadLoadingBar = null;
}
See Question&Answers more detail:os