I have successfully made it so when a user clicks a button, a command line statement runs and they are able to see the output of it.
However, they are only able to see the output of it once the command has finished.
I was intending on the user seeing a real time output like they would if they were using the command prompt in windows.
Could I get some guidance as to how to do this?
Code:
Runtime runtime = Runtime.getRuntime();
try
{
process = runtime.exec("ping riot.de");
}
catch (IOException e1)
{
System.err.print("Error: " + e1);
e1.printStackTrace();
}
try
{
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = br.readLine()) != null)
{
sb.append(line);
sb.append("\n");
}
run.setText(sb.toString());
}
catch (IOException e)
{
System.err.println("Error: " + e);
}
EDIT - Made new thread and trying to use SwingUtilities.invokeLater This allows you to see the frames components while its working, however it still doesnt write to the textarea line by line.
Runtime runtime = Runtime.getRuntime();
try
{
process = runtime.exec("ping riot.de");
}
catch (IOException e1)
{
System.err.print("Error: " + e1);
e1.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
try
{
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = br.readLine()) != null)
{
sb.append(line);
sb.append("\n");
run.setText(sb.toString());
}
}
catch (IOException e)
{
System.err.println("Error: " + e);
}
}
});