当前位置: 首页>>代码示例>>Java>>正文


Java WebEngine.getOnAlert方法代码示例

本文整理汇总了Java中javafx.scene.web.WebEngine.getOnAlert方法的典型用法代码示例。如果您正苦于以下问题:Java WebEngine.getOnAlert方法的具体用法?Java WebEngine.getOnAlert怎么用?Java WebEngine.getOnAlert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javafx.scene.web.WebEngine的用法示例。


在下文中一共展示了WebEngine.getOnAlert方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: connectBackendObject

import javafx.scene.web.WebEngine; //导入方法依赖的package包/类
/**
 * 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);
}
 
开发者ID:jamf,项目名称:HealthCheckUtility,代码行数:75,代码来源:JavaToJavascriptBridge.java

示例2: initEngine

import javafx.scene.web.WebEngine; //导入方法依赖的package包/类
private void initEngine(){
    engine = new WebEngine();
    engine.setOnAlert(this);
    correctHandlerReturned = (this == engine.getOnAlert());
}
 
开发者ID:teamfx,项目名称:openjfx-8u-dev-tests,代码行数:6,代码来源:AlertTest.java

示例3: connectBackendObject

import javafx.scene.web.WebEngine; //导入方法依赖的package包/类
/**
 * 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);
}
 
开发者ID:lipido,项目名称:javafxwebview,代码行数:75,代码来源:Java2JavascriptUtils.java


注:本文中的javafx.scene.web.WebEngine.getOnAlert方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。