Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » e(fx)clipse » Asynchronous loading
Asynchronous loading [message #1250661] Wed, 19 February 2014 08:14 Go to next message
Pablo Gomez is currently offline Pablo Gomez
Messages: 23
Registered: October 2013
Junior Member
Hi All!,

When a part is loaded asynchronously, the initialize method of the controller is NOT executed by the UI Thread, from our point of view this gives more disadvantages than advantages

if in the controller you have something like


       @FXML
       TextField myTextField;

       @Override
       public void initialize(URL url, ResourceBundle bundle) {
           myTextField.setTooltip(new javafx.scene.control.Tooltip("This textfield requires a tooltip"));
       }


That code leads to an exception
[...]
Caused by: java.lang.IllegalStateException: Not on FX application thread; currentThread = pool-4-thread-4
 at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:210)
 at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:393)
[...]


We understand the advantages of loading a SceneGraph in another thread and once it is loaded it is attached to a living scene by the UI Thread. However, we don't see advantages of running the initialized method by Non-UI. That forces the user to worry about what JavaFX API calls can use in the controller and which ones not. For instance Tooltip extends PopupControl which is a living scene graph and therefore it requires UI Thread which leads to an exception when we are loading async.


The FXMLLoader.java basically does not worry about the Thread that executes the initialize:
// FXMLLoader Line 2546:
if (controller != null) {
                if (controller instanceof Initializable) {
                    ((Initializable)controller).initialize(location, resources);
                }  // if



so why not do it in this way? so we hand over the UI Thread to the user

if (controller != null) {
  if (controller instanceof Initializable) {
    Runnable runnable = new Runnable() {
      public void run() { 
        ((Initializable)controller).initialize(location, resources);
      }
    };
 
    if (Platform.isFxApplicationThread()) {
      runnable.run(); // execute it now
    } else {
      Platform.runLater(runnable); 
    }                 
  }
  [...]



best
Pablo
Re: Asynchronous loading [message #1250675 is a reply to message #1250661] Wed, 19 February 2014 08:26 Go to previous messageGo to next message
Pablo Gomez is currently offline Pablo Gomez
Messages: 23
Registered: October 2013
Junior Member
perhaps this would be an improvement in the jdk... because as far as I see this is all in the JDK Codebase.
what do you think?
Re: Asynchronous loading [message #1250707 is a reply to message #1250661] Wed, 19 February 2014 09:06 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5259
Registered: July 2009
Senior Member
.... because this is all JavaFX code you need to discuss this with the
JavaFX people but why do you think this does not make to invoke the
controller outside the UI-Thread?

I would expect a class to execute everything on the thread I'm calling
it - your code would even not block until so I'd get back an SceneGraph
which is not connected to the controller until the Runnable is run,
leading to many raise conditions.

So I don't see big chances to get something like this into the codebase.

Tom

On 19.02.14 14:14, Pablo Gomez wrote:
> Hi All!,
>
> When a part is loaded asynchronously, the initialize method of the
> controller is NOT executed by the UI Thread, from our point of view this
> gives more disadvantages than advantages
>
> if in the controller you have something like
>
>
>
> @FXML
> TextField myTextField;
>
> @Override
> public void initialize(URL url, ResourceBundle bundle) {
> myTextField.setTooltip(new javafx.scene.control.Tooltip("This
> textfield requires a tooltip"));
> }
>
>
> That code leads to an exception
>
> [...]
> Caused by: java.lang.IllegalStateException: Not on FX application
> thread; currentThread = pool-4-thread-4
> at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:210)
> at
> com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:393)
>
> [...]
>
>
> We understand the advantages of loading a SceneGraph in another thread
> and once it is loaded it is attached to a living scene by the UI Thread.
> However, we don't see advantages of running the initialized method by
> Non-UI. That forces the user to worry about what JavaFX API calls can
> use in the controller and which ones not. For instance Tooltip extends
> PopupControl which is a living scene graph and therefore it requires UI
> Thread which leads to an exception when we are loading async.
>
>
> The FXMLLoader.java basically does not worry about the Thread that
> executes the initialize:
>
> // FXMLLoader Line 2546:
> if (controller != null) {
> if (controller instanceof Initializable) {
> ((Initializable)controller).initialize(location,
> resources);
> } // if
>
>
>
> so why not do it in this way? so we hand over the UI Thread to the user
>
>
> if (controller != null) {
> if (controller instanceof Initializable) {
> Runnable runnable = new Runnable() {
> public void run() {
> ((Initializable)controller).initialize(location, resources);
> }
> };
>
> if (Platform.isFxApplicationThread()) {
> runnable.run(); // execute it now
> } else {
> Platform.runLater(runnable); } }
> [...]
>
>
>
> best
> Pablo
>
Re: Asynchronous loading [message #1250728 is a reply to message #1250707] Wed, 19 February 2014 09:37 Go to previous messageGo to next message
Pablo Gomez is currently offline Pablo Gomez
Messages: 23
Registered: October 2013
Junior Member
yes I would also expect also that, but then the problem when is loaded async is that certain api calls like setting a tooltip must be executed in a runlater. And I think initialized method was not intended by the JavaFX jdk to be executed async this is a feature provided by efxclipse

Re: Asynchronous loading [message #1250739 is a reply to message #1250728] Wed, 19 February 2014 09:51 Go to previous message
Pablo Gomez is currently offline Pablo Gomez
Messages: 23
Registered: October 2013
Junior Member
ok forget about that, we have introduced the async loading
Previous Topic:e(fx)clipse - 0.9.0 Endgame
Next Topic:Application building error
Goto Forum:
  


Current Time: Thu Jul 31 11:39:17 EDT 2014

Powered by FUDForum. Page generated in 0.01675 seconds