當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。