问题描述
答案:
我有一个计算目录大小的线程。
I have a thread that calculates the size of a directory.
我使用了walkFileTree。
I use walkFileTree for this.
要获得一些信息,我将实际文件附加到textarea。
To get some info, I append the actuall file to a textarea.
但是当我有很多文件时(例如> 300)我得到了
But when I have a lot of files (ex. > 300) I got
以下是代码:
private void startScheduledExecutorService() {
Thread dt = new Thread(new Runnable() {
public void run() {
try {
taStatus.appendText("Dateien werden ermittelt\n");
Files.walkFileTree(quellOrdner.toPath(), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
size += attrs.size();
files++;
taStatus.appendText(file.toString() + "\n");
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}, "dt");
dt.setDaemon(true);
dt.start();
}
当我创建一个ArrayList并将每个文件添加到它并添加此ArrayList时(一行中的每个条目)到TextArea,它正在工作。
When I create an ArrayList and add each file to it and append this ArrayList (each entry in a row) to the TextArea, it is working.
看起来线程速度有问题吗?
It looks like a problem with the thread speed ?
完整的例外
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at com.sun.javafx.text.PrismTextLayout.addTextRun(PrismTextLayout.java:755)
at com.sun.javafx.text.GlyphLayout.addTextRun(GlyphLayout.java:121)
at com.sun.javafx.text.GlyphLayout.breakRuns(GlyphLayout.java:191)
at com.sun.javafx.text.PrismTextLayout.buildRuns(PrismTextLayout.java:770)
at com.sun.javafx.text.PrismTextLayout.layout(PrismTextLayout.java:1021)
at com.sun.javafx.text.PrismTextLayout.ensureLayout(PrismTextLayout.java:223)
at com.sun.javafx.text.PrismTextLayout.getBounds(PrismTextLayout.java:246)
at javafx.scene.text.Text.getLogicalBounds(Text.java:358)
at javafx.scene.text.Text.impl_computeGeomBounds(Text.java:1168)
at javafx.scene.Node.updateGeomBounds(Node.java:3556)
at javafx.scene.Node.getGeomBounds(Node.java:3509)
at javafx.scene.Node.getLocalBounds(Node.java:3457)
at javafx.scene.Node.updateTxBounds(Node.java:3620)
at javafx.scene.Node.getTransformedBounds(Node.java:3403)
at javafx.scene.Node.updateBounds(Node.java:538)
at javafx.scene.Parent.updateBounds(Parent.java:1706)
at javafx.scene.Parent.updateBounds(Parent.java:1706)
at javafx.scene.Parent.updateBounds(Parent.java:1706)
at javafx.scene.Parent.updateBounds(Parent.java:1706)
at javafx.scene.Parent.updateBounds(Parent.java:1706)
at javafx.scene.Parent.updateBounds(Parent.java:1706)
at javafx.scene.Parent.updateBounds(Parent.java:1706)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2404)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$30(Toolkit.java:314)
at com.sun.javafx.tk.Toolkit$$Lambda$178/156290868.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:313)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:340)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:525)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:505)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$400(QuantumToolkit.java:334)
at com.sun.javafx.tk.quantum.QuantumToolkit$$Lambda$47/104706045.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
at com.sun.glass.ui.gtk.GtkApplication$$Lambda$43/1086508417.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
推荐答案
进入s ame错误,正如kleopatra的评论所述,FX textarea必须在FX线程上访问。
Ran into same error and as kleopatra's comment stated, FX textarea must be accessed on FX thread.
对于简单的情况,请使用 with Java 8 Lambda:
For simple cases, use Platform.runLater with Java 8 Lambda:
javafx.application.Platform.runLater( () -> taStatus.appendText(file.toString() + "\n") );
如果你有很多日志消息,而不是充斥FX线程,最好缓冲它们和更少地更新TextArea。
以下是一个示例:
If you have many log messages, instead of flooding the FX thread it would be better to buffer them and update TextArea less frequently.Here is an example: https://stackoverflow.com/a/31414801/3710098
这篇关于JavaFX将文本附加到TextArea会抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!