I have kind of a problem with showing loader during a heavy data loading by main program. Below there is a class called SplashScreen which displays .gif animation of loader.
public class SplashScreen extends JWindow {
public SplashScreen() {
super();
Image image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/resources/load.gif"));
try {
if (image != null) {
JLabel imageLabel = new JLabel();
imageLabel.setIcon(new ImageIcon(image));
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(imageLabel, BorderLayout.CENTER);
this.pack();
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension labelSize = imageLabel.getPreferredSize();
setLocation(screenSize.width / 2 - (labelSize.width / 2),
screenSize.height / 2 - (labelSize.height / 2));
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
dispose();
}
});
addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
dispose();
}
});
}
} catch (Exception ex) {
}
}
There is a result of running this piece of code :
But when I would like to load a lot of data (say 1 milion records) I have something like this :
I was using Thread, EventQueue.invokeLater and SwingUtilities to show SplashScreen in separate Thread, but it doesn't work. It is always blank when I load heavy data. Unfortunately, I must say that using EventQueue or SwingUtilities is quite difficult, because of you are not able to end tasks started by those utilities.