You are on page 1of 3

Advanced Programming Tute/Lab 2018 Semester 1

Week 8

Objective: JavaFX components, GUI Events, Even Driven Programming

A - Review Questions:

1. What additional features/support are available in JavaFX (over Swing/AWT)?


2. What class should be extended to create a JavaFX application? Which method
should be overridden?
3. How is the default stage accessed in the application class?
4. What is the method responsible for displaying the stage?
5. Are the following statements correct or not?
A. An application can use multiple stages.
B. A stage can use multiple scenes.
C. A pane can contain multiple nodes.
D. A node can be an image, a UI control, a shape or a pane.
6. Briefly explain the differences between different types of layout panes:

FlowPane, GridPane, BorderPane, HBox, VBox

7. What is the difference between the Cartesian and the Java Coordinate systems?
8. How are events generated? What do events contain?
9. What are the event registration methods for:
A. Button click or enter in a text field
B. Mouse dragged
C. Mouse clicked

10. Explain the following code and simplify it using lambda expression.

btn.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});

11. How can animation be achieved in JavaFX?


B – Exercises:

1. Explain the code below. Without running the code, describe the output of this program.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.Rectangle;

public class Guess extends Application {


@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
double WIDTH = 400;
double HEIGHT = 400;
Pane pane = new Pane();
for (int i = 0; i < 8; i++) {
boolean isWhite = i % 2 == 0;
for (int j = 0; j < 8; j++) {
Rectangle rectangle
= new Rectangle(i * WIDTH / 8, j * HEIGHT / 8,
WIDTH / 8, HEIGHT / 8);
rectangle.setStroke(Color.BLACK );
if (isWhite) {
rectangle.setFill(Color.WHITE );
} else {
rectangle.setFill(Color.BLACK );
}
isWhite = !isWhite;
pane.getChildren().add(rectangle);
}
}
Scene scene = new Scene(pane, WIDTH, HEIGHT);
primaryStage.setTitle("Guess"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}

public static void main(String[] args) {


launch(args);
}
}
2. Write a Java FX program to print a bar chart as shown below. When the chart is
clicked, then the result “Tests: 20% \n Assignments: 40% \n Exam 30%” will be printed on
the console.

C – Additional Materials:

You can check out the online materials on JavaFX, especially regarding GUI elements that
you might need for your Assignment 2.

• Series of YouTube videos: JavaFX Tutorial for Beginners


https://goo.gl/9295Dx

A step by step guide on how to use SceneBuilder and how to create a login application
and how to connect JavaFX to SQLite, and many more useful tutorials

• Lynda.com course: JavaFX GUI Development

https://www.lynda.com/Java-tutorials/JavaFX-GUI-Development/466182-2.html

You might also like