How to create a circle of progress?

4

I want to make a circle progress like this:

In Java, I could only find something about JProgressBar .

    
asked by anonymous 31.08.2015 / 20:04

1 answer

4

There is class AnimatedIcon - non-default on the Java platform - which apparently suits what you're wanting to do, I do not know if visually it will look the way you expect but the idea of displaying a circle to tell you that something is being loaded is the same.

The simplest (and also the one I use) for this kind of situation is to simply insert an animated image with .gif extension into a JLabel . Using the setIcon() you can do this.

There are several sites that provide these loading circles on the internet, so Google can help you find one that fits your needs. Preloaders and SpiffyGif are good and allow you to customize some options existing, I ended up doing a very quick example on the first site to test:

Havingthisimage,justputitasaresourceinyourproject.ThenyoucancreateamethodtoreturnaJPanel/JLabelcontainingthisimageasanicon,whichdefinesyou.Asnothingwasspecifiedinthequestion,IcreatedanexamplewiththeaboveimagebeinginserteddirectlyintoJFrame,theresultisthis:

Andfollowthecode:

importjava.awt.*;importjavax.swing.*;importjava.net.URL;publicclassLoadingTestextendsJFrame{publicLoadingTest(Stringtitle){super(title);//PropriedadesdoJFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);setLayout(newFlowLayout());setSize(150,150);//Obtendoorecurso(nocaso,aimagemde'loading').ImageIconloadingImage=newImageIcon(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);
        }); 
    }
}
    
01.09.2015 / 00:24