Redirecting System.out to a TextArea in JavaFX

前端 未结 1 1194
无人共我
无人共我 2020-12-09 17:23

Update:

Still having the same issue, revised source of main app code: http://pastebin.com/fLCwuMVq

There must be something in CoreTest that bl

相关标签:
1条回答
  • 2020-12-09 17:46

    Have you tried running it on the UI Thread?

    public void write(final int i) throws IOException {
        Platform.runLater(new Runnable() {
            public void run() {
                output.appendText(String.valueOf((char) i));
            }
        });
    }
    

    EDIT

    I think your problem is that your run some long tasks in the GUI thread, which is going to freeze everything until it completes. I don't know what

    CoreTest t = new CoreTest(installPath);
    t.perform();
    

    does, but if it takes a few seconds, your GUI won't update during those few seconds. You need to run those tasks in a separate thread.

    For the record, this works fine (I have removed the file and CoreTest bits):

    public class Main extends Application {
    
        @Override
        public void start(Stage primaryStage) throws IOException {
    
            TextArea ta = TextAreaBuilder.create().prefWidth(800).prefHeight(600).wrapText(true).build();
            Console console = new Console(ta);
            PrintStream ps = new PrintStream(console, true);
            System.setOut(ps);
            System.setErr(ps);
            Scene app = new Scene(ta);
    
            primaryStage.setScene(app);
            primaryStage.show();
    
            for (char c : "some text".toCharArray()) {
                console.write(c);
            }
            ps.close();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    
        public static class Console extends OutputStream {
    
            private TextArea output;
    
            public Console(TextArea ta) {
                this.output = ta;
            }
    
            @Override
            public void write(int i) throws IOException {
                output.appendText(String.valueOf((char) i));
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题