JavaFX TextField EventHandler


Question

Currently I have a TextField which is my address bar and a WebView which is my page. When I click ented the TextField doesn't seem to do anything. It's meant to run the loadPage method and set to page to load whatever the user entered into the address bar. Any help would be appreciated.

package javafx_webview;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class Main extends Application {

    WebEngine myWebEngine;

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

    public void start(Stage primaryStage) {
        primaryStage.setTitle("Platinum v1");

        TextField addressBar = new TextField();

        addressBar.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {
                loadPage(event.toString());
            }
        });

        WebView myBrowser = new WebView();
        myWebEngine = myBrowser.getEngine();

        StackPane root = new StackPane();
        root.getChildren().add(myBrowser);
        root.getChildren().add(addressBar);
        primaryStage.setScene(new Scene(root, 640, 480));
        primaryStage.show();
    }

    private void loadPage(String url) {
        try {
            myWebEngine.load(url);
        } catch (Exception e) {
            System.out.println("The URL you requested could not be found.");
        }
    }
}
1
5
5/4/2013 4:46:14 AM

Accepted Answer

Get the url to load from the text of the address bar, not the toString of the action event on the address bar.

final TextField addressBar = new TextField();
addressBar.setOnAction(new EventHandler<ActionEvent>() {
  public void handle(ActionEvent event) {
    myWebEngine.load(addressBar.getText());
  }
});

Also the load is asynchronous, so your exception handler won't work. You need to monitor's the webengine loadworker's exception property to get exceptions from the engine. Also note that a url not found is not necessarily an exception which would be reported, instead a web server will usually return a page for a http 404 error.

Here is a working sample:

import javafx.application.Application;
import javafx.beans.value.*;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.web.*;
import javafx.stage.Stage;

public class Main extends Application {
  private WebEngine myWebEngine;

  public void start(Stage stage) {
    stage.setTitle("Platinum v1");

    final TextField addressBar = new TextField();

    addressBar.setOnAction(new EventHandler<ActionEvent>() {
      public void handle(ActionEvent event) {
        myWebEngine.load(addressBar.getText());
      }
    });

    WebView myBrowser = new WebView();
    myWebEngine = myBrowser.getEngine();
    myWebEngine.getLoadWorker().exceptionProperty().addListener(new ChangeListener<Throwable>() {
      @Override public void changed(ObservableValue<? extends Throwable> observableValue, Throwable oldException, Throwable exception) {
        System.out.println("WebView encountered an exception loading a page: " + exception);
      }
    });

    VBox root = new VBox();
    root.getChildren().setAll(
        addressBar,
        myBrowser
    );
    stage.setScene(new Scene(root));
    stage.show();
  }

  public static void main(String[] args) { launch(args); }
}
6
5/4/2013 8:37:01 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon