I have a program in which I call another separate program to do some works (lets call it sub-process), the sub-process takes some time to finish each task and shows the progress in a simple console.
The problem is, when I try to read that console using BufferedReader, my own program waits for the sub-process to finish before writing each line in a TextArea.
Here's the code I'm using to read each line:
JTextArea report = new JTextArea(20,40);
report.setText("");
try
{
Process p = new ProcessBuilder(command).start();
p.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = reader.readLine();
while(line!=null)
{
report.append(line+"\n");
line=reader.readLine();
}
}
catch(IOException e1){ error.setText("Failed to run toxpack");}
catch(InterruptedException e2) {error.setText("Failed to run command");}
I tried to look into threads, but It still wouldn't work.
How can I read each line and add them to my textArea without waiting for the whole sub-process to finish.
EDIT: added the rest of the code, but I'm not sure if it helps much. what I'm trying to say is, I want each line that sub-process outputs , to appear in my textArea. without waiting for it to finish.