17

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?

Community
  • 1
  • 1
Roman Rdgz
  • 12,836
  • 41
  • 131
  • 207

3 Answers3

44

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);
}
dacwe
  • 43,066
  • 12
  • 116
  • 140
10

I'd recommend using the glasspane for this type of activity. The steps involved are:

  • Create a JComponent with BorderLayout. Add a JLabel to the CENTER which includes the animated .gif icon of your choice.
  • (Optional) Override the paint(Graphics) method to make your GUI appear greyed out (or whited out). To achieve this you need to draw a filled rectangle the size of the component, filling the rectangle with a semi-transparent Color.
  • Add the component as the glasspane of your application's root frame.
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Adamski
  • 54,009
  • 15
  • 113
  • 152
3

Sure it's possible. You can use the tool AjaxLoad to generate an animated image, which can be used in any image/html container.

Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148
  • 1
    Would the down-voter care to share their reason? Given the lack of information in the question, this seems a valid answer. – Andrew Thompson Oct 03 '11 at 12:03
  • Not the down-voter, but probably won't work in a Swing component, which supports limited HTML but not Javascript. Possible merge candidate for the [duplicate](http://stackoverflow.com/questions/531831/pretty-alternative-to-jprogressbar). – trashgod Oct 05 '11 at 19:31
  • I'm skeptic about this answer, showing some working code might be appropriate. – dendini Jul 17 '14 at 06:42