I am trying to do a WebView
that surf websites. When the program is inactive for few seconds, the WebView
will load back to the beginning of the website.
JavaFXApplication13.java
@Override
public void start(Stage stage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
stage.setScene(new Scene(root));
stage.show();
Duration duration = Duration.seconds(10);
PauseTransition transition = new PauseTransition(duration);
transition.setOnFinished(evt -> inactive());
stage.addEventFilter(InputEvent.ANY, evt -> transition.playFromStart());
transition.play();
}
private void inactive(){
//to investigate if it inactive
System.out.println("Inactive once");
//load other website when inactive ?
}
MainController.java
@FXML
WebView webview;
private WebEngine webEngine;
@Override
public void initialize(URL url, ResourceBundle rb) {
webEngine = webview.getEngine();
webview.setContextMenuEnabled(false);
webEngine.load("http://www.google.com");}
This is my main.fxml https://drive.google.com/open?id=1_WnWhkbjaX1s2Y2jI0ojIvc2aQC1njJh
You could access the webview
from the controller
FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
Parent root = loader.load();
MainController controller = loader.getController();
controller.loadDefaultSite();
And in the MainController
make a Function
public void loadDefaultSite() {
webEngine.load("http://www.google.com");
}
Example:
private MainController controller;
@Override
public void start(Stage stage) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
Parent root = loader.load();
controller = loader.getController();
stage.setScene(new Scene(root));
stage.show();
Duration duration = Duration.seconds(10);
PauseTransition transition = new PauseTransition(duration);
transition.setOnFinished(evt -> inactive());
stage.addEventFilter(InputEvent.ANY, evt -> transition.playFromStart());
transition.play();
}
private void inactive(){
//check if site is already default...
controller.loadDefaultSite();
}