1

I want use an indeterminate jProgressBar on a JForm but I don't know why in my code don't work. The jProgressBar must be in the indeterminate status until the thread receives a latch.await() signal. This is the simple part of code when I push a button:

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    jProgressBar1.setVisible(true);
    jProgressBar1.setIndeterminate(true);

    inizio.sbuff.setLength(0);
    inizio.latch.reset();
    OutputStream outStream = inizio.p.getOutputStream();
    PrintWriter pWriter = new PrintWriter(outStream);
    pWriter.println("17");
    pWriter.flush();

    try {
        inizio.latch.await();
    } catch (InterruptedException ex) {
        Logger.getLogger(menu.class.getName()).log(Level.SEVERE, null, ex);
    }

    new risultati().setVisible(true);



     jProgressBar1.setIndeterminate(false);

    //jProgressBar1.setVisible(false);
    //this.setEnabled(false);
}

The jProgressBar does not activate before the arrival of the signal. If I remove the jProgressBar1.setIndeterminate(false); the jProgressBar activates after the signal of countdown arrived and not before. This is the part of the code when I do the countdown:

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');

                    if(err.equals(prec)){
                        //flag[0] = 1;
                        bandiera = 1;
                        //latch.countDown();
                    }

                    if(prec.contains("Quit")){
                        //System.out.println("STO DENTRO "+prec);
                        latch.countDown();
                    }

                  }

               }
            };
            uiThread.start();

Can I activate the jProgressBar first of the arrival of countdown signal? Thanks.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Local Hero
  • 493
  • 2
  • 7
  • 20
  • Can you be more specific? I don't understand where the problem is. – Crazyjavahacking Jun 15 '15 at 12:42
  • I guess that you could have a concurrent issue because, perhaps, you are working in the Event Dispatch Thread and you are blocking the execution flow with the await. It would be interesting to see where you make the countdown. – Victor Jun 15 '15 at 12:51
  • I have inserted the part of code when I call the countdown. Thanks. – Local Hero Jun 15 '15 at 13:00

1 Answers1

0

Is your latch variable a CountDownLatch or a CyclicBarrier? If so, or if it is a similar construct, then calling await() is a blocking call, and since you're calling this on the Swing event thread, something that should never be done, you're locking the thread rendering the GUI frozen. The solution is simple -- don't do this sort of stuff on the Swing event thread but rather within a background thread such as by calling it within the doInBackground method of a SwingWorker. Please check out Lesson: Concurrency in Swing for more on this.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Yes, it is a CountDownLatch. Is it complicated to change everything? The code is in an advanced state. – Local Hero Jun 16 '15 at 07:13
  • @LocalHero: I don't know since I don't know the state of the rest of your program. All I can say is that if you want your GUI to behave well, to not be frozen, you really have no choice, advanced state of development or not. – Hovercraft Full Of Eels Jun 16 '15 at 11:16