Creating a nice "LOADING..." animation

Roman Rdgz picture Roman Rdgz · Oct 3, 2011 · Viewed 51.6k times · Source

Possible Duplicate:
Pretty alternative to JProgressBar?

I have a process which takes several seconds to load, and I want to create an animation in Java Swing until it finishes loading.

I'd like to avoid using a typical ProgressBar and use a nice modern infinite progress like this one

infinite progress

I'm aware of similar questions but this is strictly Java Swing related. Is there any library or example for this?

Answer

dacwe picture dacwe · Oct 3, 2011

Just use a ImageIcon for this task, it automatically animates gifs. The code below produced this screenshot (the ajax-loader.gif was downloaded from http://www.ajaxload.info/):

screenshot

Code:

public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame("Test");

    ImageIcon loading = new ImageIcon("ajax-loader.gif");
    frame.add(new JLabel("loading... ", loading, JLabel.CENTER));

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);
    frame.setVisible(true);
}