Process output only becomes available after the process has finished

前端 未结 5 1333
死守一世寂寞
死守一世寂寞 2021-01-07 03:50

I have a Runnable that reads Console output from an externally called exe (see below) and writes it to both a log file and a JTextArea.

But my Runnable doesn\'t show

5条回答
  •  孤独总比滥情好
    2021-01-07 04:17

    Just to help the miner - below's a complete minimalistic (left out everything not absolutely necessary) example that indeed works in my context: each line appears in the textArea as read. It's basically using the SwingWorker as suggested by Justin and re-arranged thingies a bit for clarity.

    public class ProcessExample {
    
        public static class ProcessWorker extends SwingWorker {
            private JTextArea ta;
            private List process;
    
            public ProcessWorker(List command, JTextArea ta) {
                this.process = command;
                this.ta = ta;
            }
    
            @Override
            protected Void doInBackground() throws Exception {
                Process p = new ProcessBuilder(process).start();
                // Read Process Stream Output and write to LOG file
                BufferedReader is = new BufferedReader(new InputStreamReader(
                        p.getInputStream()));
                String line;
                while ((line = is.readLine()) != null) {
                    publish(line);
                }
                is.close();
                return null;
            }
    
            @Override
            protected void process(List chunks) {
                for (String string : chunks) {
                    ta.append(string + "\n");
                }
            }
    
        }
    
        private void startProcess(JTextArea ta) {
            ArrayList command = new ArrayList();
            command.add("ping");
            command.add("127.0.0.1");
            new ProcessWorker(command, ta).execute();
        }
    
        private JComponent getContent() {
            JPanel main = new JPanel(new BorderLayout());
            final JTextArea ta = new JTextArea(20, 60);
            main.add(new JScrollPane(ta));
            Action action = new AbstractAction("Start!") {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    startProcess(ta);
                }
            };
            main.add(new JButton(action), BorderLayout.SOUTH);
            return main;
        }
    
        public static void main(String args[]) throws IOException {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame("Example");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
                    frame.add(new ProcessExample().getContent());
                    frame.pack();
                    frame.setVisible(true);
    
                }
            });
        }
    
    }
    

提交回复
热议问题