How to create a circle of progress?

Asked

Viewed 566 times

4

I’m looking to make progress in a circle like this:

exemplo

In Java, I could only find something about JProgressBar.

  • There’s an interesting thing to do in java!

1 answer

4


There is the class AnimatedIcon - non-standard on the Java platform - which apparently serves what you’re trying to do, I don’t know if you’ll visually look like you expect but the idea of showing a circle to inform you that something is being loaded is the same.

The simplest way (and also what I use) for this type of situation is simply to insert an animated image with extension .gif in a JLabel. By means of the method setIcon() you can do this.

There are several sites that provide these circles of loading on the Internet, Google can help you find one that meets your needs. The Preloaders and the Spiffygif are good and allow you to customize some existing options, I ended up making a very fast example on the first site to test:

loading

Having this image, just put it as a resource in your project. Then you can create a method to return a JPanel/JLabel containing this image as an icon, this defines you. Since nothing was specified in the question, I created an example with the image above being inserted directly into JFrame, the result is this:

preview

And follow the code:

import java.awt.*;
import javax.swing.*;

import java.net.URL;

public class LoadingTest extends JFrame {

    public LoadingTest(String title) {
        super(title);
        // Propriedades do JFrame.
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        setSize(150, 150);


        // Obtendo o recurso (no caso, a imagem de 'loading').
        ImageIcon loadingImage = new ImageIcon(getClass().getResource("loading.GIF"));

        // Instanciando um novo JLabel e definindo a imagem encontrada como ícone.
        JLabel loadingLabel = new JLabel();
        loadingLabel.setIcon(loadingImage);

        // Inserindo o JLabel no Frame.
        getContentPane().add(loadingLabel);
    }

    public static void main(String... args) {
        SwingUtilities.invokeLater(() -> {
            new LoadingTest("Testando Loading").setVisible(true);
        }); 
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.