javafx.scene.web.WebEvent Java Examples

The following examples show how to use javafx.scene.web.WebEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PokeMateUI.java    From PokeMate with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void start(final Stage stage) throws Exception {
    stage.setTitle("Pokemate UI");
    stage.setOnCloseRequest(t -> {
        Platform.exit();
        System.exit(0);
    });
    //This needs to be set to the resources directory, however, it is not play along nicely.
    mapComponent = new GoogleMapView("/map.html");
    mapComponent.addMapInializedListener(this);
    mapComponent.getWebview().getEngine().setOnAlert((WebEvent<String> event) -> {
    });
    BorderPane bp = new BorderPane();
    bp.setCenter(mapComponent);
    Scene scene = new Scene(bp);
    stage.setScene(scene);
    stage.setWidth(1100);
    stage.setHeight(674);
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    stage.getIcons().add(new Image(classloader.getResourceAsStream("icon.png")));
    stage.show();
}
 
Example #2
Source File: DatePicker.java    From mars-sim with GNU General Public License v3.0 6 votes vote down vote up
private void initPicker(WebView webView) {
  // attach a handler for an alert function call which will set the DatePicker's date property.
  webView.getEngine().setOnAlert(new EventHandler<WebEvent<String>>() {
    @Override public void handle(WebEvent<String> event) {
      try { date.set(jQueryUiDateFormat.parse(event.getData())); } catch (ParseException e) { /* no action required */ }
    }
  });

  // place the webView holding the jQuery date picker inside this node.
  this.getChildren().add(webView);

  // monitor the date for changes and update the formatted date string to keep it in sync.
  date.addListener(new ChangeListener<Date>() {
    @Override public void changed(ObservableValue<? extends Date> observableValue, Date oldDate, Date newDate) {
      dateString.set(dateFormat.format(newDate));
    }
  });

  // workaround as I don't know how to size the stack to the size of the enclosed WebPane's html content.
  this.setMaxSize(330, 280);//307, 241);
}
 
Example #3
Source File: MainApp2.java    From GMapsFX with Apache License 2.0 6 votes vote down vote up
@Override
public void start(final Stage stage) throws Exception {
    mapComponent = new GoogleMapView();
    mapComponent.addMapInitializedListener(this);
    mapComponent.setDisableDoubleClick(true);
    mapComponent.getWebview().getEngine().setOnAlert((WebEvent<String> event) -> {
     //   System.out.println("Event event: " + event);
    });
            
    BorderPane bp = new BorderPane();
    
    bp.setCenter(mapComponent);

    Scene scene = new Scene(bp);
    stage.setScene(scene);
    stage.show();
    
}
 
Example #4
Source File: WebViewDemo.java    From javafxwebview with Apache License 2.0 5 votes vote down vote up
private void createWebView(Stage primaryStage, String page) {
	
	// create the JavaFX webview
	final WebView webView = new WebView();

	// connect the FruitsService instance as "fruitsService" 
	// javascript variable
	connectBackendObject(
			webView.getEngine(),
			"fruitsService", new FruitsService());
	
	// connect the CalculatorService instance as "calculatorService" 
	// javascript variable		
	connectBackendObject(
			webView.getEngine(),
			"calculatorService", new CalculatorService());
	
	// show "alert" Javascript messages in stdout (useful to debug)	
	webView.getEngine().setOnAlert(new EventHandler<WebEvent<String>>(){
		@Override
		public void handle(WebEvent<String> arg0) {
			System.err.println("alertwb1: " + arg0.getData());
		}
	});
	
	// load index.html
	webView.getEngine().load(
			getClass().getResource(page).
			toExternalForm());

	primaryStage.setScene(new Scene(webView));
	primaryStage.setTitle("WebView with Java backend");		
	primaryStage.show();
}
 
Example #5
Source File: Java2JavascriptUtils.java    From javafxwebview with Apache License 2.0 5 votes vote down vote up
private AlertEventHandlerWrapper(
		WebEngine engine, 
		EventHandler<WebEvent<String>> wrappedHandler) {
	
	this.engine = engine;
	this.wrappedHandler = wrappedHandler;
}
 
Example #6
Source File: Java2JavascriptUtils.java    From javafxwebview with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(WebEvent<String> arg0) {
	if (arg0.getData().contains(CONNECT_BACKEND_MAGIC_WORD)) {
		String varname = arg0.getData().substring(
				CONNECT_BACKEND_MAGIC_WORD.length());
		
		connectToWebEngine(engine, varname);
	}
	else if (wrappedHandler != null) wrappedHandler.handle(arg0);
}
 
Example #7
Source File: DemoBrowser.java    From FxDock with Apache License 2.0 4 votes vote down vote up
protected void handleStatusChange(WebEvent<String> ev)
{
	statusField.setText(ev.getData());
}
 
Example #8
Source File: JavaFxWebEngine.java    From GMapsFX with Apache License 2.0 4 votes vote down vote up
public void setOnAlert(EventHandler<WebEvent<String>> eventHandler) {
    webEngine.setOnAlert(eventHandler);
}
 
Example #9
Source File: Java2JavascriptUtils.java    From javafxwebview with Apache License 2.0 4 votes vote down vote up
/**
 * Registers a backend Java object as a Javascript variable.
 * The real connection to the webEngine comes when the javascript performs
 * an special "alert" message by invoking 
 * "alert('__CONNECT__BACKEND__varname')" where varname is the javascript
 * variable we want to make available.
 * 
 * The call to this function has to be performed before the engine loads the
 * first page (where the alert call should take place).
 * 
 * @param webEngine The webEngine to register the new variable.
 * @param varname The name of the variable in javascript.
 * @param backend The Java backend object.
 */
public static void connectBackendObject(
		final WebEngine webEngine,
		final String varname, 
		final Object backend) {
	
	registerBackendObject(webEngine, varname, backend);
	
	// create a onAlertChangeListener. We always want to listen
	// to onAlert events, since via this event, the javascript front-end
	// will send us an special "alert" message asking to connect the 
	// backend object as soon as possible(*). 
	// However, if the programmer also wants to set
	// his own onAlert handler for this web engine, 
	// we will create a handlerwrapper with our
	// behavior plus the programmer's one.
	
	// (*) It was impossible for me to re-connect the backend object
	// when the users navigates from one page to another page where the
	// backend object was also needed. The navigation erases any javascript
	// variables, so the backend has to be reconnected. However,
	// The recommended state change listeners on
	// webEngine were executed too late, after javascript code asking for the
	// backend object is executed, so it was not a solution.
	// The only way I found is to place a custom javacript "signaling" 
	// code to ask Java to reconnect the backend object.
	// The solution was "alert", because we can listen to alert calls from
	// javascript, so via an special "alert" message, we can connect the
	// backend object again.
	// It is not a bad solution, because the programmer has only to inlude
	// a simple additional script (such as "mybackend.js") in the page 
	// before any other scripts uses the backend variable.
	if (!webEnginesWithAlertChangeListener.contains(webEngine)) {
		if (webEngine.getOnAlert() == null) {
			webEngine.setOnAlert(new AlertEventHandlerWrapper(webEngine,
					null));
		}
		
		webEngine.onAlertProperty().addListener(
			new ChangeListener<EventHandler<WebEvent<String>>>() {

				@Override
				public void changed(
						ObservableValue
						<? extends EventHandler<WebEvent<String>>> arg0,
						EventHandler<WebEvent<String>> previous,
						final EventHandler<WebEvent<String>> newHandler) {

					if (!changing) { // avoid recursive calls
						changing = true;
						webEngine.setOnAlert(
							new AlertEventHandlerWrapper(
									webEngine, 
									newHandler));
						changing = false;
					}
				}
		});
	}
	webEnginesWithAlertChangeListener.add(webEngine);
}