Unit 8: GUI
A GUI environment must be event-driven
An event is an action that takes place within a program, such as the clicking of a button.
Part of writing a GUI application is creating event listeners. An event listener is a method that automatically executes when a specific event occurs.
Intro to JavaFX
In memory, the GUI objects
The Application Class
An abstract class that is the foundation of all JavaFX applications
JavaFX applications must extend the Application class
The Application class has an abstract method named start, which is the entry point for the application
Because the start method is abstract, you must override it.
The scene graph has three types of nodes:
- Root Node: There is only one root node, which is the parent of all the other nodes in the scene graph.
- Branch Node: A node that can have other nodes as children
- Leaf Node: A node that cannot have children
Creating Controls
To create a scene, you instantiate the Scene class (in the javafx.scene package)
Then, you add your root node to the scene
Once you've created a Scene object, you add it to the application's stage.
The stage is an instance of the Stage class
You do not have to instantiate the Stage class, however. It is created automatically and passed as an argument to the start method.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.geometry.Pos;
public class HelloWorld extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage)
{
Label messageLabel = new Label("Hello World");
VBox vbox = new VBox(messageLabel);
vbox.setAlignment(Pos.CENTER); Center-align the VBox
Scene scene = new Scene(vbox , 300, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
}Quiz
Management system using files and such
todo list
book library managements
Interface and files
Import system libraries
implements interface
Creating and drawing uml diagrams
Handling Events
An event is an action that takes place within a program, such as the clicking of a button.
When an event takes place, the control that is responsible for the event creates an event object in memory.
The event object contains information about the event.
The control that generated the event object is know as the event source.
It is possible that the event source is connected to one or more event listeners.
You write event handler classes that implement the EventHandler interface (from the javafx.event package).
The EventHandler interface specifies a void method named handle that has a parameter of the Event class (or one of its subclasses)
class ButtonClickHandler implements EventHandler<ActionEvent> {
@Override
void handle(ActionEvent event) {
// Write event handling code here.
}
}
mybutton.setOnAction(new ButtonClickHandler());Uses BorderPane in Lab9
Top has button for set, play, and size of head scroll bar.
In center, container of type grid and has rows/columns. keep an array to know which cells are empty.