有没有办法在不是“JavaFX应用程序线程”的线程中创建javafx.scene.control.Tooltip?
出于性能考虑,我在一个线程中准备了一些视觉元素并将它们保存在内存中。当我需要它们时,我使用应用程序线程来显示它们。但 new Tooltip() 似乎只需要应用程序线程。
最佳答案
is there a way how to create javafx.scene.control.Tooltip in thread which is not "JavaFX Application Thread"?
对于 JavaFX 2.2 - 否。
请参阅此 JavaFX 问题跟踪表:Tooltip of Tab (and other controls) cannot be set of FXApplicationThread .
票证中有一个建议的解决方法来创建 Tooltip在 Platform.runLater打电话。
package fxthread_25127_wrongthread;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.control.Tooltip;
import javafx.stage.Stage;
/**
*
* @author mickf
*/
public class Main extends Application {
Tooltip tooltip;
public static void main(String[] args) {
Application.launch(args);
}
@Override public void init() throws Exception {
Platform.runLater(new Runnable() {
@Override public void run() {
tooltip = new Tooltip("Top tip : use runLater()");
}
});
/*
* this will cause an ISE
*/
//tooltip = new Tooltip("flip");
}
@Override public void start(Stage s) {
System.exit(0);
}
}
来自 javadoc 的 Platform.runLater 如何工作的描述:
Run the specified Runnable on the JavaFX Application Thread at some unspecified time in the future. This method, which may be called from any thread, will post the Runnable to an event queue and then return immediately to the caller. The Runnables are executed in the order they are posted. A runnable passed into the runLater method will be executed before any Runnable passed into a subsequent call to runLater.
关于multithreading - 在非 JavaFX 应用程序线程上创建新的工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13864182/