0

I'm using a listener for the state of the web engine and it's working fine. Also the document is filled at this time, because the State SUCCEEDED is reached.

The State Listener:

public class WebViewListener implements ChangeListener<Worker.State> {
   public void changed(ObservableValue observable, State oldState, State newState) {
      if (newState == State.SUCCEEDED) {
         String style = "";

         try {
            style = cssModel.getRuntimeCSSFileAsString();
            htmlModel.setDocument(view.getWebEngine().getDocument());
            htmlModel.setStyleAttribute(style);
         } catch (IOException e) {
            e.printStackTrace();
         } catch (HTMLDocumentNullException e) {
            e.printStackTrace();
         }
    }
}

}

My problem is, that I use another listener on a toggle button, which I manually fire by the method selectToggle() at an earlier time (in the constructor of another view), where the document model of the web engine is not yet set by the state listener, because the State SUCCEED is not reached before the JavaFX stage pops up.

So now I need a solution, how I can listen on the JavaFX Stage, when the UI is rendered. Because at this time, the State listener reacts and after that I could call the setInitialState() Method and work with the document model of the web engine.

The code of selecting the ToggleButton:

public void setInitialState() {
   screenGroup.selectToggle(rbMasterScreen);
   elementGroup.selectToggle(btnButton);
}
  • 1
    I really don't understand what do you want to do. If you listen to to State, how it is possible that the document is null? And what with the ToggleButton? Or post some code sample, otherwise really difficult to help. – DVarga Apr 07 '16 at 16:39

1 Answers1

0

In this case, the most primitive solution: Introduce a SimpleBoolean property e.g.

SimpleBooleanProperty webEngineLoadedProperty = new SimpleBooleanProperty(false);

Then in the end of:

public void changed(ObservableValue observable, State oldState, State newState) {
    if (newState == State.SUCCEEDED) {
        ...
        webEngineLoadedProperty.set(true);
    }
}

And listen to the change:

webEngineLoadedProperty.addListener(new ChangeListener<Boolean>() {

    @Override
    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
        setInitialState();  
    }
});
DVarga
  • 21,311
  • 6
  • 55
  • 60