Jprogressbar loading in real time

Asked

Viewed 64 times

0

It sounds silly, but I don’t know why you’re not incrementing the Jprogressbar() of my Java Swing application. Follow code below:

public void executar() {
        JFileChooser fc = new JFileChooser("Z:\");
        fc.showOpenDialog(this);

        File file = fc.getSelectedFile();
        v_Caminho.setText(file.getAbsolutePath()); //campo texto simples

        new Thread() {

            @Override
            public void run() {

                int fileSize = (int) file.length();
                v_Progresso.setMaximum(fileSize); //JProgressBar()

                try {
                    for (int i = 0; i <= fileSize; i++) {
                        fileSize = (fileSize * 100) / 1024;

                        v_Progresso.setValue(fileSize);
                        v_Progresso.setStringPainted(true);

                        sleep(300);

                        System.out.println("Restam: " + fileSize + " bytes para o carregamento.");
                    }
                } catch (InterruptedException ex) {
                    Logger.getLogger(ProgressBar.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }.start();
    }

It calculates the file size and prints in the "Sout" the rest for loading. I need to make this remaining turn increment in Jprogressbar() and it’s not working. What I’m doing wrong?

  • You have to use SWINGWORKER, see another answer q uses this solution: https://answall.com/a/119137/164151

1 answer

0

A curiosity, it would not be easier to set the value of jprogress with the i?

the for could be so:

for (int i = fileSize; i >=0; i--) {

    v_Progresso.setValue(i);
    v_Progresso.setStringPainted(true);

    sleep(300);

    System.out.println("Restam: " + i+ " bytes para o carregamento.");
}

Let’s just think a little bit about this code below:

JFileChooser fc = new JFileChooser("Z:\\");
        fc.showOpenDialog(this);

        File file = fc.getSelectedFile();
        v_Caminho.setText(file.getAbsolutePath()); //campo texto simples

        new Thread() {

            @Override
            public void run() {

                int fileSize = (int) file.length();
                v_Progresso.setMaximum(fileSize); //JProgressBar()
                v_Progresso.setMinimum(0);
                System.out.println("Tamanho do fileSize inicial: "+fileSize);
                try {
                    for (int i = 0; i <= fileSize; i++) {
                        fileSize = (fileSize * 100) / 1024;
                        System.out.println("FileSize: "+fileSize+ "\t i = "+i);
                        v_Progresso.setValue(fileSize);
                        //v_Progresso.setStringPainted(true);

                        sleep(1000);

                        System.out.println("Restam: " + fileSize + " bytes para o carregamento.");
                    }
                } catch (InterruptedException ex) {
                    System.out.println(ex);
                }
            }
        }.start();
        System.out.println("fim");

Let’s say we have an initial 356352b file.. then in the first for (i==0) it will be worth 34800 then i == 1 will be 3398 then i == 2 331 then i == 3 32 i ==4 3 and will end with 3 filesize,

let’s analyze the first interaction with for.. o i =0

In the entry we have 356352b but in the first interaction with the for it is already worth 9.76% of the total value (34800).. And with every repetition that number only falls... reaching 0 very fast.. as we can see the value 3398 is already very small compared to the initial 34800.. imagine 331 and 32...

  • So friend, I had already done it this way. The problem is that it’s going to do a very huge regression. I need to set Jprogressbar to its value according to how much "really" remains to be loaded. For example: filesize = 9Mb. I need to make Progressbar take over this Maximum and calculate the decrease. That’s the problem. It loads 12% initials then goes back to 0%. Meanwhile, the Sout fires perfectly: Left: 893762 bytes. Left: 87281 bytes. Left: 8523 bytes. Left: 832 bytes. Left: 81 bytes. Left: 7 bytes. Left: 0 bytes.

  • I understand... I’ll take a closer look at my code, then. I think I did some procedure incorrectly until I got to this FOR as you suggested. It really hadn’t worked. So I did something wrong... I will return with a status. Thank you!

Browser other questions tagged

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