2

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();
    }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2043602
  • 237
  • 2
  • 4
  • 15
  • 1
    The [progress bar tutorial](http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html) would be a good place to start – Robin Mar 05 '13 at 16:10
  • i started with tuto, but it isn't the problem to create a loop method; to progress the bar, but how to relat the bar to the installation methode that execute a script. or if i use indeterminate mode how to stop animation – user2043602 Mar 05 '13 at 16:15
  • Don't update UI components outside of the EDT context. Use SwingWorker instead of thread – MadProgrammer Mar 05 '13 at 19:21

2 Answers2

2

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().

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • that is what i am doing in the Install class, and the number of line gived as a result of my script is gived by the static variable value – user2043602 Mar 05 '13 at 16:36
  • because when i call this value i will have juste the last value in the end of the execution – user2043602 Mar 05 '13 at 16:37
  • Right, a single value isn't very helpful; you have to parse the script's `stdout` stream. Is it your script? Can you enable something like verbose mode? Edit: Please update your question to reflect your approach. – trashgod Mar 05 '13 at 16:39
1

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
PeakGen
  • 21,894
  • 86
  • 261
  • 463