Java Reference
In-Depth Information
When creating the
ManipulatingHtmlContent
class for this example, you
need two instance variables:
url
and
refreshCountdown
. The
url
variable will
be assigned to the RSS URL web address from step 4. The
refreshCountdown
variable of type
int
is assigned the value of
60
to denote the time in seconds until a
refresh or another retrieval of the weather information takes place.
Like all the JavaFX examples inside of the
start()
method, you begin by creat-
ing the
Scene
object for the initial main content region. Next, you create a
javafx.scene.web.WebEngine
instance by passing in the
url
into the con-
structor. The
WebEngine
object will asynchronously load the web content from Ya-
hoo!'s weather service. Later you will learn about the callback method responsible for
handling the content when the web content is done loading. The following code line
creates and loads an URL web address using a
WebEngine
object:
final WebEngine webEngine = new WebEngine(url);
After you create a
WebEngine
object, you'll create an HTML document that will
act as a template when the web content is successfully loaded. Although the code con-
tains HTML markup tags in Java code, which violates the principles of the separation
of concerns, this example inlines the HTML by concatenating string values for brevity.
To maintain proper MVC-style separation, you may want to create a separate file con-
taining your HTML content with substitution sections for data that will change over
time. The code snippet that follows is the start of the creation of a template used to dis-
play weather information:
StringBuilder template = new StringBuilder();
template.append("<head>\n")
.append("<style type=\"text/css\">body
{background-color:#b4c8ee;}</style>\n")
.append("</head>\n")
.append("<body id='weather_background'>");
Once you have created the web page by concatenating strings, you create a
We-
bView
object instance, which is a displayable graph node that will be responsible for
rendering the web page. Remember in Recipe 17-2 you learned that a
WebView
has its
own instance of a
WebEngine
. Knowing this fact, you only use the
WebView
node
to render the assembled HTML web page, not to retrieve the XML weather information
via an URL. In other words, the
WebEngine
object is responsible for retrieving the
XML from Yahoo!'s weather service to be parsed and then fed into the
WebView
ob-