Reputation: 31
I am currently looking for ways that I could actually print information to the jTextpane when I run the following code. When I press the button to run it, the program actually hang and no display for the output. Is there anyway to go round it or to fix it?
private void ScannetworkActionPerformed(java.awt.event.ActionEvent evt) {
Process p = null;
try {
p = Runtime.getRuntime().exec("ipconfig /all");
} catch (IOException ex) {
Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
}
try {
p.waitFor();
} catch (InterruptedException ex) {
Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
}
BufferedReader buf = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = "";
String output = "";
try {
while ((line = buf.readLine()) != null) {
output += line + "\n";
} } catch (IOException ex) {
Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
}
nninfo.setText(output);
Upvotes: 0
Views: 144
Reputation: 10972
You will need to execute the process in a separate thread, otherwise it will be executed in the UI-Thread and will block any refresh events as long as the process is running. In swing this is usually done by using a SwingWorker
(just google for it and you'll probably find some nice tutorials).
Furthermore Process.waitFor()
will wait for the process to finish and after that you'll read the contents of the process' output. That is you'll not get any updates as long as the process is running. To update your UI with information from the running process you have to read the data from the process' input stream prior to waiting for the process to finish. Maybe this question and the accepted answer will help you to figure out how to do this.
This is what your SwingWorker
might look like. I haven't tested it, but it should give you some idea:
public class ScannetworkWorker
extends SwingWorker<String, String>
{
private final JTextPane mOutputPane;
public ScannetworkWorker(JTextPane aOutputPane)
{
super();
mOutputPane = aOutputPane;
}
@Override
protected String doInBackground() throws Exception
{
Process p = null;
try
{
p = Runtime.getRuntime().exec("ipconfig /all");
}
catch (IOException ex)
{
Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
}
BufferedReader buf = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
String output = "";
try
{
while ((line = buf.readLine()) != null)
{
publish(line);
output += line + "\n";
}
}
catch (IOException ex)
{
Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
}
try
{
p.waitFor();
}
catch (InterruptedException ex)
{
Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
}
return output;
}
@Override
protected void process(List<String> aChunks)
{
final String intermediateOutput = aChunks.stream().collect(Collectors.joining("\n"));
final String existingText = mOutputPane.getText();
final String newText = existingText + "\n" + intermediateOutput;
mOutputPane.setText(newText);
}
}
Upvotes: 1