-2

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);
                    }
                }
            });
Plumel
  • 45
  • 9

1 Answers1

0

Swing uses Single-threaded model so you reading blocks GUI thread and no event is dispatched and no text rendered until it is free.

You should create a new thread and put all the reading there and update JTextArea text within the loop using SwingUtilities.invokeLater.

You could also use SwingWorker.

ike3
  • 1,760
  • 1
  • 17
  • 26
  • I will try and do this and get back to you. - I haven't dealt with threads in Java/Swing yet so this could take a while – Plumel Sep 20 '16 at 12:18
  • Could you let me know if I'm doing this correctly in my edit? – Plumel Sep 20 '16 at 12:32
  • You should call thread.start() and wrap run.setText(sb.toString()); into SwingUtilities.invokeLater(new Runnable() { ... }); See example here http://stackoverflow.com/questions/12077245/what-is-swingutilities-invokelater – ike3 Sep 20 '16 at 12:38