I am executing an external process at the backend using Processbuilder in my java program.I am using process.waitFor to get the exit status of the process. The backend process 'll take time which depends upon the input provided.so i need to make a progressbar for indeterminate time until the process completes. This is just to notify the user that there is a process going on at back end.
try {
button.setEnabled(false);
this.progressbar.setVisible(true);
this.progressbar.setIndeterminate(true);
}
catch(Exception e)
{
e.printStackTrace();
}
perform(this);
}
}
public void perform(JFrame f)
{
String cmd[]={"cmd", "/c","a.exe:};
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.directory(new File(dir_path));
pb.redirectErrorStream(true);
try {
p = pb.start();
} catch (IOException ex) {
Logger.getLogger(execution.class.getName()).log(Level.SEVERE, null, ex);
}
try
{
int exitVal = p.waitFor();
if(exitVal>0)
{
this.progressbar.setVisible(false);
this.progressbar.setIndeterminate(false);
this.button.setEnabled(true);
}
if(p.waitFor()==0)
{
this.progressbar.setVisible(false);
this.progressbar.setIndeterminate(false);
JOptionPane.showMessageDialog(f,"Scan completed successfully.");
}
} catch (InterruptedException ex) {
Logger.getLogger(execution.class.getName()).log(Level.SEVERE, null, ex);
}
}
Button is the name of my button which invokes the call. But the problem I'm facing is the function call perform is made without executing these statements.
try {
button.setEnabled(false);
this.progressbar.setVisible(true);
this.progressbar.setIndeterminate(true);
}
catch(Exception e)
{
e.printStackTrace();
}
Button is disabled after the execution of that function. 1) may i know why that happens? 2) may i know the solution to make progressbar indicating the backend progress until it completes?
Thanks in advance.