Running batch fine onclick delay

51 views Asked by At

I have a JButton that executes a batch file (ping 192.168.0.5) and outputs the ping result into a JTextArea. It does work when I push the button to run the batch file. The console shows the computer being pinged in realtime just like cmd prompt would, but the after the button click, it waits for the batch file to finish executing and then outputs the entire result all at once instead of in realtime like the console. How can I get the textArea output to run in realtime like the console instead of all at once?

JButton btnPingComputer = new JButton("PING");

    btnPingComputer.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {

            String line;

            Process p = null;

            try {

                p = Runtime.getRuntime().exec("c:\\test.bat");

            } catch (IOException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

            BufferedReader in = new BufferedReader(new InputStreamReader(p
                    .getInputStream()));

            try {

                while ((line = in.readLine()) != null) {

                    System.out.println(line);

                    textArea.append(line);
                    textArea.append(String.format("  %s%n", line));

                }

            } catch (IOException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

            try {

                in.close();

            } catch (IOException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // textArea.setText("");

        }

    });
0

There are 0 answers