user2043602
user2043602

Reputation: 237

how to make the progression of a progressbar swing?

I would like to creat an apllication that install files in devices. but i have a problem to implement the progress, the code that i use in my fram to call the class to install is given below, execShellCmd is the method called to install to all devices. value is a static value gived by the Install class. i would like to implement a progressbar relited to install and value in order to give the progression of installation.

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
     Thread t;  
        t = new Thread(){   
        private int postion;
     public void run(){  
        Install install = new Install();
        int position = 0;
        String fileName = directory;
        String shellCommand = fileName;

       // for (int position =0; postion < 105;position +5) {
                jProgressBar1.setValue(Install.value);
                try {
                        Thread.sleep(500);
                } catch (InterruptedException e) {
                }
                position += 5;
        //}
        install.execShellCmd(shellCommand);
        //jTextArea1.setText(error.err.toString());
        }

        };
        t.start();
    }

Upvotes: 2

Views: 777

Answers (2)

PeakGen
PeakGen

Reputation: 22995

Hmm... it is said this thread approach is not suitable for JProgressBar. Instead, move with Timer. I have had the same issue while trying to work with JProgressBar, inside threads. Use the following link for more info, it has working example as well:

http://www.coderanch.com/t/566728/GUI/java/JProgressBar-Napkin-feel-working

And yes, look at my answer in the following:

What is the ways updating jProgressBar?

Upvotes: 1

trashgod
trashgod

Reputation: 205765

As shown here, you can use ProcessBuilder to execute your script. Parse the combined streams, obtained from getInputStream(), to assess the script's actual progress. Use that information to condition your JProgresBar. To preserve liveness in the GUI, do this in the doInBackground() method of a SwingWorker, from which you can invoke setProgress().

Upvotes: 2

Related Questions