我有一个Java FX 2应用程序,它正在更新几百次。 我得到其中标签部分空白一秒钟的一小部分问题,但这种情况相当频繁。 我该如何解决?
Answer 1:
调用Platform.runLater()数百次第二是不是一个好主意。 我建议你调用Platform.runLater()来更新UI,使得Platform.runLater不被调用> 30倍的第二前一起节流数据源或批量数据的输入速度。
我提起JIRA请求RT-21569 ,以提高对Platform.runLater调用的文件,并考虑在底层平台实现卓越的runLater事件限制系统。
对于配料runLater事件的样品溶液是由理查德·拜尔在此给出的论坛主题 。
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.stage.Stage;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Semaphore;
/**
*
*/
public class BackgroundLoadingApp extends Application {
private ListView<String> listView;
private List<String> pendingItems;
private Semaphore lock = new Semaphore(1);
protected void addItem(String item) throws InterruptedException {
if (Platform.isFxApplicationThread()) {
listView.getItems().add(item);
} else {
// It might be that the background thread
// will update this title quite frequently, and we need
// to throttle the updates so as not to completely clobber
// the event dispatching system.
lock.acquire();
if (pendingItems == null) {
pendingItems = new LinkedList<String>();
pendingItems.add(item);
Platform.runLater(new Runnable() {
@Override public void run() {
try {
lock.acquire();
listView.getItems().addAll(pendingItems);
pendingItems = null;
} catch (InterruptedException ex) {
ex.printStackTrace();
} finally {
lock.release();
}
}
});
} else {
pendingItems.add(item);
}
lock.release();
}
}
/**
* The main entry point for all JavaFX applications.
* The start method is called after the init method has returned,
* and after the system is ready for the application to begin running.
* <p/>
* <p>
* NOTE: This method is called on the JavaFX Application Thread.
* </p>
*
* @param primaryStage the primary stage for this application, onto which
* the application scene can be set. The primary stage will be embedded in
* the browser if the application was launched as an applet.
* Applications may create other stages, if needed, but they will not be
* primary stages and will not be embedded in the browser.
*/
@Override public void start(Stage primaryStage) throws Exception {
listView = new ListView<String>();
primaryStage.setScene(new Scene(listView));
primaryStage.show();
// Start some background thread to load millions of rows as fast as it can. But do
// so responsibly so as not to throttle the event thread
Thread th = new Thread() {
@Override public void run() {
try {
for (int i=0; i<2000000; i++) {
addItem("Item " + i);
if (i % 200 == 0) {
Thread.sleep(20);
}
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
};
th.setDaemon(true);
th.start();
}
public static void main(String[] args) {
launch(args);
}
}
由理查德·拜尔评论来自简称论坛主题复制:
这里是模拟某些产生丰富的数据的长期运行线程的示例。 我发现,这是不是很合作,我的胃口。 如果Thread.sleep代码丢失,但它仍然淹没过程(也许是我处理在这种情况下并发的方式)。 我还发现,如果我降低到“2”沉睡的MS然后我不能抢滚动条拇指和移动它。 我觉得这里的问题是,有在新闻界和拖动事件队列中的鼠标事件,但拖动事件之间的新项目被添加到列表中引起拇指移动,因为这种情况发生的频率比我拖动事件拇指从来没有去的地方我希望它是。 这个我觉得是由于拇指的位置是基于行的数目,不知道该用什么办法来完成,除了应用到油门和批了行,因为我在这里做的添加处理方式。
文章来源: JavaFX 2 blank label refresh issue