Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
89 views
in Technique[技术] by (71.8m points)

java - How can I make this method update the GUI within my loop?

I'm making a program that runs a few cmd commands (USMT and file transfer)

It's working fine, but I only get the last line from the cmd in my text box and only after it completes the operation. I want it to print what the cmd is outputting in real time.

 public void load() throws IOException {
    ProcessBuilder builder = new ProcessBuilder(
            "cmd.exe", "/c", "cd "C:\usmt" && loadstate.bat");
    builder.redirectErrorStream(true);
    Process p = builder.start();
    BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;

    while (true) {

        line = r.readLine();
        if (line == null) { break; }

        cOut.setText(line);
        System.out.println(line);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Cause...

The basic cause of your problem is the fact that you are blocking the Event Dispatching Thread, this is preventing the UI from been updated until AFTER the command has executed.

Swing is a single threaded framework, meaning that you shouldn't execute blocking or long running code from within the context of the EDT. Swing is also NOT thread safe, meaning that you should never modify the state of the UI from outside of the context of the EDT.

See Concurrency in Swing for more details

Solution...

To solve this you have two basic options. You could use a Thread, but then you become responsible for ensuring that any and all updates to the UI are synchronised to the context of the EDT, or you could use a SwingWorker, for example...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Runner {

    public static void main(String[] args) {
        new Runner();
    }

    public Runner() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTextArea ta;

        public TestPane() {
            setLayout(new BorderLayout());
            ta = new JTextArea(25, 80);
            add(new JScrollPane(ta));

            JButton execute = new JButton("Make it so");
            execute.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    execute.setEnabled(false);
                    CommandWorker worker = new CommandWorker(ta, "cmd.exe", "/c", "cd "C:\usmt" && loadstate.bat");
                    worker.addPropertyChangeListener(new PropertyChangeListener() {
                        @Override
                        public void propertyChange(PropertyChangeEvent evt) {
                            switch (evt.getPropertyName()) {
                                case "state":
                                    SwingWorker work = (SwingWorker) evt.getSource();
                                    switch (worker.getState()) {
                                        case DONE: {
                                            try {
                                                worker.get();
                                            } catch (InterruptedException | ExecutionException ex) {
                                                ex.printStackTrace();;
                                                JOptionPane.showMessageDialog(TestPane.this, "Execution of command failed: " + ex.getMessage());
                                            } finally {
                                                execute.setEnabled(true);
                                            }
                                        }
                                        break;
                                    }
                                    break;
                            }
                        }
                    });
                    worker.execute();
                }
            });

            add(execute, BorderLayout.SOUTH);
        }

    }

    public static class CommandWorker extends SwingWorker<List<String>, String> {

        private JTextArea ta;
        private List<String> commands;

        public CommandWorker(JTextArea ta, List<String> commands) {
            this.ta = ta;
            this.commands = commands;
        }

        public CommandWorker(JTextArea ta, String... commands) {
            this(ta, Arrays.asList(commands));
        }

        @Override
        protected List<String> doInBackground() throws Exception {
            List<String> output = new ArrayList<>(25);
            ProcessBuilder builder = new ProcessBuilder(commands);
            builder.redirectErrorStream(true);

            Process p = builder.start();
            try (BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
                String line = null;
                while ((line = r.readLine()) != null) {
                    output.add(line);
                    publish(line);
                }
            }
            return output;
        }

        @Override
        protected void process(List<String> chunks) {
            for (String text : chunks) {
                ta.append(text);
                ta.append("
");
            }
        }

    }

}

See Worker Threads and SwingWorker for more details


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...