Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a nice "LOADING..." animation [duplicate]

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?

like image 943
Roman Rdgz Avatar asked Oct 03 '11 11:10

Roman Rdgz


People also ask

How do I make a simple CSS loading animation?

It's easy to make a simple CSS loading animation. Let's walk through how to make the following loading spinner. First, add a div in HTML and define it with the classname "loader." Next, use the CSS class selector .loader to customize your CSS loading animation. You can define mutliple properties, as shown in the rule set below.

What makes this loading animation so special?

And fortunately, the designer of this animation realizes this psychological fact of users and especially add quickly rotating color bars and rapidly changing numbers to tell users that this web/app is processing their problems in the background quickly. This Loading animation is really a nice shot to offer users a much more pleasant experience.

What is the simplest way to create a simple animation?

The simplest method is creating a loading gif. Gif is an image format used for simple animations. it’s fairly popular and easy to use. CSS loader is another popular choice among developers.

What is the best tool to make an animated loading animation?

There are many different tools that can build loading animations. However, one of the most practical ones is CSS. Let's explore why in the section below. To be clear, you could use another coding language like JavaScript (JS) or a simple animated GIF for your loader.


2 Answers

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);
}
like image 61
dacwe Avatar answered Nov 15 '22 22:11

dacwe


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.
like image 41
Adamski Avatar answered Nov 16 '22 00:11

Adamski