Update jLabel content from the output of shell script

I have in a jFrame a jLabel that must update its content from the output of shell script. The execution of the shell script is made from a thread in another jFrame in which I store the output in a StringBuilder (public and static):

p = Runtime.getRuntime().exec("testpad -i -c"+can+" -n"+pad+" "+pathFile);
            final InputStream inStream = p.getInputStream();

            Thread uiThread = new Thread("UIHandler") {
                @Override
                public void run() {
                  InputStreamReader reader = new InputStreamReader(inStream);
                  Scanner scan = new Scanner(reader);
                  prec=null;

                  while (scan.hasNextLine()) {
                    prec=scan.nextLine();
                    System.out.println(prec);
                    sbuff.append(prec);
                    sbuff.append('n');

                  }
               }
            };
            uiThread.start();

I update the jLabel (content in a jPannel) when the jPannel is showed:

private void jPanel1ComponentShown(java.awt.event.ComponentEvent evt) {                                       
    // TODO add your handling code here:
    jLabel2.setText(inizio.sbuff.toString());

}

I think it is a race condition trouble but I put a Thread.sleep() with many second but the conten of jLabel don’t update.
I am doing this how example:

enter image description here

When I push the jButton the shell script print the output in the rectangle in red , then it opens a new jFrame with the jLabel that it should update but its don’t change. Where am I wrong? Thanks.


Source: shell

Leave a Reply