本文整理匯總了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);
}
示例2: initEngine
import javafx.scene.web.WebEngine; //導入方法依賴的package包/類
private void initEngine(){
engine = new WebEngine();
engine.setOnAlert(this);
correctHandlerReturned = (this == engine.getOnAlert());
}
示例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);
}