Java Reference
In-Depth Information
alertPopup.getContent().add(contentPane);
BorderPane.setAlignment(okButton, Pos.CENTER);
BorderPane.setMargin(okButton, new Insets(10, 0, 10, 0));
return alertPopup;
}
Here are some relevant notes about the
createAlertPopup()
method code:
A
•
String
argument containing the HTML to be displayed is passed into the method.
•
A
Popup
instance is created. A
StackPane
, including a
BorderPane
that has a
Label
containing
the HTML, is added to the
Popup
.
The
•
onAction
handler in the OK button causes the
Popup
to
hide
from view, as you
experienced in Step 34 of the exercise.
Let's move on to the final UI control in the StarterApp program.
Using a
WebView
The
WebView
control is a web browser that you can embed in JavaFX applications. As you experienced in Steps 34
and 35 of the exercise, the
WebView
control shown in Figure
6-14
automatically displays a randomly selected web page
when the tab labeled PasswordField is selected.
In the following snippet from Listing 6-5, the
WebView
created earlier is assigned to the
Tab
's content and
properties are assigned, including the lambda expression that is called when the tab is selected.
Tab webViewTab = new Tab("WebView");
webViewTab.setContent(webView);
webViewTab.setClosable(false);
webViewTab.setOnSelectionChanged(e -> {
String randomWebSite = model.getRandomWebSite();
if (webViewTab.isSelected()) {
webView.getEngine().load(randomWebSite);
System.out.println("WebView tab is selected, loading: "
+ randomWebSite);
}
});
The code in the
onSelectionChanged()
method earlier calls a method in the
StarterAppModel
class to get the
URL of a randomly selected web site. The
getEngine()
method of the
WebView
is then invoked to get the
WebEngine
instance associated with the
WebView
. The
load()
method of the
WebEngine
is invoked, passing a
String
that contains
the randomly selected URL, which causes the
WebView
to display the web page retrieved from that URL. The following
snippet contains the relevant code from the
StarterAppModel
class:
Search WWH ::
Custom Search