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

查看:1428
本文介绍了将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请求,async 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不同的线程中外汇线程

我使用了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();

控制台类:

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天全站免登陆