- JavaFX Tutorial
- JavaFX - Home
- JavaFX - Overview
- JavaFX Installation and Architecture
- JavaFX - Installation Using Netbeans
- JavaFX - Installation Using Eclipse
- JavaFX - Installation using Visual Studio Code
- JavaFX - Architecture
- JavaFX - Application
- JavaFX 2D Shapes
- JavaFX - 2D Shapes
- JavaFX - Drawing a Line
- JavaFX - Drawing a Rectangle
- JavaFX - Drawing a Rounded Rectangle
- JavaFX - Drawing a Circle
- JavaFX - Drawing an Ellipse
- JavaFX - Drawing a Polygon
- JavaFX - Drawing a Polyline
- JavaFX - Drawing a Cubic Curve
- JavaFX - Drawing a Quad Curve
- JavaFX - Drawing an Arc
- JavaFX - Drawing an SVGPath
- JavaFX Properties of 2D Objects
- JavaFX - Stroke Type Property
- JavaFX - Stroke Width Property
- JavaFX - Stroke Fill Property
- JavaFX - Stroke Property
- JavaFX - Stroke Line Join Property
- JavaFX - Stroke Miter Limit Property
- JavaFX - Stroke Line Cap Property
- JavaFX - Smooth Property
- Operations on 2D Objects
- JavaFX - 2D Shapes Operations
- JavaFX - Union Operation
- JavaFX - Intersection Operation
- JavaFX - Subtraction Operation
- JavaFX Color and Texture
- JavaFX - Colors
- JavaFX Text
- JavaFX - Text
- JavaFX Effects
- JavaFX - Effects
- JavaFX Transformations
- JavaFX - Transformations
- JavaFX Animations
- JavaFX - Animations
- JavaFX Images
- JavaFX - Images
- JavaFX 3D Shapes
- JavaFX - 3D Shapes
- JavaFX - Creating a Box
- JavaFX - Creating a Cylinder
- JavaFX - Creating a Sphere
- Properties of 3D Objects
- JavaFX - Cull Face Property
- JavaFX - Drawing Modes Property
- JavaFX - Material Property
- JavaFX Event Handling
- JavaFX - Event Handling
- JavaFX - Using Convenience Methods
- JavaFX - Event Filters
- JavaFX - Event Handlers
- JavaFX UI Controls
- JavaFX - UI Controls
- JavaFX - ListView
- JavaFX - Accordion
- JavaFX - ButtonBar
- JavaFX - ChoiceBox
- JavaFX - HTMLEditor
- JavaFX - MenuBar
- JavaFX - Pagination
- JavaFX - ProgressIndicator
- JavaFX - ScrollPane
- JavaFX - Separator
- JavaFX - Slider
- JavaFX - Spinner
- JavaFX - SplitPane
- JavaFX - TableView
- JavaFX - TabPane
- JavaFX - ToolBar
- JavaFX - TreeView
- JavaFX - Label
- JavaFX - CheckBox
- JavaFX - RadioButton
- JavaFX - TextField
- JavaFX - PasswordField
- JavaFX - FileChooser
- JavaFX - Hyperlink
- JavaFX - Tooltip
- JavaFX - Alert
- JavaFX - DatePicker
- JavaFX - TextArea
- JavaFX Charts
- JavaFX - Charts
- JavaFX - Creating Pie Chart
- JavaFX - Creating Line Chart
- JavaFX - Creating Area Chart
- JavaFX - Creating Bar Chart
- JavaFX - Creating Bubble Chart
- JavaFX - Creating Scatter Chart
- JavaFX - Creating Stacked Area Chart
- JavaFX - Creating Stacked Bar Chart
- JavaFX Layout Panes
- JavaFX - Layout Panes
- JavaFX - HBox Layout
- JavaFX - VBox Layout
- JavaFX - BorderPane Layout
- JavaFX - StackPane Layout
- JavaFX - TextFlow Layout
- JavaFX - AnchorPane Layout
- JavaFX - TilePane Layout
- JavaFX - GridPane Layout
- JavaFX - FlowPane Layout
- JavaFX CSS
- JavaFX - CSS
- Media with JavaFX
- JavaFX - Playing Video
- JavaFX Useful Resources
- JavaFX - Quick Guide
- JavaFX - Useful Resources
- JavaFX - Discussion
JavaFX - Anchorpane Layout
AnchorPane Layout in JavaFX
The Anchor Pane enables the anchoring of child node edges at a specified offset from the anchor pane's edges. It ensures that nodes maintain a fixed distance from the edge of layout pane. In JavaFX, an anchor pane is represented by a class named AnchorPane which is a part of javafx.scene.layout package.
Suppose, we have a node and we need to set an anchor to it from the bounds of the pane in all directions (Top, Bottom, Right and Left). To set an anchor, the AnchorPane class provides four built-in methods namely setBottomAnchor(), setTopAnchor(), setLeftAnchor() and setRightAnchor(). These methods accepts a double value representing the anchor. The following figure illustrates the same −
Note that if the anchor pane has a border and/or padding set, the offsets are calculated from the inner edge of these insets.
Cosntructors of the AnchorPane class are as follows −
AnchorPane() − Constructs an empty AnchorPane layout.
AnchorPane(Node childNodes) − It creates a new AnchorPane layout with the specified nodes.
Example
The following program is an example of the Anchor Pane layout. In this, we are inserting a rotating cylinder in an anchor pane. Save this code in a file with the name AnchorPaneExample.java.
import javafx.animation.RotateTransition; import javafx.scene.Scene; import javafx.scene.layout.AnchorPane; import javafx.scene.paint.Color; import javafx.scene.paint.PhongMaterial; import javafx.scene.shape.Cylinder; import javafx.scene.transform.Rotate; import javafx.stage.Stage; import javafx.util.Duration; public class AnchorPaneExample extends Application { @Override public void start(Stage stage) { //Drawing a Cylinder Cylinder cylinder = new Cylinder(); //Setting the properties of the Cylinder cylinder.setHeight(180.0f); cylinder.setRadius(100.0f); //Preparing the phong material of type diffuse color PhongMaterial material = new PhongMaterial(); material.setDiffuseColor(Color.BLANCHEDALMOND); //Setting the diffuse color material to Cylinder5 cylinder.setMaterial(material); //Setting rotation transition for the cylinder RotateTransition rotateTransition = new RotateTransition(); //Setting the duration for the transition rotateTransition.setDuration(Duration.millis(1000)); //Setting the node for the transition rotateTransition.setNode(cylinder); //Setting the axis of the rotation rotateTransition.setAxis(Rotate.X_AXIS); //Setting the angle of the rotation rotateTransition.setByAngle(360); //Setting the cycle count for the transition rotateTransition.setCycleCount(RotateTransition.INDEFINITE); //Setting auto reverse value to false rotateTransition.setAutoReverse(false); //playing the animation rotateTransition.play(); //Creating an Anchor Pane AnchorPane anchorPane = new AnchorPane(); //Setting the anchor to the cylinder AnchorPane.setTopAnchor(cylinder, 50.0); AnchorPane.setLeftAnchor(cylinder, 50.0); AnchorPane.setRightAnchor(cylinder, 50.0); AnchorPane.setBottomAnchor(cylinder, 50.0); //Adding cylinder to the pane anchorPane.getChildren().addAll(cylinder); //Creating a scene object Scene scene = new Scene(anchorPane, 400, 300); //Setting title to the Stage stage.setTitle("Anchor Pane Example"); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } }
Compile and execute the saved java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls AnchorPaneExample.java java --module-path %PATH_TO_FX% --add-modules javafx.controls AnchorPaneExample
Output
On executing, the above program generates a JavaFX window as shown below.