tion to handle the width and height values
for the fan-shaped layout:
else { // arrangementStyle == FAN_
ARRANGEMENT
preferredWidth = Math.sqrt(
Math.pow(lastNodeWidth, 2) +
Math.pow(lastNodeHeight, 2)) 2;
preferredHeight = preferredWidth;
}
And be sure to also change the ELSE for
the arrangementStyle == HORIZONTAL_
ARRANGEMENT statement to an ELSE IF for
proper syntax.
Step 2: Override the doLayout() function.
The doLayout() function contains the rules
for how nodes should be laid out. For the
example projects, the overridden doLayout()
function iterates over the managed nodes
and affects their sizes and positions.
override function doLayout():Void {
if (preferredDirty) {
}
When the JavaFX runtime calls the
doLayout() function, it first determines
if the arrangement of a layout manager’s
nodes should be re-evaluated by checking
the value of the preferredDirty variable.
When preferredDirty is set to true,
doLayout() calls the calcPreferredSize()
helper function to calculate the width and
the height based on the arrangement style,
size, and number of cards to be arranged,
as well as the offset value.
The calcPreferredSize() function returns
these values for the card stack in question
and then resets the preferredDirty variable
to false, so that when the processing control
returns back to the calling function (in this
case, doLayout()), the returned values will be
handled by the appropriate IF statement in
the function. In other words, the playing card
will be positioned properly within its designated arrangement (horizontal, vertical, or
fan-shaped).
DOWNLOAD the JavaFX 1. 3. 1 API and
NetBeans IDE 6. 9. 1
javafx.com/downloads/all.jsp
if (arrangementStyle == VERTICAL_
ARRANGEMENT) {
for (node in getManaged(content)) {
layoutNode(node, 0,
indexof node verticalOffset,
getNodePrefWidth(node),
getNodePrefHeight(node));
}
}
else if (arrangementStyle ==
HORIZONTAL_ARRANGEMENT) {
for (node in getManaged(content)) {
layoutNode(node,
indexof node horizontalOffset,
0,
getNodePrefWidth(node),
}
else if (arrangementStyle ==
FAN_ARRANGEMENT) {
def startAngle = (sizeof
getManaged(content)) -0.5 fanAngle;
for (node in getManaged(content)) {
def rot = Rotate.rotate(
startAngle + (indexof node
fanAngle), 0,
node.layoutBounds.height);
delete node.transforms;
insert rot into node.transforms;
var ph = getNodePrefHeight(node);
var pw = getNodePrefWidth(node);
layoutNode(node, ph, pw, pw, pw);
}
}
}
getNodePrefHeight(node));
}
Within the overridden doLayout() func-
tion are several convenience functions (from
Container) for managing nodes:
documentation ( download.oracle.com/
docs/cd/E17802_01/javafx/javafx/1.3/
docs/api) for complete details.
getNodePref Width() •;—determines the pre-
ferred width of a node.
getNodePrefHeight() •;—determines the pre-
ferred height of a node.
CONCLUSION
JavaFX includes several built-in layout managers, but occasionally they don’t meet the
requirements of an application. When this
happens, consider creating your own, following the steps detailed in this article.
James L. Weaver has been a developer
for more than 25 years. Since 2000 he has
specialized in Java, object-oriented, and Web-based technologies. He is the author of Pro
JavaFX Platform: Script, Desktop and Mobile
RIA with Java Technology (Apress, 2009) and
JavaFX Script: Dynamic Java Scripting for Rich
Internet/Client-Side Applications (Apress, 2007).
NEXT STEPS
DOWNLOAD the sample code for
this article
oracle.com/technetwork/issue-archive/
2010/10-nov/o60javafx-168643.zip
READ online-only article content
oracle.com/technetwork/issue-archive/
2010/10-nov/ o60javafx-170536.html
READ more technical articles about JavaFX
oracle.com/technetwork/java/
index-141532.html#javafx
“Building GUI Applications With JavaFX”
download.oracle.com/javafx/1.3/tutorials/ui
“Learning the JavaFX Script Programming
Language”
download.oracle.com/javafx/1.3/tutorials/core
LEARN how to get started with NetBeans IDE
6. 9 for JavaFX
download.oracle.com/javafx/1.3/getstarted
more about the JavaFX 1. 3. 1 API
download.oracle.com/docs/cd/E17802_01/
javafx/javafx/1.3/docs/api
VISIT the JavaFX resources site