Java Reference
In-Depth Information
stage.show();
Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds();
stage.setX((primScreenBounds.getWidth() - stage.getWidth()) / 2);
stage.setY((primScreenBounds.getHeight() - stage.getHeight()) / 4);
}
}
Obtaining Program Arguments
The first new concept introduced by this program is the ability to read the arguments passed into a JavaFX program.
The
javafx.application
package includes a class named
Application
that has application life cycle-related methods
such as
launch()
,
init()
,
start()
, and
stop()
. Another method in the
Application
class is
getParameters()
, which
gives the application access to the arguments passed on the command line, as well as unnamed parameters and
<name,value>
pairs specified in a JNLP file. Here's the relevant code snippet from Listing 2-1 for your convenience:
StageStyle stageStyle = StageStyle.DECORATED;
List<String> unnamedParams = getParameters().getUnnamed();
if (unnamedParams.size() > 0) {
String stageStyleParam = unnamedParams.get(0);
if (stageStyleParam.equalsIgnoreCase("transparent")) {
stageStyle = StageStyle.TRANSPARENT;
}
else if (stageStyleParam.equalsIgnoreCase("undecorated")) {
stageStyle = StageStyle.UNDECORATED;
}
else if (stageStyleParam.equalsIgnoreCase("utility")) {
stageStyle = StageStyle.UTILITY;
}
}
...code omitted...
stage.initStyle(stageStyle);
Setting the Style of the Stage
We're using the
getParameters()
method described previously to get an argument that tells us whether the stage
style of the
Stage
instance should be its default (
StageStyle.DECORATED
),
StageStyle.UNDECORATED
, or
StageStyle.
TRANSPARENT
. You saw the effects of each in the preceding exercise, specifically in Figures
2-2
,
2-4
, and
2-5
.
Controlling Whether a Stage Is Resizable
As shown in the following excerpt from Listing 2-1, to make this application's window initially resizable we're calling
the
setResizable()
method of the
Stage
instance. To keep the resizable property of the
Stage
and the state of the
resizable check box synchronized, the check box is bidirectionally bound to the resizable property of the
Stage
instance.
stage.setResizable(true);
checkBoxResizable.selectedProperty()
.bindBidirectional(stage.resizableProperty());
Search WWH ::
Custom Search