2

is there a way how to create javafx.scene.control.Tooltip in thread which is not "JavaFX Application Thread"?

Because of performance I prepare some visual elements in one thread and keep them in memory. And when I need them I use Application Thread for showing them. But new Tooltip() seems to require Application Thread only.

1 Answer 1

2

is there a way how to create javafx.scene.control.Tooltip in thread which is not "JavaFX Application Thread"?

For JavaFX 2.2 - No.

See this JavaFX issue tracker tiecket: Tooltip of Tab (and other controls) cannot be set of FXApplicationThread.

There is a suggested workaround in the ticket to create the Tooltip in a Platform.runLater call.

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);
    }
} 

A description of how Platform.runLater works from it's javadoc:

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.

2
  • Thanks, and does the runLater method block the current thread?
    – dpelisek
    Dec 14, 2012 at 14:20
  • No, the current thread is not blocked. Updated answer to include documentation for Platform.runLater.
    – jewelsea
    Dec 18, 2012 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.