Detect URL Changes in Javafx Web Browsing

In JavaFx webview, I am trying to detect a change in the url.

I have this method in the class:

     public Object urlchange(){

     engine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
     @Override
     public void changed(ObservableValue ov, State oldState, State newState) {

           if (newState == Worker.State.SUCCEEDED) {
                 return engine.getLocation()); 
           }

     }
     });    

     }

and I'm trying to use it for an object called loginbrowser, for example:

System.out.print(loginbrowser.urlchange());

Do you see what I did wrong?

+4
source share
1 answer

(part) what are you doing wrong

The code you provided in your question does not even compile. The modified ChangeListener method is a void function, it cannot return any value.

, - . , - - , ( JavaFX, ), , ( , ).

(),

location -. :

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.web.*;
import javafx.stage.Stage;

public class LocationViewer extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        Label location = new Label();

        WebView webView = new WebView();
        WebEngine engine = webView.getEngine();
        engine.load("http://www.fxexperience.com");

        location.textProperty().bind(engine.locationProperty());

        Scene scene = new Scene(new VBox(10, location, webView));
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

, - ( , ). , , WebView, :

import javafx.application.Application;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.web.*;
import javafx.stage.Stage;

public class LocationAfterLoadViewer extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        Label location = new Label();

        WebView webView = new WebView();
        WebEngine engine = webView.getEngine();
        engine.load("http://www.fxexperience.com");

        engine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
            if (Worker.State.SUCCEEDED.equals(newValue)) {
                location.setText(engine.getLocation());
            }
        });

        Scene scene = new Scene(new VBox(10, location, webView));
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

, , , , , , , , , - .

url ? , , .

location.textProperty().addListener((observable, oldValue, newValue) -> {
    // perform required action.
});
+5

All Articles