SlideShare una empresa de Scribd logo
1 de 40
Brought to you by
Graphical User Interface in Java
Using JavaFX
Layouts
1K. N. Toosi University of Technology
What are Layouts?
• Until now, we only worked with the simplest Node container which
was Pane.
• It’s the simplest one, because we specified the location of our
elements by hand in pixel format.
• Now days, with a great needs of responsive UIs no one is going to
hard code the location of UI elements.
• So we need smarter node container.
• A smart container decides about the location of an element
automatically based on a specified behavior.
K. N. Toosi University of Technology 2
JavaFX Containers Hierarchy:
3K. N. Toosi University of Technology
Region
Control
SplitPane ScrollPane
Accordion TabPane
Pane
AnchorPane BorderPane
DialogPane FlowPane
VBox HBox
GridPane StackPane
TilePane
BorderPane:
K. N. Toosi University of Technology 4
LEFT RIGHTCENTER
TOP
BOTTOM
Demo 1: BorderPane
5K. N. Toosi University of Technology
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane();
Rectangle left = new Rectangle(100, 400, Color.BLUEVIOLET);
root.setLeft(left);
Rectangle right = new Rectangle(100, 400, Color.BLUEVIOLET);
root.setRight(right);
Rectangle center = new Rectangle(200, 200, Color.GREEN);
root.setCenter(center);
Rectangle top = new Rectangle(400, 100, Color.RED);
root.setTop(top);
Rectangle bottom = new Rectangle(400, 100, Color.RED);
root.setBottom(bottom);
//Alignments
BorderPane.setAlignment(bottom, Pos.CENTER);
//Margin
BorderPane.setMargin(left, new Insets(12,12,12,12));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
Demo 1: BorderPane
6K. N. Toosi University of Technology
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane();
Rectangle left = new Rectangle(100, 400, Color.BLUEVIOLET);
root.setLeft(left);
Rectangle right = new Rectangle(100, 400, Color.BLUEVIOLET);
root.setRight(right);
Rectangle center = new Rectangle(200, 200, Color.GREEN);
root.setCenter(center);
Rectangle top = new Rectangle(400, 100, Color.RED);
root.setTop(top);
Rectangle bottom = new Rectangle(400, 100, Color.RED);
root.setBottom(bottom);
//Alignments
BorderPane.setAlignment(bottom, Pos.CENTER);
//Margin
BorderPane.setMargin(left, new Insets(12,12,12,12));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
Rectangle bottom = new Rectangle(400, 100,
Color.RED);
root.setBottom(bottom);
//Alignments
BorderPane.setAlignment(bottom, Pos.CENTER);
//Margin
BorderPane.setMargin(left, new Insets(12,12,12,12));
AcnchorPane:
K. N. Toosi University of Technology 7
Node Node
30px
AnchorPane allows the edges of child nodes to be anchored to an offset from the anchor pane's edges.
AcnchorPane:
Constraint Type Description
topAnchor double distance from the anchor pane's top insets to the child's top edge.
leftAnchor double distance from the anchor pane's left insets to the child's left edge.
bottomAnchor double
distance from the anchor pane's bottom insets to the child's bottom
edge.
rightAnchor double distance from the anchor pane's right insets to the child's right edge.
K. N. Toosi University of Technology 8
static void setBottomAnchor(Node child, Double value)
Sets the bottom anchor for the child when contained by an anchor pane.
static void setLeftAnchor(Node child, Double value)
Sets the left anchor for the child when contained by an anchor pane.
static void setRightAnchor(Node child, Double value)
Sets the right anchor for the child when contained by an anchor pane.
static void setTopAnchor(Node child, Double value)
Sets the top anchor for the child when contained by an anchor pane.
Demo 2:
9K. N. Toosi University of Technology
@Override
public void start(Stage primaryStage) throws Exception {
AnchorPane root = new AnchorPane();
Button button = new Button("You Can Click Me");
AnchorPane.setLeftAnchor(button, 30D);
AnchorPane.setRightAnchor(button, 30D);
AnchorPane.setTopAnchor(button, 30D);
root.getChildren().add(button);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
Demo 2:
10K. N. Toosi University of Technology
@Override
public void start(Stage primaryStage) throws Exception {
AnchorPane root = new AnchorPane();
Button button = new Button("You Can Click Me");
AnchorPane.setLeftAnchor(button, 30D);
AnchorPane.setRightAnchor(button, 30D);
AnchorPane.setTopAnchor(button, 30D);
root.getChildren().add(button);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
…
Button button = new Button("You Can Click
Me");
AnchorPane.setLeftAnchor(button, 30D);
AnchorPane.setRightAnchor(button, 30D);
AnchorPane.setTopAnchor(button, 30D);
Hbox/Vbox:
K. N. Toosi University of Technology 11
Node
Node
Node
Node Node Node
spacing
Demo 3:
12K. N. Toosi University of Technology
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane();
VBox vbox = new VBox(10);
root.setLeft(vbox);
for(int i=0;i<5;i++){
Rectangle rectangle = new Rectangle(40, 40);
vbox.getChildren().add(rectangle);
}
HBox hbox = new HBox(40);
root.setTop(hbox);
for(int i=0;i<5;i++){
Circle circle = new Circle(20);
hbox.getChildren().add(circle);
}
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
Demo 3:
13K. N. Toosi University of Technology
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane();
VBox vbox = new VBox(10);
root.setLeft(vbox);
for(int i=0;i<5;i++){
Rectangle rectangle = new Rectangle(40, 40);
vbox.getChildren().add(rectangle);
}
HBox hbox = new HBox(40);
root.setTop(hbox);
for(int i=0;i<5;i++){
Circle circle = new Circle(20);
hbox.getChildren().add(circle);
}
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
VBox vbox = new VBox(10);
root.setLeft(vbox);
for(int i=0;i<5;i++){
Rectangle rectangle = new Rectangle(40, 40);
vbox.getChildren().add(rectangle);
}
HBox hbox = new HBox(40);
root.setTop(hbox);
for(int i=0;i<5;i++){
Circle circle = new Circle(20);
hbox.getChildren().add(circle);
}
Demo 3:
14K. N. Toosi University of Technology
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane root = new BorderPane();
VBox vbox = new VBox(10);
root.setLeft(vbox);
for(int i=0;i<5;i++){
Rectangle rectangle = new Rectangle(40, 40);
vbox.getChildren().add(rectangle);
}
HBox hbox = new HBox(40);
hbox.setAlignment(Pos.CENTER);
root.setTop(hbox);
for(int i=0;i<5;i++){
Circle circle = new Circle(20);
hbox.getChildren().add(circle);
}
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
StackPane:
K. N. Toosi University of Technology 15
Demo 4:
16K. N. Toosi University of Technology
@Override
public void start(Stage primaryStage) throws Exception {
StackPane root = new StackPane();
for (int i = 0; i < 90; i += 10) {
Rectangle rectangle = new Rectangle(300, 300);
rectangle.setRotate(i);
rectangle.setFill(Color.rgb(i+90, i / 20, i / 30));
root.getChildren().add(rectangle);
}
Label label = new Label("JavaFX is Awesome");
label.setFont(Font.font(24));
label.setTextFill(Color.WHITE);
root.getChildren().add(label);
Scene scene = new Scene(root, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
You can set the alignment of components
with: root.setAlignment( Pos.? );
Demo 4:
17K. N. Toosi University of Technology
@Override
public void start(Stage primaryStage) throws Exception {
StackPane root = new StackPane();
for (int i = 0; i < 90; i += 10) {
Rectangle rectangle = new Rectangle(300, 300);
rectangle.setRotate(i);
rectangle.setFill(Color.rgb(i+90, i / 20, i / 30));
root.getChildren().add(rectangle);
}
Label label = new Label("JavaFX is Awesome");
label.setFont(Font.font(24));
label.setTextFill(Color.WHITE);
root.getChildren().add(label);
Scene scene = new Scene(root, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
for (int i = 0; i < 90; i += 10) {
Rectangle rectangle = new Rectangle(300, 300);
rectangle.setRotate(i);
rectangle.setFill(Color.rgb(i+90, i / 20, i / 30));
root.getChildren().add(rectangle);
}
Demo 4:
18K. N. Toosi University of Technology
@Override
public void start(Stage primaryStage) throws Exception {
StackPane root = new StackPane();
for (int i = 0; i < 90; i += 10) {
Rectangle rectangle = new Rectangle(300, 300);
rectangle.setRotate(i);
rectangle.setFill(Color.rgb(i+90, i / 20, i / 30));
root.getChildren().add(rectangle);
}
Label label = new Label("JavaFX is Awesome");
label.setFont(Font.font(24));
label.setTextFill(Color.WHITE);
root.getChildren().add(label);
Scene scene = new Scene(root, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
Label label = new Label("JavaFX is Awesome");
label.setFont(Font.font(24));
label.setTextFill(Color.WHITE);
root.getChildren().add(label);
GridPane:
K. N. Toosi University of Technology 19
(0,0) (1,0) (2,0)
(0,1) (1,1)
(0,2) (1,2) (2,3)
padding
hgap
vgap
Demo 5 (1/3):
20K. N. Toosi University of Technology
@Override
public void start(Stage primaryStage) throws Exception {
GridPane root = new GridPane();
Scene scene = new Scene(root);
root.setPadding(new Insets(10));
root.setHgap(20);
root.setVgap(5);
ColumnConstraints column1 = new ColumnConstraints(100);
ColumnConstraints column2 = new ColumnConstraints(90, 150, Double.MAX_VALUE);
column2.setHgrow(Priority.ALWAYS);
root.getColumnConstraints().addAll(column1, column2);
. . . . .
}
Demo 5 (2/3):
21K. N. Toosi University of Technology
@Override
public void start(Stage primaryStage) throws Exception {
. . . . .
Label formLbl = new Label("Enter your First name and last name");
Label fNameLbl = new Label("First Name");
TextField fNameFld = new TextField();
Label lNameLbl = new Label("Last Name");
TextField lNameFld = new TextField();
Button saveButton = new Button("Save");
. . . . .
}
Demo 5 (3/3):
22K. N. Toosi University of Technology
@Override
public void start(Stage primaryStage) throws Exception {
. . . . .
GridPane.setHalignment(formLbl, HPos.LEFT);
root.add(formLbl, 0, 0,2,1);
GridPane.setHalignment(fNameLbl, HPos.LEFT);
root.add(fNameLbl, 0, 1);
GridPane.setHalignment(lNameLbl, HPos.LEFT);
root.add(lNameLbl, 0, 2);
GridPane.setHalignment(fNameFld, HPos.LEFT);
root.add(fNameFld, 1, 1);
GridPane.setHalignment(lNameFld, HPos.LEFT);
root.add(lNameFld, 1, 2);
GridPane.setHalignment(saveButton, HPos.RIGHT);
root.add(saveButton, 2, 3);
primaryStage.setScene(scene);
primaryStage.show();
}
FlowPane:
K. N. Toosi University of Technology 23
Node2
Node4
Node3Node1
Node2
Node4
Node3
Node1
HGap
VGap
Demo 6:
24K. N. Toosi University of Technology
@Override
public void start(Stage primaryStage) throws Exception {
FlowPane root = new FlowPane();
root.setHgap(10);
root.setVgap(30);
root.setPadding(new Insets(15));
Button button1 = new Button("Button1");
root.getChildren().add(button1);
Button button2 = new Button("Button2");
button2.setPrefSize(100, 100);
root.getChildren().add(button2);
TextField textField = new TextField("Text Field");
textField.setPrefWidth(110);
root.getChildren().add(textField);
Rectangle rectangle = new Rectangle(300, 200);
root.getChildren().add(rectangle);
Scene scene = new Scene(root, 550, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
Demo 6:
25K. N. Toosi University of Technology
@Override
public void start(Stage primaryStage) throws Exception {
FlowPane root = new FlowPane();
root.setHgap(10);
root.setVgap(30);
root.setPadding(new Insets(15));
Button button1 = new Button("Button1");
root.getChildren().add(button1);
Button button2 = new Button("Button2");
button2.setPrefSize(100, 100);
root.getChildren().add(button2);
TextField textField = new TextField("Text Field");
textField.setPrefWidth(110);
root.getChildren().add(textField);
Rectangle rectangle = new Rectangle(300, 200);
root.getChildren().add(rectangle);
Scene scene = new Scene(root, 550, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
FlowPane root = new FlowPane();
root.setHgap(10);
root.setVgap(30);
root.setPadding(new Insets(15));
Demo 6:
26K. N. Toosi University of Technology
@Override
public void start(Stage primaryStage) throws Exception {
FlowPane root = new FlowPane(Orientation.VERTICAL);
root.setHgap(10);
root.setVgap(30);
root.setPadding(new Insets(15));
Button button1 = new Button("Button1");
root.getChildren().add(button1);
Button button2 = new Button("Button2");
button2.setPrefSize(100, 100);
root.getChildren().add(button2);
TextField textField = new TextField("Text Field");
textField.setPrefWidth(110);
root.getChildren().add(textField);
Rectangle rectangle = new Rectangle(300, 200);
root.getChildren().add(rectangle);
Scene scene = new Scene(root, 550, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
FlowPane root = new
FlowPane(Orientation.VERTICAL);
root.setHgap(10);
root.setVgap(30);
root.setPadding(new Insets(15));
A horizontal flowpane (the default) will layout nodes in rows,
wrapping at the flowpane's width.
A vertical flowpane lays out nodes in columns, wrapping at the
flowpane's height.
TilePane:
K. N. Toosi University of Technology 27
Node2
Node4
Node3Node1
Node2
Node4Node3
Node1
Demo 7:
28K. N. Toosi University of Technology
@Override
public void start(Stage primaryStage) throws Exception {
TilePane root = new TilePane(Orientation.HORIZONTAL);
root.setHgap(10);
root.setVgap(30);
root.setPadding(new Insets(15));
root.setTileAlignment(Pos.TOP_CENTER);
Button button1 = new Button("Button1");
root.getChildren().add(button1);
Button button2 = new Button("Button2");
button2.setPrefSize(100, 100);
root.getChildren().add(button2);
TextField textField = new TextField("Text Field");
textField.setPrefWidth(110);
root.getChildren().add(textField);
Rectangle rectangle = new Rectangle(200, 200);
root.getChildren().add(rectangle);
Scene scene = new Scene(root, 550, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
DialogPane:
• We will talk about Dialogs separately.
K. N. Toosi University of Technology 29
JavaFX Containers Hierarchy:
30K. N. Toosi University of Technology
Region
Control
SplitPane ScrollPane
Accordion TabPane
Pane
AnchorPane BorderPane
DialogPane FlowPane
VBox HBox
GridPane StackPane
TilePane
Control versus Pane:
• A "Control" is a node in the scene graph which can be manipulated
by the user. Controls provide additional variables and behaviors
beyond those of Node to support common user interactions in a
manner which is consistent and predictable for the user.
• Pane is the base class for layout panes which need to expose the
children list as public so that users of the subclass can freely
add/remove children.
K. N. Toosi University of Technology 31
ScrollPane:
K. N. Toosi University of Technology 32
Some very very very very very
very very very very very very
very very very very very very
very very very very very very
very very very very very very
very very very very very very
very very very very very very
very very very very very very
very very very very very very
very very very very very very
very very very very very very
very very very very very
Long Text
Some very very very very very
very very very very very very
very very very very very very
very very very very very very
very very very very very very
very very very very very very
very very very very very very
Demo 8:
33K. N. Toosi University of Technology
@Override
public void start(Stage primaryStage) throws Exception {
StackPane stackPane = new StackPane();
ScrollPane root = new ScrollPane();
root.setPannable(true);
root.setFitToHeight(true);
root.setFitToWidth(true);
Rectangle rectangle = new Rectangle(900, 400);
stackPane.getChildren().add(rectangle);
root.setContent(stackPane);
Scene scene = new Scene(root, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
SplitPane:
K. N. Toosi University of Technology 34
RIGHTBOTTOM BOTTOM
Divider
Divider Position
between [0.0 – 1.0]
Demo 9:
35K. N. Toosi University of Technology
@Override
public void start(Stage primaryStage) throws Exception {
SplitPane root = new SplitPane();
Button button21 = new Button("Click Me21");
Button button22 = new Button("Click Me22");
VBox vBox1 = new VBox(button21,button22);
Button button11 = new Button("Click Me11");
Button button12 = new Button("Click Me12");
BorderPane borderPane = new BorderPane(null, button12,
null, button11, null);
Button button31 = new Button("Click Me31");
Button button32 = new Button("Click Me32");
VBox vBox2 = new VBox(button31,button32);
root.setDividerPositions(0.2f,0.8f);
root.getItems().addAll(vBox1,borderPane,vBox2);
Scene scene = new Scene(root,500,400);
primaryStage.setScene(scene);
primaryStage.show();
}
20%
80%
100%
TabPane:
K. N. Toosi University of Technology 36
Tab3Tab1 Tab2
Tab Content
Demo 10:
37K. N. Toosi University of Technology
@Override
public void start(Stage primaryStage) throws Exception {
TabPane root = new TabPane();
Tab tab1 = new Tab("First Tab");
tab1.setContent(new Button("Hello"));
Tab tab2 = new Tab("Second Tab");
tab2.setContent(new Button("World"));
root.getTabs().addAll(tab1,tab2);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
Live Coding: UI Sketch to JavaFX
38K. N. Toosi University of Technology
Codes:
• https://github.com/mhrimaz/JavaFXLayoutsDemo
K. N. Toosi University of Technology 39
Brought to you by
Happy Coding 
40K. N. Toosi University of Technology

Más contenido relacionado

La actualidad más candente

Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Mario Fusco
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
Suneel Dogra
 

La actualidad más candente (20)

Generics
GenericsGenerics
Generics
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
Collections - Maps
Collections - Maps Collections - Maps
Collections - Maps
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
 
Advanced behavioral modeling chapter 4 of omd
Advanced behavioral modeling chapter 4 of omdAdvanced behavioral modeling chapter 4 of omd
Advanced behavioral modeling chapter 4 of omd
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
 
Unit I Advanced Java Programming Course
Unit I   Advanced Java Programming CourseUnit I   Advanced Java Programming Course
Unit I Advanced Java Programming Course
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
 
Lecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVMLecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVM
 
Introduction to EJB
Introduction to EJBIntroduction to EJB
Introduction to EJB
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 Foc
 
Minimization of DFA.pptx
Minimization of DFA.pptxMinimization of DFA.pptx
Minimization of DFA.pptx
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Java 8 - CJ
Java 8 - CJJava 8 - CJ
Java 8 - CJ
 
Threads V4
Threads  V4Threads  V4
Threads V4
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Unit Testing with Foq
Unit Testing with FoqUnit Testing with Foq
Unit Testing with Foq
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
 
Adbms lab manual
Adbms lab manualAdbms lab manual
Adbms lab manual
 

Similar a 003 - JavaFX Tutorial - Layouts

package chapter15;import javafx.application.Application;import j.pdf
package chapter15;import javafx.application.Application;import j.pdfpackage chapter15;import javafx.application.Application;import j.pdf
package chapter15;import javafx.application.Application;import j.pdf
KARTIKINDIA
 
Create a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdfCreate a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdf
arihantmobileselepun
 
Exploring Canvas
Exploring CanvasExploring Canvas
Exploring Canvas
Kevin Hoyt
 
Fee managment system
Fee managment systemFee managment system
Fee managment system
fairy9912
 

Similar a 003 - JavaFX Tutorial - Layouts (20)

package chapter15;import javafx.application.Application;import j.pdf
package chapter15;import javafx.application.Application;import j.pdfpackage chapter15;import javafx.application.Application;import j.pdf
package chapter15;import javafx.application.Application;import j.pdf
 
Create a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdfCreate a java project that - Draw a circle with three random init.pdf
Create a java project that - Draw a circle with three random init.pdf
 
Sbaw090623
Sbaw090623Sbaw090623
Sbaw090623
 
The Java Fx Platform – A Java Developer’S Guide
The Java Fx Platform – A Java Developer’S GuideThe Java Fx Platform – A Java Developer’S Guide
The Java Fx Platform – A Java Developer’S Guide
 
JavaFX 2.0 and Alternative Languages
JavaFX 2.0 and Alternative LanguagesJavaFX 2.0 and Alternative Languages
JavaFX 2.0 and Alternative Languages
 
Jetpack Compose - Hands-on February 2020
Jetpack Compose - Hands-on February 2020Jetpack Compose - Hands-on February 2020
Jetpack Compose - Hands-on February 2020
 
C# v8 new features - raimundas banevicius
C# v8 new features - raimundas baneviciusC# v8 new features - raimundas banevicius
C# v8 new features - raimundas banevicius
 
JavaFX 2.0 With Alternative Languages - JavaOne 2011
JavaFX 2.0 With Alternative Languages - JavaOne 2011JavaFX 2.0 With Alternative Languages - JavaOne 2011
JavaFX 2.0 With Alternative Languages - JavaOne 2011
 
JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and V...
JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and V...JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and V...
JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and V...
 
Hacking JavaFX with Groovy, Clojure, Scala, and Visage
Hacking JavaFX with Groovy, Clojure, Scala, and VisageHacking JavaFX with Groovy, Clojure, Scala, and Visage
Hacking JavaFX with Groovy, Clojure, Scala, and Visage
 
Coding in Style
Coding in StyleCoding in Style
Coding in Style
 
M.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMS
M.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMSM.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMS
M.TECH 1ST SEM COMPUTER SCIENCE ADBMS LAB PROGRAMS
 
Exploring Canvas
Exploring CanvasExploring Canvas
Exploring Canvas
 
Heaps
HeapsHeaps
Heaps
 
JavaFX Your Way - Devoxx Version
JavaFX Your Way - Devoxx VersionJavaFX Your Way - Devoxx Version
JavaFX Your Way - Devoxx Version
 
Java Performance Puzzlers
Java Performance PuzzlersJava Performance Puzzlers
Java Performance Puzzlers
 
JavaFX introduction
JavaFX introductionJavaFX introduction
JavaFX introduction
 
Fee managment system
Fee managment systemFee managment system
Fee managment system
 
Building Data Rich Interfaces with JavaFX
Building Data Rich Interfaces with JavaFXBuilding Data Rich Interfaces with JavaFX
Building Data Rich Interfaces with JavaFX
 
1z0 851 exam-java standard edition 6 programmer certified professional
1z0 851 exam-java standard edition 6 programmer certified professional1z0 851 exam-java standard edition 6 programmer certified professional
1z0 851 exam-java standard edition 6 programmer certified professional
 

Más de Mohammad Hossein Rimaz

Más de Mohammad Hossein Rimaz (7)

004 - JavaFX Tutorial - Event Handling
004 - JavaFX Tutorial - Event Handling004 - JavaFX Tutorial - Event Handling
004 - JavaFX Tutorial - Event Handling
 
002- JavaFX Tutorial - Getting Started
002- JavaFX Tutorial - Getting Started002- JavaFX Tutorial - Getting Started
002- JavaFX Tutorial - Getting Started
 
Java 9, JShell, and Modularity
Java 9, JShell, and ModularityJava 9, JShell, and Modularity
Java 9, JShell, and Modularity
 
Quick introduction to scala
Quick introduction to scalaQuick introduction to scala
Quick introduction to scala
 
Continuous integration with Jenkins
Continuous integration with JenkinsContinuous integration with Jenkins
Continuous integration with Jenkins
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
JavaFX in Action Part I
JavaFX in Action Part IJavaFX in Action Part I
JavaFX in Action Part I
 

Último

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 

Último (20)

Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 

003 - JavaFX Tutorial - Layouts

  • 1. Brought to you by Graphical User Interface in Java Using JavaFX Layouts 1K. N. Toosi University of Technology
  • 2. What are Layouts? • Until now, we only worked with the simplest Node container which was Pane. • It’s the simplest one, because we specified the location of our elements by hand in pixel format. • Now days, with a great needs of responsive UIs no one is going to hard code the location of UI elements. • So we need smarter node container. • A smart container decides about the location of an element automatically based on a specified behavior. K. N. Toosi University of Technology 2
  • 3. JavaFX Containers Hierarchy: 3K. N. Toosi University of Technology Region Control SplitPane ScrollPane Accordion TabPane Pane AnchorPane BorderPane DialogPane FlowPane VBox HBox GridPane StackPane TilePane
  • 4. BorderPane: K. N. Toosi University of Technology 4 LEFT RIGHTCENTER TOP BOTTOM
  • 5. Demo 1: BorderPane 5K. N. Toosi University of Technology @Override public void start(Stage primaryStage) throws Exception { BorderPane root = new BorderPane(); Rectangle left = new Rectangle(100, 400, Color.BLUEVIOLET); root.setLeft(left); Rectangle right = new Rectangle(100, 400, Color.BLUEVIOLET); root.setRight(right); Rectangle center = new Rectangle(200, 200, Color.GREEN); root.setCenter(center); Rectangle top = new Rectangle(400, 100, Color.RED); root.setTop(top); Rectangle bottom = new Rectangle(400, 100, Color.RED); root.setBottom(bottom); //Alignments BorderPane.setAlignment(bottom, Pos.CENTER); //Margin BorderPane.setMargin(left, new Insets(12,12,12,12)); Scene scene = new Scene(root); primaryStage.setScene(scene); primaryStage.show(); }
  • 6. Demo 1: BorderPane 6K. N. Toosi University of Technology @Override public void start(Stage primaryStage) throws Exception { BorderPane root = new BorderPane(); Rectangle left = new Rectangle(100, 400, Color.BLUEVIOLET); root.setLeft(left); Rectangle right = new Rectangle(100, 400, Color.BLUEVIOLET); root.setRight(right); Rectangle center = new Rectangle(200, 200, Color.GREEN); root.setCenter(center); Rectangle top = new Rectangle(400, 100, Color.RED); root.setTop(top); Rectangle bottom = new Rectangle(400, 100, Color.RED); root.setBottom(bottom); //Alignments BorderPane.setAlignment(bottom, Pos.CENTER); //Margin BorderPane.setMargin(left, new Insets(12,12,12,12)); Scene scene = new Scene(root); primaryStage.setScene(scene); primaryStage.show(); } Rectangle bottom = new Rectangle(400, 100, Color.RED); root.setBottom(bottom); //Alignments BorderPane.setAlignment(bottom, Pos.CENTER); //Margin BorderPane.setMargin(left, new Insets(12,12,12,12));
  • 7. AcnchorPane: K. N. Toosi University of Technology 7 Node Node 30px AnchorPane allows the edges of child nodes to be anchored to an offset from the anchor pane's edges.
  • 8. AcnchorPane: Constraint Type Description topAnchor double distance from the anchor pane's top insets to the child's top edge. leftAnchor double distance from the anchor pane's left insets to the child's left edge. bottomAnchor double distance from the anchor pane's bottom insets to the child's bottom edge. rightAnchor double distance from the anchor pane's right insets to the child's right edge. K. N. Toosi University of Technology 8 static void setBottomAnchor(Node child, Double value) Sets the bottom anchor for the child when contained by an anchor pane. static void setLeftAnchor(Node child, Double value) Sets the left anchor for the child when contained by an anchor pane. static void setRightAnchor(Node child, Double value) Sets the right anchor for the child when contained by an anchor pane. static void setTopAnchor(Node child, Double value) Sets the top anchor for the child when contained by an anchor pane.
  • 9. Demo 2: 9K. N. Toosi University of Technology @Override public void start(Stage primaryStage) throws Exception { AnchorPane root = new AnchorPane(); Button button = new Button("You Can Click Me"); AnchorPane.setLeftAnchor(button, 30D); AnchorPane.setRightAnchor(button, 30D); AnchorPane.setTopAnchor(button, 30D); root.getChildren().add(button); Scene scene = new Scene(root); primaryStage.setScene(scene); primaryStage.show(); }
  • 10. Demo 2: 10K. N. Toosi University of Technology @Override public void start(Stage primaryStage) throws Exception { AnchorPane root = new AnchorPane(); Button button = new Button("You Can Click Me"); AnchorPane.setLeftAnchor(button, 30D); AnchorPane.setRightAnchor(button, 30D); AnchorPane.setTopAnchor(button, 30D); root.getChildren().add(button); Scene scene = new Scene(root); primaryStage.setScene(scene); primaryStage.show(); } import javafx.scene.control.Button; import javafx.scene.layout.AnchorPane; … Button button = new Button("You Can Click Me"); AnchorPane.setLeftAnchor(button, 30D); AnchorPane.setRightAnchor(button, 30D); AnchorPane.setTopAnchor(button, 30D);
  • 11. Hbox/Vbox: K. N. Toosi University of Technology 11 Node Node Node Node Node Node spacing
  • 12. Demo 3: 12K. N. Toosi University of Technology @Override public void start(Stage primaryStage) throws Exception { BorderPane root = new BorderPane(); VBox vbox = new VBox(10); root.setLeft(vbox); for(int i=0;i<5;i++){ Rectangle rectangle = new Rectangle(40, 40); vbox.getChildren().add(rectangle); } HBox hbox = new HBox(40); root.setTop(hbox); for(int i=0;i<5;i++){ Circle circle = new Circle(20); hbox.getChildren().add(circle); } Scene scene = new Scene(root); primaryStage.setScene(scene); primaryStage.show(); }
  • 13. Demo 3: 13K. N. Toosi University of Technology @Override public void start(Stage primaryStage) throws Exception { BorderPane root = new BorderPane(); VBox vbox = new VBox(10); root.setLeft(vbox); for(int i=0;i<5;i++){ Rectangle rectangle = new Rectangle(40, 40); vbox.getChildren().add(rectangle); } HBox hbox = new HBox(40); root.setTop(hbox); for(int i=0;i<5;i++){ Circle circle = new Circle(20); hbox.getChildren().add(circle); } Scene scene = new Scene(root); primaryStage.setScene(scene); primaryStage.show(); } VBox vbox = new VBox(10); root.setLeft(vbox); for(int i=0;i<5;i++){ Rectangle rectangle = new Rectangle(40, 40); vbox.getChildren().add(rectangle); } HBox hbox = new HBox(40); root.setTop(hbox); for(int i=0;i<5;i++){ Circle circle = new Circle(20); hbox.getChildren().add(circle); }
  • 14. Demo 3: 14K. N. Toosi University of Technology @Override public void start(Stage primaryStage) throws Exception { BorderPane root = new BorderPane(); VBox vbox = new VBox(10); root.setLeft(vbox); for(int i=0;i<5;i++){ Rectangle rectangle = new Rectangle(40, 40); vbox.getChildren().add(rectangle); } HBox hbox = new HBox(40); hbox.setAlignment(Pos.CENTER); root.setTop(hbox); for(int i=0;i<5;i++){ Circle circle = new Circle(20); hbox.getChildren().add(circle); } Scene scene = new Scene(root); primaryStage.setScene(scene); primaryStage.show(); }
  • 15. StackPane: K. N. Toosi University of Technology 15
  • 16. Demo 4: 16K. N. Toosi University of Technology @Override public void start(Stage primaryStage) throws Exception { StackPane root = new StackPane(); for (int i = 0; i < 90; i += 10) { Rectangle rectangle = new Rectangle(300, 300); rectangle.setRotate(i); rectangle.setFill(Color.rgb(i+90, i / 20, i / 30)); root.getChildren().add(rectangle); } Label label = new Label("JavaFX is Awesome"); label.setFont(Font.font(24)); label.setTextFill(Color.WHITE); root.getChildren().add(label); Scene scene = new Scene(root, 600, 600); primaryStage.setScene(scene); primaryStage.show(); } You can set the alignment of components with: root.setAlignment( Pos.? );
  • 17. Demo 4: 17K. N. Toosi University of Technology @Override public void start(Stage primaryStage) throws Exception { StackPane root = new StackPane(); for (int i = 0; i < 90; i += 10) { Rectangle rectangle = new Rectangle(300, 300); rectangle.setRotate(i); rectangle.setFill(Color.rgb(i+90, i / 20, i / 30)); root.getChildren().add(rectangle); } Label label = new Label("JavaFX is Awesome"); label.setFont(Font.font(24)); label.setTextFill(Color.WHITE); root.getChildren().add(label); Scene scene = new Scene(root, 600, 600); primaryStage.setScene(scene); primaryStage.show(); } for (int i = 0; i < 90; i += 10) { Rectangle rectangle = new Rectangle(300, 300); rectangle.setRotate(i); rectangle.setFill(Color.rgb(i+90, i / 20, i / 30)); root.getChildren().add(rectangle); }
  • 18. Demo 4: 18K. N. Toosi University of Technology @Override public void start(Stage primaryStage) throws Exception { StackPane root = new StackPane(); for (int i = 0; i < 90; i += 10) { Rectangle rectangle = new Rectangle(300, 300); rectangle.setRotate(i); rectangle.setFill(Color.rgb(i+90, i / 20, i / 30)); root.getChildren().add(rectangle); } Label label = new Label("JavaFX is Awesome"); label.setFont(Font.font(24)); label.setTextFill(Color.WHITE); root.getChildren().add(label); Scene scene = new Scene(root, 600, 600); primaryStage.setScene(scene); primaryStage.show(); } Label label = new Label("JavaFX is Awesome"); label.setFont(Font.font(24)); label.setTextFill(Color.WHITE); root.getChildren().add(label);
  • 19. GridPane: K. N. Toosi University of Technology 19 (0,0) (1,0) (2,0) (0,1) (1,1) (0,2) (1,2) (2,3) padding hgap vgap
  • 20. Demo 5 (1/3): 20K. N. Toosi University of Technology @Override public void start(Stage primaryStage) throws Exception { GridPane root = new GridPane(); Scene scene = new Scene(root); root.setPadding(new Insets(10)); root.setHgap(20); root.setVgap(5); ColumnConstraints column1 = new ColumnConstraints(100); ColumnConstraints column2 = new ColumnConstraints(90, 150, Double.MAX_VALUE); column2.setHgrow(Priority.ALWAYS); root.getColumnConstraints().addAll(column1, column2); . . . . . }
  • 21. Demo 5 (2/3): 21K. N. Toosi University of Technology @Override public void start(Stage primaryStage) throws Exception { . . . . . Label formLbl = new Label("Enter your First name and last name"); Label fNameLbl = new Label("First Name"); TextField fNameFld = new TextField(); Label lNameLbl = new Label("Last Name"); TextField lNameFld = new TextField(); Button saveButton = new Button("Save"); . . . . . }
  • 22. Demo 5 (3/3): 22K. N. Toosi University of Technology @Override public void start(Stage primaryStage) throws Exception { . . . . . GridPane.setHalignment(formLbl, HPos.LEFT); root.add(formLbl, 0, 0,2,1); GridPane.setHalignment(fNameLbl, HPos.LEFT); root.add(fNameLbl, 0, 1); GridPane.setHalignment(lNameLbl, HPos.LEFT); root.add(lNameLbl, 0, 2); GridPane.setHalignment(fNameFld, HPos.LEFT); root.add(fNameFld, 1, 1); GridPane.setHalignment(lNameFld, HPos.LEFT); root.add(lNameFld, 1, 2); GridPane.setHalignment(saveButton, HPos.RIGHT); root.add(saveButton, 2, 3); primaryStage.setScene(scene); primaryStage.show(); }
  • 23. FlowPane: K. N. Toosi University of Technology 23 Node2 Node4 Node3Node1 Node2 Node4 Node3 Node1 HGap VGap
  • 24. Demo 6: 24K. N. Toosi University of Technology @Override public void start(Stage primaryStage) throws Exception { FlowPane root = new FlowPane(); root.setHgap(10); root.setVgap(30); root.setPadding(new Insets(15)); Button button1 = new Button("Button1"); root.getChildren().add(button1); Button button2 = new Button("Button2"); button2.setPrefSize(100, 100); root.getChildren().add(button2); TextField textField = new TextField("Text Field"); textField.setPrefWidth(110); root.getChildren().add(textField); Rectangle rectangle = new Rectangle(300, 200); root.getChildren().add(rectangle); Scene scene = new Scene(root, 550, 250); primaryStage.setScene(scene); primaryStage.show(); }
  • 25. Demo 6: 25K. N. Toosi University of Technology @Override public void start(Stage primaryStage) throws Exception { FlowPane root = new FlowPane(); root.setHgap(10); root.setVgap(30); root.setPadding(new Insets(15)); Button button1 = new Button("Button1"); root.getChildren().add(button1); Button button2 = new Button("Button2"); button2.setPrefSize(100, 100); root.getChildren().add(button2); TextField textField = new TextField("Text Field"); textField.setPrefWidth(110); root.getChildren().add(textField); Rectangle rectangle = new Rectangle(300, 200); root.getChildren().add(rectangle); Scene scene = new Scene(root, 550, 250); primaryStage.setScene(scene); primaryStage.show(); } FlowPane root = new FlowPane(); root.setHgap(10); root.setVgap(30); root.setPadding(new Insets(15));
  • 26. Demo 6: 26K. N. Toosi University of Technology @Override public void start(Stage primaryStage) throws Exception { FlowPane root = new FlowPane(Orientation.VERTICAL); root.setHgap(10); root.setVgap(30); root.setPadding(new Insets(15)); Button button1 = new Button("Button1"); root.getChildren().add(button1); Button button2 = new Button("Button2"); button2.setPrefSize(100, 100); root.getChildren().add(button2); TextField textField = new TextField("Text Field"); textField.setPrefWidth(110); root.getChildren().add(textField); Rectangle rectangle = new Rectangle(300, 200); root.getChildren().add(rectangle); Scene scene = new Scene(root, 550, 250); primaryStage.setScene(scene); primaryStage.show(); } FlowPane root = new FlowPane(Orientation.VERTICAL); root.setHgap(10); root.setVgap(30); root.setPadding(new Insets(15)); A horizontal flowpane (the default) will layout nodes in rows, wrapping at the flowpane's width. A vertical flowpane lays out nodes in columns, wrapping at the flowpane's height.
  • 27. TilePane: K. N. Toosi University of Technology 27 Node2 Node4 Node3Node1 Node2 Node4Node3 Node1
  • 28. Demo 7: 28K. N. Toosi University of Technology @Override public void start(Stage primaryStage) throws Exception { TilePane root = new TilePane(Orientation.HORIZONTAL); root.setHgap(10); root.setVgap(30); root.setPadding(new Insets(15)); root.setTileAlignment(Pos.TOP_CENTER); Button button1 = new Button("Button1"); root.getChildren().add(button1); Button button2 = new Button("Button2"); button2.setPrefSize(100, 100); root.getChildren().add(button2); TextField textField = new TextField("Text Field"); textField.setPrefWidth(110); root.getChildren().add(textField); Rectangle rectangle = new Rectangle(200, 200); root.getChildren().add(rectangle); Scene scene = new Scene(root, 550, 250); primaryStage.setScene(scene); primaryStage.show(); }
  • 29. DialogPane: • We will talk about Dialogs separately. K. N. Toosi University of Technology 29
  • 30. JavaFX Containers Hierarchy: 30K. N. Toosi University of Technology Region Control SplitPane ScrollPane Accordion TabPane Pane AnchorPane BorderPane DialogPane FlowPane VBox HBox GridPane StackPane TilePane
  • 31. Control versus Pane: • A "Control" is a node in the scene graph which can be manipulated by the user. Controls provide additional variables and behaviors beyond those of Node to support common user interactions in a manner which is consistent and predictable for the user. • Pane is the base class for layout panes which need to expose the children list as public so that users of the subclass can freely add/remove children. K. N. Toosi University of Technology 31
  • 32. ScrollPane: K. N. Toosi University of Technology 32 Some very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very Long Text Some very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very
  • 33. Demo 8: 33K. N. Toosi University of Technology @Override public void start(Stage primaryStage) throws Exception { StackPane stackPane = new StackPane(); ScrollPane root = new ScrollPane(); root.setPannable(true); root.setFitToHeight(true); root.setFitToWidth(true); Rectangle rectangle = new Rectangle(900, 400); stackPane.getChildren().add(rectangle); root.setContent(stackPane); Scene scene = new Scene(root, 600, 600); primaryStage.setScene(scene); primaryStage.show(); }
  • 34. SplitPane: K. N. Toosi University of Technology 34 RIGHTBOTTOM BOTTOM Divider Divider Position between [0.0 – 1.0]
  • 35. Demo 9: 35K. N. Toosi University of Technology @Override public void start(Stage primaryStage) throws Exception { SplitPane root = new SplitPane(); Button button21 = new Button("Click Me21"); Button button22 = new Button("Click Me22"); VBox vBox1 = new VBox(button21,button22); Button button11 = new Button("Click Me11"); Button button12 = new Button("Click Me12"); BorderPane borderPane = new BorderPane(null, button12, null, button11, null); Button button31 = new Button("Click Me31"); Button button32 = new Button("Click Me32"); VBox vBox2 = new VBox(button31,button32); root.setDividerPositions(0.2f,0.8f); root.getItems().addAll(vBox1,borderPane,vBox2); Scene scene = new Scene(root,500,400); primaryStage.setScene(scene); primaryStage.show(); } 20% 80% 100%
  • 36. TabPane: K. N. Toosi University of Technology 36 Tab3Tab1 Tab2 Tab Content
  • 37. Demo 10: 37K. N. Toosi University of Technology @Override public void start(Stage primaryStage) throws Exception { TabPane root = new TabPane(); Tab tab1 = new Tab("First Tab"); tab1.setContent(new Button("Hello")); Tab tab2 = new Tab("Second Tab"); tab2.setContent(new Button("World")); root.getTabs().addAll(tab1,tab2); Scene scene = new Scene(root); primaryStage.setScene(scene); primaryStage.show(); }
  • 38. Live Coding: UI Sketch to JavaFX 38K. N. Toosi University of Technology
  • 40. Brought to you by Happy Coding  40K. N. Toosi University of Technology