Chapter-6 GUI With JavaFX
Chapter-6 GUI With JavaFX
)
BSc. CSIT 7th Semester
Chapter - 6
10/11/2023
Er. Jeewan Rai
4
package application;
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
10/11/2023 Er. Jeewan Rai 5
JavaFX Layouts
JavaFX • Layouts are the top level container classes that define the UI styles for scene graph objects.
• Layout can be seen as the parent node to all the other nodes.
• All these classes belong to javafx.scene.layout package. javafx.scene.layout.Pane class is the base class for all the
Chapter – 6 : GUI with JavaFX (3 Hrs.)
Class Description
BorderPane Organizes nodes in top, left, right, centre and the bottom of the screen.
FlowPane Organizes the nodes in the horizontal rows according to the available horizontal spaces. Wraps the nodes to
the next line if the horizontal space is less than the total width of the nodes
StackPane Organizes nodes in the form of a stack i.e. one onto another
HBox VBox
10/11/2023 Er. Jeewan Rai 7
Steps to create layout
JavaFX
• Instantiate the respective layout class.
root.setSpacing(20);
root.getChildren().addAll(<NodeObjects>);
• The vertical flowpane arranges the nodes in a column and wrap them
according to the flowpane's height.
• FlowPane layout is represented by javafx.scene.layout.FlowPane class.
• We just need to instantiate this class to create the flowpane layout.
columnHalignment The horizontal alignment of nodes within the columns. setColumnHalignment(HPos Value)
Chapter – 6 : GUI with JavaFX (3 Hrs.)
prefWrapLength The preferred height or width where content should setPrefWrapLength(double value)
wrap in the horizontal or vertical flowpane.
rowValignment The vertical alignment of the nodes within the rows. setRowValignment(VPos value)
10/11/2023 https://www.javatpoint.com/javafx-flowpane
Er. Jeewan Rai 10
package application;
import javafx.application.Application;
import javafx.scene.Scene;
Example: FlowPane
JavaFX
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
Chapter – 6 : GUI with JavaFX (3 Hrs.)
Override
Advanced JAVA Programming
primaryStage.setScene(scene);
primaryStage.show();
}
10/11/2023 Er. Jeewan Rai 11
JavaFX : BorderPane
JavaFX
• BorderPane arranges the nodes at the left, right, centre, top and bottom of
the screen.
• It is represented by javafx.scene.layout.BorderPane class.
Chapter – 6 : GUI with JavaFX (3 Hrs.)
setBottom() and setTop() which are used to set the position for the
specified nodes.
• We need to instantiate BorderPane class to create the BorderPane layout.
Node Bottom setBottom() Add the node to the bottom of the screen
Node Centre setCentre() Add the node to the centre of the screen
Chapter – 6 : GUI with JavaFX (3 Hrs.)
Node Left setLeft() Add the node to the left of the screen
Advanced JAVA Programming
Node Right setRight() Add the node to the right of the screen
Node Top setTop() Add the node to the top of the screen
import javafx.stage.Stage;
public class Label_Test extends Application {
Advanced JAVA Programming
Override
public void start(Stage primaryStage) throws Exception {
BorderPane BPane = new BorderPane();
BPane.setTop(new Label("This will be at the top"));
BPane.setLeft(new Label("This will be at the left"));
BPane.setRight(new Label("This will be at the Right"));
BPane.setCenter(new Label("This will be at the Centre"));
BPane.setBottom(new Label("This will be at the bottom"));
Scene scene = new Scene(BPane,600,400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
10/11/2023 Er. Jeewan Rai 14
}
JavaFX Layouts: HBox, VBox
JavaFX
• HBox layout pane arranges the nodes in a single row. It is represented
by javafx.scene.layout.HBox class. We just need to instantiate HBox class in
Chapter – 6 : GUI with JavaFX (3 Hrs.)
• VBox: Instead of arranging the nodes in horizontal row, Vbox Layout Pane
arranges the nodes in a single vertical column. It is represented
by javafx.scene.layout.VBox class which provides all the methods to deal
with the styling and the distance among the nodes. This class needs to be
instantiated in order to implement VBox layout in our application.
HBox VBox
10/11/2023 https://www.javatpoint.com/javafx-hbox
Er. Jeewan Rai 16
JavaFX Layouts: GridPane
JavaFX
• GridPane Layout pane allows us to add the multiple nodes in multiple rows
and columns.
• It is seen as a flexible grid of rows and columns where nodes can be placed
Chapter – 6 : GUI with JavaFX (3 Hrs.)
alignment Represents the alignment of the grid within the setAlignment(Pos value)
GridPane.
gridLinesVisible This property is intended for debugging. Lines can be setGridLinesVisible(Boolean value)
displayed to show the gidpane's rows and columns by
setting this property to true.
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Label_Test extends Application {
Advanced JAVA Programming
Override
public void start(Stage primaryStage) throws Exception {
Label first_name=new Label("First Name");
Label last_name=new Label("Last Name");
TextField tf1new TextField();
TextField tf2new TextField();
Button Submit=new Button ("Submit");
GridPane root=new GridPane();
Scene scene = new Scene(root,400,200);
root.addRow(0, first_name,tf1;
root.addRow(1, last_name,tf2;
root.addRow(2, Submit);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
https://www.javatpoint.com/javafx-gridpane
10/11/2023 18
Label
JavaFX
• Label first_name=new Label("First Name");
Chapter – 6 : GUI with JavaFX (3 Hrs.)
Advanced JAVA Programming
button1.setToggleGroup(group);
button2.setToggleGroup(group);
button3.setToggleGroup(group);
button4.setToggleGroup(group);
VBox root=new VBox();
root.setSpacing(10);
root.getChildren().addAll(button1,button2,button3,button4;
Scene scene=new Scene(root,400,300);
primaryStage.setScene(scene);
primaryStage.setTitle("Radio Button Example");
primaryStage.show();
}
root.getChildren().add(hp);
Scene scene=new Scene(root,400,300);
primaryStage.setScene(scene);
primaryStage.setTitle("Hyperlink Example");
primaryStage.show();
}
root.setTop(menubar);
menubar.getMenus().addAll(FileMenu, EditMenu);
primaryStage.setScene(scene);
primaryStage.show();
}
10/11/2023 Er. Jeewan Rai 25
ToolTip
JavaFX
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
PasswordField pf = new PasswordField();
Tooltip tool=new Tooltip();
Chapter – 6 : GUI with JavaFX (3 Hrs.)
tool.setText("Information");
pf.setTooltip(tool);
Advanced JAVA Programming
file.setTitle("Open File");
file.showOpenDialog(primaryStage);
Advanced JAVA Programming
root.setSpacing(20);
}
10/11/2023 Er. Jeewan Rai 27
Advanced JAVA Programming
Chapter – 6 : GUI with JavaFX (3 Hrs.)
JavaFX
10/11/2023
JavaFX UI Controls
gridPane.add(button1, 0, 2);
@Override gridPane.add(button2, 1, 2);
public void start(Stage stage) {
//creating label //Styling nodes
Text text1 = new Text("Email"); button1.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
Text text2 = new Text("Password"); button2.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
//Creating Text Filed for email text1.setStyle("-fx-font: normal bold 20px 'serif' ");
TextField textField1 = new TextField(); text2.setStyle("-fx-font: normal bold 20px 'serif' ");
PasswordField textField2 = new PasswordField(); gridPane.setStyle("-fx-background-color: BEIGE;");
▪ Key Event − This is an input event that indicates the key stroke occurred on a node. It is represented by
the class named KeyEvent. This event includes actions like key pressed, key released and key typed.
Advanced JAVA Programming
▪ Drag Event − This is an input event which occurs when the mouse is dragged. It is represented by the
class named DragEvent. It includes actions like drag entered, drag dropped, drag entered target, drag
exited target, drag over, etc.
▪ Window Event − This is an event related to window showing/hiding actions. It is represented by the
class named WindowEvent. It includes actions like window hiding, window shown, window hidden,
window showing
Event handling
Advanced JAVA Programming
lr.setText("Sum="+(n1+n2));
}
}
10/11/2023 https://www.tutorialspoint.com/javafx/javafx_ui_controls.htm
Er. Jeewan Rai 32
Advanced JAVA Programming
Chapter – 6 : GUI with JavaFX (3 Hrs.)
JavaFX
10/11/2023
Er. Jeewan Rai
The End
33