L14 JavaFXBasics
L14 JavaFXBasics
2
(c) Paul Fodor and Pearson Inc.
Graphical User Interfaces (GUIs)
Graphical User Interfaces (GUIs)
provides user-friendly human interaction
they were the initial motivation for object-oriented programming
predefined classes for GUI components, event processing interfaces
Building GUIs require use of GUI frameworks:
JavaFX (part of JSE 8, 2014)
Older Java frameworks:
Abstract Window Toolkit (AWT):
java.awt.*, java.awt.geom.*,
java.awt.event.*
SWING:
javax.swing.*, javax.swing.event.*
3
(c) Paul Fodor and Pearson Inc.
What does a GUI framework do for you?
Provides ready, interactive, customizable components
and event handling management
you wouldn’t want to have to code your own window class, label class,
text fields and text areas classes, button class, checkbox, radio buttons,
lists, geometrical figures, containers/layout managers, etc.
4
(c) Paul Fodor and Pearson Inc.
JavaFX vs AWT and Swing
Swing and AWT were older Java frameworks replaced by the
JavaFX platform for developing rich Internet applications in JDK8.
When Java was introduced, the GUI classes were bundled in a library
known as the Abstract Windows Toolkit (AWT).
AWT was prone to platform-specific bugs
AWT was fine for developing simple graphical user interfaces, but not for
developing comprehensive GUI projects.
The AWT user-interface components were replaced by a more robust,
versatile, and flexible library known as Swing components.
Swing components were painted directly on canvases using Java code.
Swing components depended less on the target platform and used less of the
native GUI resource.
With the release of Java 8, Swing was replaced by a
completely new GUI platform: JavaFX.
5
(c) Paul Fodor and Pearson Inc.
How do GUIs work?
• GUIs loop and respond to events:
• Example: a mouse click on a button
• The Operating System recognizes mouse
click, determines which window it was Construct GUI Components
inside and notifies that program by
putting the event on that program's input
buffer/queue
• The program runs in loop: Render GUI
6
(c) Paul Fodor and Pearson Inc.
GUI Look vs. Behavior
Look: physical appearance
GUIs components
containment and layout management
Behavior: responding to events
event programmed response through
event handlers that we define
7
(c) Paul Fodor and Pearson Inc.
Stage
Scene
Basic Structure of a
Button JavaFX GUI
javafx.application.Application is
the entry point for JavaFX applications
JavaFX creates an application thread for running the
application start method, processing input events,
and running animation timelines.
We override the start(Stage) method!
javafx.stage.Stage is the top level JavaFX
container (i.e., window)
The primary Stage is constructed by the platform.
javafx.scene.Scene class is the container
for all content in a scene graph in the stage.
javafx.scene.Node is the base class for
8
scene graph nodes (i.e., components).
(c) Paul Fodor and Pearson Inc.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
9
(c) Paul Fodor and Pearson Inc.
// Multiple stages can be added beside the primaryStage
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
11
(c) Paul Fodor and Pearson Inc.
Layout Panes
JavaFX provides many types of panes for organizing nodes
in a container.
12
(c) Paul Fodor and Pearson Inc.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.control.Button;
13
(c) Paul Fodor and Pearson Inc.
FlowPane
14
(c) Paul Fodor and Pearson Inc.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.geometry.Insets;
public class ShowFlowPane extends Application {
@Override
public void start(Stage primaryStage) {
FlowPane pane = new FlowPane();
pane.setPadding(new Insets(11, 12, 13, 14));
pane.setHgap(5);
pane.setVgap(5);
// Place nodes in the pane
pane.getChildren().addAll(new Label("First Name:"),
new TextField(), new Label("MI:"));
TextField tfMi = new TextField();
tfMi.setPrefColumnCount(1);
pane.getChildren().addAll(tfMi, new Label("Last Name:"),
new TextField());
// Create a scene and place it in the stage
Scene scene = new Scene(pane, 210, 150);
primaryStage.setTitle("ShowFlowPane");
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
public static void main(String[] args) {
launch(args);
15 }
(c) Paul Fodor and Pearson Inc.
}
GridPane
16
(c) Paul Fodor and Pearson Inc.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.control.*;
import javafx.geometry.*;
public class ShowGridPane extends Application {
@Override
public void start(Stage primaryStage) {
// Create a pane and set its properties
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setHgap(5.5);
pane.setVgap(5.5);
// Place nodes in the pane at positions column,row
pane.add(new Label("First Name:"), 0, 0);
pane.add(new TextField(), 1, 0);
pane.add(new Label("MI:"), 0, 1);
pane.add(new TextField(), 1, 1);
pane.add(new Label("Last Name:"), 0, 2);
pane.add(new TextField(), 1, 2);
Button btAdd = new Button("Add Name");
pane.add(btAdd, 1, 3);
GridPane.setHalignment(btAdd, HPos.RIGHT);
// Create a scene and place it in the stage
Scene scene = new Scene(pane);
primaryStage.setTitle("ShowGridPane");
primaryStage.setScene(scene); primaryStage.show(); }
public static void main(String[] args) {
17 launch(args);
(c) Paul Fodor and Pearson Inc.
}}
BorderPane
18
(c) Paul Fodor and Pearson Inc.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.control.Label;
import javafx.geometry.Insets;
public class ShowBorderPane extends Application {
@Override
public void start(Stage primaryStage) {
BorderPane pane = new BorderPane();
pane.setTop(new CustomPane("Top"));
pane.setRight(new CustomPane("Right"));
pane.setBottom(new CustomPane("Bottom"));
pane.setLeft(new CustomPane("Left"));
pane.setCenter(new CustomPane("Center"));
Scene scene = new Scene(pane);
primaryStage.setScene(scene); primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
class CustomPane extends StackPane {
public CustomPane(String title) {
getChildren().add(new Label(title));
setStyle("-fx-border-color: red");
setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
}
19 }
(c) Paul Fodor and Pearson Inc.
Hbox and VBox
20
(c) Paul Fodor and Pearson Inc.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class ShowHBoxVBox extends Application {
@Override
public void start(Stage primaryStage) {
BorderPane pane = new BorderPane();
HBox hBox = new HBox(15);
hBox.setStyle("-fx-background-color: gold");
hBox.getChildren().add(new Button("Computer Science"));
hBox.getChildren().add(new Button("CEWIT"));
ImageView imageView = new ImageView(new Image("cs14.jpg"));
hBox.getChildren().add(imageView);
pane.setTop(hBox);
VBox vBox = new VBox(15);
vBox.getChildren().add(new Label("Courses"));
Label[] courses = {new Label("CSE114"), new Label("CSE214"),
new Label("CSE219"), new Label("CSE308")};
for (Label course: courses) {
vBox.getChildren().add(course);
}
pane.setLeft(vBox);
21 Scene scene = new Scene(pane); primaryStage.setScene(scene);
primaryStage.show(); (c) Paul Fodor and Pearson Inc.
Display Shapes
22
(c) Paul Fodor and Pearson Inc.
Shapes
JavaFX provides many shape classes for drawing texts,
lines, circles, rectangles, ellipses, arcs, polygons, and
polylines.
23
(c) Paul Fodor and Pearson Inc.
Text
24
(c) Paul Fodor and Pearson Inc.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.geometry.Insets;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.FontPosture;
public class ShowText extends Application {
@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
pane.setPadding(new Insets(5, 5, 5, 5));
Text text1 = new Text(20, 20, "Programming is fun");
text1.setFont(Font.font("Courier", FontWeight.BOLD,
FontPosture.ITALIC, 15));
pane.getChildren().add(text1);
Text text2 = new Text(60, 60, "Programming is fun\nDisplay text");
pane.getChildren().add(text2);
Text text3 = new Text(10, 100, "Programming is fun\nDisplay text");
text3.setFill(Color.RED);
text3.setUnderline(true);
text3.setStrikethrough(true);
pane.getChildren().add(text3);
Scene scene = new Scene(pane, 600, 800);
primaryStage.setScene(scene); primaryStage.show();
}
25 ...
} (c) Paul Fodor and Pearson Inc.
Helper classes: The Color Class
26
(c) Paul Fodor and Pearson Inc.
Helper classes: The Font Class
27
(c) Paul Fodor and Pearson Inc.
Line
28
(c) Paul Fodor and Pearson Inc.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.scene.paint.Color;
public class ShowLine extends Application {
@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
Line line1 = new Line(10, 10, 10, 10);
line1.endXProperty().bind(pane.widthProperty().subtract(10));
line1.endYProperty().bind(pane.heightProperty().subtract(10));
line1.setStrokeWidth(5);
line1.setStroke(Color.GREEN);
pane.getChildren().add(line1);
Line line2 = new Line(10, 10, 10, 10);
line2.startXProperty().bind(pane.widthProperty().subtract(10));
line2.endYProperty().bind(pane.heightProperty().subtract(10));
line2.setStrokeWidth(5);
line2.setStroke(Color.GREEN);
pane.getChildren().add(line2);
Scene scene = new Scene(pane, 200, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
29 }
(c) Paul Fodor and Pearson Inc.
Rectangle
30
(c) Paul Fodor and Pearson Inc.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import java.util.Collections;
public class ShowRectangle extends Application {
public void start(Stage primaryStage) {
Pane pane = new Pane();
Rectangle r1 = new Rectangle(25, 10, 60, 30);
r1.setStroke(Color.BLACK);
r1.setFill(Color.WHITE);
pane.getChildren().add(new Text(10, 27, "r1"));
pane.getChildren().add(r1);
Rectangle r2 = new Rectangle(25, 50, 60, 30);
pane.getChildren().add(new Text(10, 67, "r2"));
pane.getChildren().add(r2);
for (int i = 0; i < 4; i++) {
Rectangle r = new Rectangle(100, 50, 100, 30);
r.setRotate(i * 360 / 8);
r.setStroke(Color.color(Math.random(), Math.random(),
Math.random()));
r.setFill(Color.WHITE);
pane.getChildren().add(r);
}
Scene scene = new Scene(pane, 250, 150);
primaryStage.setScene(scene); primaryStage.show();
31 }
...// main (c) Paul Fodor and Pearson Inc.
Circle
32
(c) Paul Fodor and Pearson Inc.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene; Circle in a Pane
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
33
(c) Paul Fodor and Pearson Inc.
Ellipse
radiusX radiusY
(centerX, centerY)
34
(c) Paul Fodor and Pearson Inc.
Arc radiusY length
startAngle
0 degree
radiusX
(centerX, centerY)
35
(c) Paul Fodor and Pearson Inc.
Polygon and Polyline
The getter and setter methods for property values and a getter for property
javafx.scene.shape.Polygon itself are provided in the class, but omitted in the UML diagram for brevity.
36
(c) Paul Fodor and Pearson Inc.
The Image and ImageView Classes
37
(c) Paul Fodor and Pearson Inc.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.HBox;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.geometry.Insets;
39
(c) Paul Fodor and Pearson Inc.
JavaFX CSS style and Node rotation
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.control.Button;
40
(c) Paul Fodor and Pearson Inc.
JavaFX External CSS style file
// Example to load and use a CSS style file in a scene
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;