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.
Is your
latch
variable a CountDownLatch or a CyclicBarrier? If so, or if it is a similar construct, then callingawait()
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.