5

I have a Java Swing app which I'm investigating if it is even possible to port to JavaFX. The app is a development environment and simulator for an internally used scripting language. The interesting thing about it is you can set breakpoints for this scripting language and step through it, like any programmer would expect for a language.

Now because the language in the simulator is interpreted, deep within the execution of the interpreter, when it hits a breakpoint, it can pop back to the gui with a Java Swing SecondaryLoop class. So when the breakpoint is hit, it calls secondaryLoop.enter(). The gui is then active for the user to inspect variables and gui components are active. When the user hits "Continue" in the program, it calls secondaryLoop.exit() to continue execution of the interpreter. It wouldn't really be feasible for the interpreter to unwind it's entire state to go back to the primary loop, and then take up where it left off at exactly the same point. That's why the SecondaryLoop is invaluable in making it work.

Is this possible in JavaFX?

1 Answer 1

2

Yes, it's possible. You need to use the enterNestedEventLoop and exitNestedEventLoop methods (they are inside the com.sun.javafx.tk.Toolkit class). See this usage example:

// Make sure to import the FX Toolkit first
import com.sun.javafx.tk.Toolkit;

// This object will be used as a unique identifier to the nested loop (to
// block the execution of the thread until exitNestedEventLoop is called)
final Object loopLock = new Object();

// Simulate a long process thread (DB call, download, etc)
Thread longProcess = new Thread(new Runnable()
{
    @Override
    public void run()
    {
        // Sleep for 12 seconds to simulate a long process
        try
        {
            Thread.sleep(12000);
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        // Setup a result to pass back to the enterNestedLoop() caller
        String result = "Result of this long process";

        // We are now done. Call exitNestedEventLoop() to unblock 
        // the enterNestedLoop() caller. This needs to run from
        // the FX Thread so use Platform.runLater()
        Runnable fxRunner = new Runnable()
        {
            public void run()
            {
                try
                {
                    Toolkit.getToolkit().exitNestedEventLoop(loopLock,
                            result);
                } catch (Throwable t)
                {
                    t.printStackTrace();
                }
            }
        };
        Platform.runLater(fxRunner);
    }
});

// Start that long process from the FX Thread
longProcess.start();
// The next call will block until exitNestedEventLoop is called, however 
// the FX Thread will continue processing UI requests
Object result = Toolkit.getToolkit().enterNestedEventLoop(loopLock);
// Next statement will print: "Result of this long process"
System.out.println("Result is: " + result);

Now, before you use this be warned of two important things:

  1. The com.sun.javafx.tk.Toolkit class is not part of the public API, so Oracle reserves the right of removing it without notice. I've been using it just fine from Java 7 to 8u51 so they could stay there forever, change package/names or disappear completely (unlikely).

  2. Nested loops (and Swing's secondary loops) are great for flexibility and small applications but overusing them often comes with a price. Nesting to many loops (huge stack trace) will often cause "strange" behaviour in your applications since initial parts of your code might end up waiting four or five things ahead completely unrelated to them. I've seen FX nested loops causing "empty" exceptions in FX WebEngine executeScript() calls and duplicating keyboard preprocessing (when pairing FX+Swing) among other problems.

That said I would recommend using the javafx.concurrent.Task instead (if it makes sense). Using the Task class will require a bit more effort but I think it's the correct way of doing things and will probably save you lots of maintenance time.

For extra reference about the FX Task class see this great article: http://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm

UPDATE: enterNestedEventLoop and exitNestedEventLoop will be part of the Java 9 public API (Platform class), more info in JDK-8090865

Hope this helps!

6
  • 1
    According to JEP 253: "With the forthcoming release of Java 9, and in particular with the introduction of strong boundaries between modules in Project Jigsaw, developers will find that their code will no longer compile or run since the com.sun.* packages will no longer be accessible." Also: "So if starts with com.sun.*, it won’t exist on any non-Oracle JDK. And if it belongs to one of those packages and is not annotated with @jdk.Exported, it will be unaccessible from Java 9 on."
    – jewelsea
    Aug 18, 2015 at 4:49
  • 1
    Thanks for the extra information jewelsea! Indeed this is always the risk of using sun packages. However, I would think that the FX Toolkit will end up being part of the public API, anyway if anyone is interested in following that see this openJDK request
    – JavierJ
    Aug 19, 2015 at 14:40
  • Your example does not work. exitNestedEventLoop throws an exception that it is not called on the platform thread. :-/
    – Vampire
    Apr 26, 2016 at 15:25
  • @Vampire exitNestedEventLoop and enterNestedEventLoop need to be run in the FX Thread. You can achieve this with a Platform.runLater() call
    – JavierJ
    Sep 12, 2016 at 21:29
  • @JavierJ I know that. But your codes does not do it. You call it directly in a new Thread and that wont work. ;-)
    – Vampire
    Sep 13, 2016 at 8:47

Your Answer

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

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