将 System.out 重定向到 JavaFX 中的 TextArea [英] Redirecting System.out to a TextArea in JavaFX

查看:26
本文介绍了将 System.out 重定向到 JavaFX 中的 TextArea的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还是有同样的问题,修改了主应用代码的来源:http://pastebin.com/fLCwuMVq

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

CoreTest 中肯定有一些东西会阻止 UI,但它会做各种各样的事情(异步 xmlrpc 请求、异步 http 请求、文件 io 等),我尝试将其全部放入 runLater 但它没有帮助.

There must be something in CoreTest that blocks the UI, but its doing all sorts of stuff (async xmlrpc requests, async http requests, file io etc), i tried putting it all into runLater but it doesnt help.

我验证了代码运行并正确生成输出,但 UI 组件无法显示它很长时间

I verified the code runs and produces output correctly but the UI component cant manage to display it for ages

好的,我修好了.我不知道为什么,但没有关于 JavaFX 的指南说过这一点,这非常重要:

OK I fixed it. I don't know why, but no guide about JavaFX said this, and its very important:

始终将您的程序逻辑放在与 Java FX 线程不同的线程中

我在 Swing 的 JTextArea 上使用过这个,但由于某种原因它不适用于 JavaFX.

I had this working with Swing's JTextArea but for some reason it doesn't work with JavaFX.

我尝试调试并在每次写入后执行 .getText() 返回似乎正确写入的字符,但 GUI 中的实际 TextArea 显示没有文本.

I tried debugging and it and doing .getText() after each write returns what seems to be characters written to it properly, but the actual TextArea in GUI shows no text.

我是不是忘了以某种方式刷新它?

Did I forgot to somehow refresh it or something?

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();

还有 Console 类:

import java.io.IOException;
import java.io.OutputStream;

import javafx.scene.control.TextArea;

public 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));
    }

}

注意:这是基于这个答案的解决方案,我删除了我不关心但未修改的部分(除了从 Swing 更改为 JavaFX),它具有相同的结果:数据写入 UI 元素,屏幕上没有显示数据.

Note: this is based on a solution from this answer, I removed bits I didn't care about but unmodified (apart from changing from Swing to JavaFX), it had the same result: data written to the UI element, no data showing on the screen.

推荐答案

您是否尝试过在 UI 线程上运行它?

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));
        }
    });
}

编辑

我认为您的问题是您在 GUI 线程中运行了一些长任务,这将冻结所有内容直到完成.不知道是什么

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();

可以,但如果需要几秒钟,您的 GUI 将不会在这几秒钟内更新.您需要在单独的线程中运行这些任务.

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.

为了记录,这很好用(我已经删除了文件和 CoreTest 位):

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));
        }
    }
}

这篇关于将 System.out 重定向到 JavaFX 中的 TextArea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆