-
-
Save ctnels/f07a2382451770eb0704428b7a5cbbd1 to your computer and use it in GitHub Desktop.
A simple JavaFX WebView sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WebTesting extends Application{ | |
public static void main(String[] args){ | |
launch(args); | |
} | |
@Override | |
public void start(Stage stage) throws Exception { | |
VBox vbox = new VBox(10); | |
vbox.setAlignment(Pos.CENTER_LEFT); | |
HBox hbox = new HBox(10); | |
hbox.setAlignment(Pos.CENTER_LEFT); | |
TextField textField = new TextField(); | |
Button button = new Button("Load"); | |
hbox.getChildren().addAll(textField, button); | |
vbox.getChildren().add(hbox); | |
ProgressBar progressBar = new ProgressBar(0); | |
vbox.getChildren().add(progressBar); | |
ScrollPane scrollPane = new ScrollPane(); | |
WebView webView = new WebView(); | |
WebEngine webEngine = webView.getEngine(); | |
scrollPane.setContent(webView); | |
scrollPane.getStyleClass().add("noborder-scroll-pane"); | |
scrollPane.setStyle("-fx-background-color: white"); | |
scrollPane.setFitToWidth(true); | |
scrollPane.setFitToHeight(true); | |
vbox.getChildren().add(webView); | |
button.setOnAction(buttonAction(textField, progressBar, webEngine, webView)); | |
Scene scene = new Scene(vbox); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
private EventHandler<ActionEvent> buttonAction(final TextField textField, | |
final ProgressBar progressBar, | |
final WebEngine webEngine, | |
final WebView webView) { | |
return new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent event) { | |
String route = textField.getText(); | |
System.out.println("Loading route: " + route); | |
progressBar.progressProperty().bind(webEngine.getLoadWorker().progressProperty()); | |
webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() { | |
@Override | |
public void changed(ObservableValue<? extends State> value, | |
State oldState, State newState) { | |
if(newState == State.SUCCEEDED){ | |
System.out.println("Location loaded + " + webEngine.getLocation()); | |
} | |
} | |
}); | |
webEngine.load(route); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment