I am writing a Java application which loads a file before performing any processing. I want to use an animated gif while the file is loading. I was able to create a JFrame which pops up as soon as the file loading starts and disappears just before the file is displayed in the GUI. Taking the help of these two answers, I was able to get this JFrame in place-
Load a gif image untill a method execution and
Creating a nice "LOADING..." animation
However, I am not able to display text or animated gif inside this JFrame. The JFrame is empty and shows nothing, but comes up and disappears on the desired time. Here is my code for the showProgress() and hideProgress() methods:
private JFrame loadingFrame = new JFrame("Notification");
private void showProgress() {
//progress show code here
ImageIcon loading = new ImageIcon("ajax-loader.gif");
loadingFrame.add(new JLabel("File loading in progress. Please wait... ", loading, JLabel.CENTER));
loadingFrame.pack();
loadingFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loadingFrame.setSize(500, 200);
loadingFrame.setLocationRelativeTo(this);
loadingFrame.validate();
loadingFrame.repaint();
loadingFrame.setVisible(true);
}
private void hideProgress() {
//progress show code here
loadingFrame.setVisible(false); //you can't see me!
loadingFrame.dispose(); //Destroy the JFrame object
}
The .gif file is in the same folder as the .java file. Can someone point to where am I going wrong?
With reference to the answer provided here Issue with gif animation in Java swing, I made changes to my showProgress() method, which now calls a URL available on the internet. Here is the code:
public void showProgress() throws Exception{
// imageURL = getClass().getResource("Processing.gif");
// loading = new ImageIcon(imageURL);
ImageIcon loading = new ImageIcon(
new URL("https://i.stack.imgur.com/8IXqb.gif"));
// loadingLabel = new JLabel("Please wait...");
//JFrame
loadingFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loadingFrame.setSize(400,300);
loadingFrame.setLocationRelativeTo(this);
loadingFrame.add(new JLabel(loading, JLabel.CENTER));
loadingFrame.setVisible(true);
}
