Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing Loading Animation

I Would like to realize the following loading animation with java swing :

enter image description here

The circle has to spin clockwise.

What would be the best way to make it ?

Thank you very much.

like image 822
Vincent Roye Avatar asked Jan 09 '12 03:01

Vincent Roye


4 Answers

Just use an ImageIcon and an animated gif. see setImageObserver in ImageIcon.

Loading icons can be made using a variety of online generators such as AjaxLoad.

like image 153
Jakob Weisblat Avatar answered Oct 11 '22 22:10

Jakob Weisblat


Hopefully it's not too late for this.

I managed to get the animated gif inside my JPanel this way:

private JPanel loadingPanel() {
    JPanel panel = new JPanel();
    BoxLayout layoutMgr = new BoxLayout(panel, BoxLayout.PAGE_AXIS);
    panel.setLayout(layoutMgr);

    ClassLoader cldr = this.getClass().getClassLoader();
    java.net.URL imageURL   = cldr.getResource("img/spinner.gif");
    ImageIcon imageIcon = new ImageIcon(imageURL);
    JLabel iconLabel = new JLabel();
    iconLabel.setIcon(imageIcon);
    imageIcon.setImageObserver(iconLabel);

    JLabel label = new JLabel("Loading...");
    panel.add(iconLabel);
    panel.add(label);
    return panel;
}

Some points of this approach:
1. The image file is within the jar;
2. ImageIO.read() returns a BufferedImage, which doesn't update the ImageObserver;
3. Another alternative to find images that are bundled in the jar file is to ask the Java class loader, the code that loaded your program, to get the files. It knows where things are.

So by doing this I was able to get my animated gif inside my JPanel and it worked like a charm.

like image 34
Paulo Pedroso Avatar answered Oct 11 '22 21:10

Paulo Pedroso


You can use the Animated Icon class to create your own animation using your existing icon.

like image 38
camickr Avatar answered Oct 11 '22 20:10

camickr


This could be drawn with a custom component or a custom icon, using regular Java2D calls. To me it looks like an Arc2D with a fairly thick BasicStroke drawn with a GradientPaint.

Alternately, export frames from Inkscape (or other graphics program) and load them as images.

like image 25
Russell Zahniser Avatar answered Oct 11 '22 22:10

Russell Zahniser