更新:
仍然有同样的问题,修改了主要应用程序代码的来源:
http://pastebin.com/fLCwuMVq
CoreTest中必须有一些阻止用户界面的东西,但是它可以做各种各样的东西(异步xmlrpc请求,异步http请求,文件io等),我试着把它全部放在runLater中,但是并没有帮助.
更新2:
我验证了代码运行并正确生成输出,但UI组件无法管理显示它的年龄
更新3:
好的我修好了我不知道为什么,但是没有关于JavaFX的指南说这个,它的非常重要:
始终将程序逻辑放在与Java FX线程不同的线程中
我有这个工作与Swing的JTextArea,但由于某些原因,它不适用于JavaFX.
我尝试调试,并在.getText()之后每次写入返回似乎是正确写入的字符,但在GUI中的实际TextArea显示没有文本.
我忘了刷新一下吗?
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));
}
}
注意:这是基于this answer的解决方案,我删除了我不关心的位,但没有修改(除了从Swing更改为JavaFX),它具有相同的结果:数据写入UI元素,没有数据显示在屏幕.
解决方法
你是否尝试在UI线程上运行它?
public void write(final int i) throws IOException {
Platform.runLater(new Runnable() {
public void run() {
output.appendText(String.valueOf((char) i));
}
});
}
编辑
我认为你的问题是你在GUI线程中运行一些长时间的任务,这将冻结一切,直到完成.我不知道是什么
CoreTest t = new CoreTest(installPath); t.perform();
但是,如果需要几秒钟,您的GUI将不会在几秒钟内更新.您需要在单独的线程中运行这些任务.
为了记录,这工作正常(我删除了文件和CoreTest位):
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));
}
}
}