JavaFX Basics
Why?
Not everything has to be in command-line interfaces (CLIs).
Think of the suffers your grandparents face for using CLIs!
Your apps shall be user-friendly to get many users!
2
3
4
5
Intro
JavaFX is a library for creating GUIs in Java.
OOPs are heavily utilized in JavaFX.
A JavaFX application can run both on a desktop and
a Web browser.
6
Hello
import [Link];
import [Link];
import [Link];
import [Link];
// hello world
public class GUI00 extends Application {
@Override
public void start(Stage stg) throws Exception {
Label label = new Label("Hello world from JavaFX!");
Scene scene = new Scene(label);
[Link](scene);
[Link]();
}
public static void main(String[] args) {
[Link](args);
}
7
}
Hello
8
Hello
import [Link];
import [Link];
import [Link];
import [Link]; Analyze the OOP of this Hello code!
// hello world
public class GUI00 extends Application {
@Override
public void start(Stage stg) throws Exception {
Label label = new Label("Hello world from JavaFX!");
Scene scene = new Scene(label);
[Link](scene);
[Link]();
}
public static void main(String[] args) {
[Link](args);
}
9
}
Hello
import [Link];
import [Link];
import [Link];
import [Link];
// hello world
public class GUI00 extends Application {
@Override
public void start(Stage stg) throws Exception {
Label label = new Label("Hello world from JavaFX!");
Scene scene = new Scene(label);
Label is a non-editable text control. A Label is
[Link](scene);
[Link](); useful for displaying text that is required to fit
} within a specific space.
public static void main(String[] args) {
[Link](args);
}
10
}
Hello
import [Link];
import [Link];
import [Link];
import [Link];
// hello world
public class GUI00 extends Application {
@Override
public void start(Stage stg) throws Exception {
Label label = new Label("Hello world from JavaFX!");
Scene scene = new Scene(label);
[Link](scene);
[Link](); The JavaFX Scene class is the container for all
} content in a scene graph.
public static void main(String[] args) {
[Link](args);
}
11
}
Hello
import [Link];
import [Link];
import [Link];
import [Link];
// hello world
public class GUI00 extends Application {
@Override
public void start(Stage stg) throws Exception {
Label label = new Label("Hello world from JavaFX!");
A Stage in JavaFX is a top-level container that
Scene scene = new Scene(label);
[Link](scene); hosts scenes. The primary stage is created by
[Link](); the platform and passed to the start(Stage stg)
}
method of the Application class.
public static void main(String[] args) {
[Link](args);
}
12
}
13
The very basics of JavaFX
import [Link];
import [Link];
import [Link];
import [Link];
public class GUI001 extends Application {
@Override
public void start(Stage stg) throws Exception {
Button button = new Button("Readyyy!");
Scene scene = new Scene(button, 250, 100);
[Link]("GUI 001");
[Link](scene);
[Link]();
}
public static void main(String[] args) {
[Link](args);
}
} 14
The very basics of JavaFX
import [Link];
import [Link];
import [Link];
import [Link];
public class GUI001 extends Application {
@Override
public void start(Stage stg) throws Exception {
Button button = new Button("Readyyy!");
Scene scene = new Scene(button, 250, 100);
[Link]("GUI 001");
[Link](scene);
[Link]();
}
public static void main(String[] args) {
[Link](args);
}
} 15
The very basics of JavaFX
import [Link];
import [Link];
import [Link];
import [Link];
public class GUI001 extends Application {
@Override
public void start(Stage stg) throws Exception {
Button button = new Button("Readyyy!");
Scene scene = new Scene(button, 250, 100);
[Link]("GUI 001");
[Link](scene);
[Link]();
}
public static void main(String[] args) {
[Link](args);
}
Add [Link](false);
} and what would happen?
16
Multiple stages
// ... other code parts follow previous code
public void start(Stage stg1) throws Exception {
[Link]("GUI 002");
[Link](new Scene(new Button("Gooo!"), 200,100));
[Link]();
Stage stg2 = new Stage();
[Link]("GUI 002");
[Link](new Scene(new Button("Steadyyy!"), 200,100));
[Link]();
Stage stg3 = new Stage();
[Link]("GUI 002");
[Link](new Scene(new Button("Readyyy!"), 200,100));
[Link]();
}
17
Multiple stages
// ... other code parts follow previous code
public void start(Stage stg1) throws Exception {
[Link]("GUI 002");
[Link](new Scene(new Button("Gooo!"), 200,100));
[Link]();
Stage stg2 = new Stage();
[Link]("GUI 002");
[Link](new Scene(new Button("Steadyyy!"), 200,100));
[Link]();
Stage stg3 = new Stage();
[Link]("GUI 002");
[Link](new Scene(new Button("Readyyy!"), 200,100));
[Link]();
}
Close the window, you'll see the Steadyyy! stage,
close the window again, you'll see the Gooo! stage.
18
A pane is like a container
public void start(Stage stg1) throws Exception {
StackPane pn = new StackPane();
[Link]().add(new Button("Niceee!")); // because Nice! would sound rude
Scene scn = new Scene(pn, 300, 100);
[Link](scn);
[Link]();
}
19
A pane is like a container
public void start(Stage stg1) throws Exception {
StackPane pn = new StackPane();
[Link]().add(new Button("Niceee!")); // because Nice! would sound rude
Scene scn = new Scene(pn, 300, 100);
[Link](scn);
[Link]();
}
Note that the button now doesn't occupy the whole scene, thanks to Pane!
20
Stacked square and button
public void start(Stage stg1) throws Exception {
Rectangle rect = new Rectangle(100, 100, 180, 140);
[Link]([Link]);
StackPane pn = new StackPane(rect, new Button("Niceee!"));
Scene scn = new Scene(pn, 500, 200);
[Link](scn);
[Link]();
}
21
Stacked square and button
public void start(Stage stg1) throws Exception {
Rectangle rect = new Rectangle(100, 100, 180, 140);
[Link]([Link]);
StackPane pn = new StackPane(rect, new Button("Niceee!"));
Scene scn = new Scene(pn, 500, 200);
[Link](scn);
[Link]();
}
22
Quiztime: Java art
23
Quiztime: Java art
public void start(Stage stg1) throws Exception {
StackPane pn = new StackPane();
for(int i = 100; i > 0; i--) {
Rectangle rect = new Rectangle(0,0,5*i,5*i);
switch(i % 3) {
case 0:
[Link]([Link]); break;
case 1:
[Link]([Link]); break;
case 2:
[Link]([Link]); break;
}
[Link]().add(rect);
}
Scene scn = new Scene(pn, 500, 500);
[Link](scn);
[Link]();
}
24
Circle
public void start(Stage stg1) throws Exception {
Circle c = new Circle();
[Link](200);
[Link](100);
[Link](50);
[Link]([Link]);
[Link](5);
[Link]([Link]);
Pane pn = new Pane();
[Link]().add(c);
Scene scn = new Scene(pn, 400, 200);
[Link]("Life's like a circle");
[Link](scn);
[Link]();
}
25
Circle
public void start(Stage stg1) throws Exception {
Circle c = new Circle();
[Link](200);
[Link](100);
[Link](50);
[Link]([Link]);
[Link](5);
[Link]([Link]);
Pane pn = new Pane();
[Link]().add(c);
Scene scn = new Scene(pn, 400, 200);
[Link]("Life's like a circle");
[Link](scn);
[Link]();
}
26
Circle
public void start(Stage stg1) throws Exception {
Circle c = new Circle();
[Link](70);
[Link](100);
[Link](50);
[Link]([Link]);
[Link](5);
[Link]([Link]);
Pane pn = new Pane();
[Link]().add(c);
Scene scn = new Scene(pn, 400, 200);
[Link]("Life's like a circle");
[Link](scn);
[Link]();
}
27
Circle
public void start(Stage stg1) throws Exception {
Circle c = new Circle();
[Link](70);
[Link](0);
[Link](50);
[Link]([Link]);
[Link](5);
[Link]([Link]);
Pane pn = new Pane();
[Link]().add(c);
Scene scn = new Scene(pn, 400, 200);
[Link]("Life's like a circle");
[Link](scn);
[Link]();
}
28
Quiztime: Loops of circles
29
Solution
public Circle createCircle(int X, int Y) {
Random r = new Random();
Circle c = new Circle();
[Link](X);
[Link](Y);
[Link](30);
[Link]([Link]([Link](256), [Link](256), [Link](256)));
return c;
}
// ... cont
30
Solution
// ... cont
@Override
public void start(Stage stg1) throws Exception {
Pane pn = new Pane();
for(int i = 0; i < 5; i++)
for(int j = 0; j < 5; j++)
[Link]().add(createCircle(50 + i*65,50 + j*65));
Scene scn = new Scene(pn, 360, 360);
[Link]("Circles");
[Link](scn);
[Link](false);
[Link]();
}
31
JavaFX provides many other shapes
Image copyright: [Link]
32
Quiztime: We love buttons
33
Solution
@Override
public void start(Stage stg1) throws Exception {
VBox pn = new VBox();
[Link]([Link]);
[Link](20);
for(int i = 1; i <= 9; i++)
[Link]().add(new Button("Button " + i));
Scene scn = new Scene(pn, 300, 500);
[Link]("We love buttons");
[Link](scn);
[Link](false);
[Link]();
}
34
Quiztime: We love horizontal buttons
35
Solution
@Override
public void start(Stage stg1) throws Exception {
HBox pn = new HBox();
[Link]([Link]);
[Link](20);
for(int i = 1; i <= 9; i++)
[Link]().add(new Button("Button " + i));
Scene scn = new Scene(pn, 800, 200);
[Link]("We love buttons");
[Link](scn);
[Link](false);
[Link]();
}
36
Toggle Buttons
37
Solution
@Override
public void start(Stage primaryStage) throws Exception {
[Link]("HBox Experiment 1");
ToggleButton toggleButton1 = new ToggleButton("Left");
HBox hbox = new HBox(toggleButton1);
Scene scene = new Scene(hbox, 200, 100);
[Link](scene);
[Link]();
}
38
JavaFX Labels and Images
39
Solution
public void start(Stage stg1) throws Exception {
VBox pn = new VBox();
[Link]([Link]);
[Link](20);
[Link]().add(new Label("Happy Ramadhan!"));
FileInputStream fis = new FileInputStream("pics/[Link]");
Image img = new Image(fis);
ImageView iv = new ImageView(img);
[Link](400);
[Link](true);
[Link]().add(iv);
Scene scn = new Scene(pn, 500, 500);
[Link]("Happy Ramadhan!");
[Link](scn);
[Link](false);
[Link]();
}
40
Quiztime: Create your own Happy Ramadhan (or
Merry X-Mas, or any greetings!)
41
ChoiceBox
42
ChoiceBox: Code
public void start(Stage stg1) throws Exception {
VBox pn = new VBox();
[Link]([Link]);
[Link](20);
[Link]().add(new Label("Pick your fav K-pop star!"));
String[] stars = {"Joo Ko-Wee", "Park Bo-Wow"};
ChoiceBox cb = new
ChoiceBox([Link](stars));
[Link]().add(cb);
Scene scn = new Scene(pn, 180, 150);
[Link](scn);
[Link](false);
[Link]();
}
43
TextField
44
TextField: Code
public void start(Stage stg1) throws Exception {
HBox pn = new HBox();
[Link]([Link]);
[Link](20);
[Link]().add(new Label("Who's fav K-pop star?"));
TextField tf = new TextField();
[Link](150);
[Link]().add(tf);
Scene scn = new Scene(pn, 300, 100);
[Link](scn);
[Link]();
}
45
CheckBox
46
CheckBox: Code
public void start(Stage stg1) throws Exception {
VBox pn = new VBox();
[Link]([Link]);
[Link](20);
[Link]().add(new Label("Who are your fav K-pop stars?"));
CheckBox cb1 = new CheckBox("Joo Ko-Wee");
CheckBox cb2 = new CheckBox("Park Bo-Wow");
CheckBox cb3 = new CheckBox("Sandiaga Yunho");
[Link]().add(cb1);
[Link]().add(cb2);
[Link]().add(cb3);
Scene scn = new Scene(pn, 200, 200);
[Link](scn);
[Link]();
}
47
Inspired by:
Liang, Introduction to Java, 10th edition, Pearson.
[Link]
Google