- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create an anchor pane using JavaFX?
Once you create all the required nodes for your application you can arrange them using a layout. Where a layout is the process of calculating the position of objects in the given space. JavaFX provides various layouts in the javafx.scene.layout package.
Anchor Pane
In this layout, the nodes are attached/anchored to a point (offset) from the edges of the pane.
You can create an anchor pane by instantiating the javafx.scene.layout.AnchorPane class. There are four anchor constraints to specify the distance from the edges of the panes to the edges of the nodes they are − topAnchor, leftAnchor, bottomAnchor, rightAnchor.
You can set the size of the anchor pane using the setPrefSize() method. To add nodes to this pane you can either pass them as arguments of the constructor or, add them to the observable list of the pane as −
getChildren().addAll();
Example
import javafx.application.Application; import javafx.collections.ObservableList; import javafx.geometry.HPos; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.control.Label; import javafx.scene.control.Separator; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.scene.text.FontPosture; import javafx.scene.text.FontWeight; import javafx.stage.Stage; public class AnchorPaneExample extends Application { public void start(Stage stage) { //Creating the check boxes CheckBox checkBox1 = new CheckBox("Hindi"); CheckBox checkBox2 = new CheckBox("Gujarathi"); CheckBox checkBox3 = new CheckBox("Punjabi"); CheckBox checkBox4 = new CheckBox("Telugu"); CheckBox checkBox5 = new CheckBox("Tamil"); CheckBox checkBox6= new CheckBox("Malayalam"); //Creating a label Label label = new Label("Select known languages:"); Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12); label.setFont(font); //Creating a separator Separator sep = new Separator(); sep.setMaxWidth(80); sep.setHalignment(HPos.CENTER); //Adding the check boxes and separator to the pane VBox vBox = new VBox(5); vBox.setPadding(new Insets(5, 5, 5, 210)); vBox.getChildren().addAll(label, checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6); //Adding the separator after the 3rd check box vBox.getChildren().add(4, sep); //Creating the buttons Button next = new Button("Next"); Button previous = new Button("previous"); //Creating an Anchor Pane AnchorPane anchorPane = new AnchorPane(); //Setting the anchor to the next and previous buttons AnchorPane.setRightAnchor(next, 5.0); AnchorPane.setLeftAnchor(previous, 5.0); //Retrieving the observable list of the Anchor Pane ObservableList list = anchorPane.getChildren(); //Adding vBox and buttons to the anchor pane list.addAll(vBox, next, previous); //Setting the stage Scene scene = new Scene(anchorPane, 595, 200, Color.BEIGE); stage.setTitle("Anchor Pane Example"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
Output
- Related Articles
- How to create Titled Pane using JavaFx?
- How to create a scroll pane using JavaFX?
- How to create a border pane using JavaFX?
- How to create a flow pane using JavaFX?
- How to create a grid pane using JavaFX?
- How to create an Arc using JavaFX?
- How to create an Ellipse using JavaFX?
- How to create an HBox using JavaFX?
- How to create an VBox using JavaFX?
- How to create an area chart using JavaFX?
- How to change the orientation of nodes in a tile pane using JavaFX?
- How to create a circle using JavaFX?
- How to create a Rectangle using JavaFX?
- How to create a Line using JavaFX?
- How to create a Polygon using JavaFX?
