Java Reference
In-Depth Information
Callback<Class<?>, Object> getControllerFactory()
•
void setControllerFactory(Callback<Class<?>, Object> controllerFactory)
•
Charset getCharset()
•
void setCharset(Charset charset)
•
ClassLoader getClassLoader()
•
void setClassLoader(ClassLoader classLoader)
As you can see from this list, the
location
,
resources
,
builderFactory
,
controllerFactory
, and the
charset
can also be set after the
FXMLLoader
is constructed. In addition, we can get and set the
root
,
controller
,
classLoader
, and get the
namespace
of the
FXMLLoader
. The
root
is relevant only if the FXML file uses
fx:root
as its
root element, in which case
setRoot()
must be called before the FXML file is loaded. We go into more detail about
the usage of
fx:root
in the next section. The
controller
needs to be set before the FXML file is loaded only if the
fx:controller
attribute is not present in the top-level element in the FXML file. The
classLoader
and
namespace
are
mostly used internally by the
FXMLLoader
and usually are not called by user code.
The actual loading of the FXML file happens when one of the
load()
methods is called. The
FXMLLoader
class has
the following load methods:
•
•
<T> T load() throws IOException
•
<T> T load(InputStream input) throws IOException
•
static <T> T load(URL location) throws IOException
•
static <T> T load(URL location, ResourceBundle resources) throws IOException
•
static <T> T load(URL location, ResourceBundle resources, BuilderFactory
builderFactory) throws IOException
•
static <T> T load(URL location, ResourceBundle resources, BuilderFactory
builderFactory, Callback<Class<?>, Object> controllerFactory) throws IOException
•
static <T> T load(URL location, ResourceBundle resources, BuilderFactory
builderFactory, Callback<Class<?>, Object> controllerFactory, Charset charset)
throws IOException
The
load()
method with no argument can be called on an
FXMLLoader
instance that has all the necessary fields
already initialized. The
load()
method that takes an
InputStream
argument will load the FXML from the specified
input
. All the static
load()
methods are convenience methods that will instantiate an
FXMLLoader
with the supplied
parameters and then call one of its nonstatic
load()
methods.
In our next example, we deliberately did not specify
fx:controller
in the FXML file. We also added a
one-parameter constructor to the controller class. The FXML file, the controller class, and the main class are shown
in Listings 3-4, 3-5, and 3-6.
Listing 3-4.
FXMLLoaderExample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.web.WebView?>
Search WWH ::
Custom Search