Results 1 to 2 of 2
  1. #1
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default WebEngine callback

    JavaFX offers the WebEngine that shall load webpages in the background and offers callbacks when the status has changed. Somehow I have issues with the callback, maybe you guys can take a look at the code below.
    The first part has the WebEngine portion, it establishes the callback and the state changes twice. But then it gets stuck somewhere and it seems it never changes its state to SUCCEEDED.
    The second portion (commented out for now) loads the same URL and seems to work. In order to compare the time you have to comment out the first portion and un-comment the second portion.
    Any help is appreciated.
    p.s.: i you get some strange messages with the second portion in the comment section: this is a bug to be resolved in Java 9.

    Java FX Code:
    package webEngine;
    
    import javafx.application.Application;
    import javafx.beans.value.ChangeListener;
    import javafx.beans.value.ObservableValue;
    import javafx.concurrent.Worker;
    import javafx.concurrent.Worker.State;
    import javafx.scene.Scene;
    import javafx.scene.web.WebEngine;
    import javafx.scene.web.WebView;
    import javafx.stage.Stage;
    
    public class webEngineMain extends Application {
    
    	@Override
    	public void start(Stage primaryStage) {
    		
    		//webengine process
    		final WebEngine webEngine = new WebEngine();			
    		webEngine.getLoadWorker().stateProperty().addListener(
    				new ChangeListener<State>(){
    					@Override
    					public void changed(
    							ObservableValue observable,
    							State oldValue, State newValue) {
    						System.out.println("state change from " + oldValue.toString() + " to " + newValue.toString());
    						if(newValue==State.SUCCEEDED){
    							System.out.println("document finished loading");
    						}
    					}
    				}
    				);
    		webEngine.load("http://www.java-forums.org/forum.php");
    		System.out.println("loading started");
    		/*
    		//webview process
    		WebView webview = new WebView();
    		webview.getEngine().load("http://www.java-forums.org/forum.php");		
    		Scene scene = new Scene(webview, 800,200);
    		primaryStage.setScene(scene);
    		primaryStage.show();
    		*/
    	}
    
    	public static void main(String[] args) {
    		launch(args);
    	}
    }

  2. #2
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: WebEngine callback

    I found a solution and wanted to wrap up this thread:
    the Worker Interface States can be found here

    The State of the Worker changes to SUCCEEDED if you have a WebView and you actually show the webpage on the scene. I am not sure why the snippet above did not change the State to SUCCEEDED, I would guess that the main thread exited (ahead of the background thread) since we did not show anything on any stage.

    I am not sure how easy the Mods are about posting large amounts of code, but I can post a compiling and working code of a fxml browser that shows the status changes, just let me know.

Similar Threads

  1. Callback Methods
    By Nazneen Ali in forum New To Java
    Replies: 4
    Last Post: 10-28-2013, 10:33 AM
  2. Non blocking callback method
    By Bibhukalyana in forum New To Java
    Replies: 4
    Last Post: 04-11-2013, 06:53 PM
  3. use api without callback url?
    By Seiya0890 in forum New To Java
    Replies: 0
    Last Post: 03-02-2013, 05:02 PM
  4. query with a callback?
    By Filobel in forum JDBC
    Replies: 4
    Last Post: 01-20-2012, 09:33 AM
  5. WebEngine find text
    By PhQ in forum JavaFX
    Replies: 6
    Last Post: 09-15-2011, 08:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •