SDA Lect11 Fall2024 JavaFX
SDA Lect11 Fall2024 JavaFX
CS-3004
Lecture#10
• It is mainly made of graphical components like buttons, labels, windows, etc. through which
the user can interact with an application.
• GUI plays an important role to build easy interfaces for Java applications.
2
Graphical User Interface
3
JavaFX
• JavaFX is the latest graphical user interface framework. It is a
platform for making a really amazing looking GUI application.
• After the advent of JavaFX, these Java programmers can now develop GUI
applications effectively with rich content.
5
JavaFX vs Swing vs AWT
6
Steps to create a JavaFX Program
1. Extend Application
2. Override start (Stage)
3. Create Nodes (e.g., Button)
4. Place the Nodes in the Scene
5. Place the Scene on Stage
6. Show Stage
7
Panes, UI Controls, and Shapes
8
Example
9
UI Component
10
Main Building Blocks
1. Stage
2. Scene
3. Nodes
4. Event Handlers
5. Timelines and transitions for animation purposes
11
Layout Panes
12
Example
13
User Interface Code
• File->new->other->JavaFX project
Main Frame
GUI Widgets
Select Layout
Container
15
Cont..
16
Cont..
17
Install JavaFX
18
Search for e(fx)clipse
https://download.eclipse.org/efxclipse/updates-released/3.5.0/site/ 19
Scene Builder
• https://gluonhq.com/products/scene-builder/
20
Scene Builder
Property
Widget Corner View
Corner
21
Adding Scene Builder in Eclipse
22
Event Handling in JavaFX
• Writing GUI applications requires that program control be driven by the user's interaction with the GUI.
• When the user moves the mouse, clicks on a button, or selects an item from a menu an “event” occurs.
23
Event Driven Programming
• Graphics applications use events.
• An event dispatcher receives events and notifies interested objects.
Event Listener
24
Example
1. User clicks mouse on a button -- that's an Event.
2. JavaFX creates a MouseEvent object.
– the MouseEvent describes what happened (which mouse button
was presses, which field it was in).
3. JavaFX looks for a registered "Event Listener", and calls it using the
MouseEvent as parameter.
My Event Listener
Click! MouseEvent void handle( Event event ) {
notify
MyButton
26
Check the Event class API
All Events are subclasses of Event.
Event
• ActionEvent
• InputEvent
KeyEvent
MouseEvent
• WebEvent
• WindowEvent
27
Source of Events
• A component or node can be "source" of many kinds of events. Some event types are different
for each node or component.
• Its not complicated! Mostly you can guess event types.
28
What is an EventHandler?
JavaFX has just one interface for all kinds of Event
Handlers. This is a lot simpler than Swing and AWT.
<<interface>>
EventHandler<T extends Event>
29
Example: ENTER or Button click
1. User types his name and clicks a button (or ENTER)
...
31
2 Ways to Add Event Handler (demo)
// 1. use addEventHandler:
button.addEventHandler(ActionEvent.ALL,new
ButtonHandler( ) )
// 2. use setOnAction
button.setOnAction( new ButtonHandler( ) )
• Notice that the EventHandler is the same. The result will be the same, too.
• Both add Event Handler for ActionEvents.
32
You can re-use event handlers
• For clarity, or to reuse the same event handler on many components,
assign new event handler to a reference variable first.
33
Don't Create Duplicate Handlers
It is bad programming to create two objects to
do the same thing (greet the user).
// don't do this
button.setOnAction( new ButtonHandler() );
nameField.setOnAction( new ButtonHandler() );
34
4 Ways to Define an EventHandler
35
Event Handler as Anonymous Class
You must specify what interface you are implementing,
including type parameter.
EventHandler<ActionEvent> buttonHandler =
new EventHandler<ActionEvent>() {
// anonymous class definition:
public void handle(ActionEvent evt) {
String text = nameField.getText();
//TODO greet user using Alert box
nameField.setText(""); // clear
input
}
};
36
button.setOnAction( buttonHandler );
Avoid inline definition & use
This is hard to understand and hard to maintain.
Avoid it. Define the anonymous class first, then use
it.
// This is harder to understand, especially
// when the anonymous class is long.
button.setOnAction(
new EventHandler<ActionEvent>()
{ public void handle(ActionEvent
evt) {
String text = nameField.getText();
//TODO greet user using Alert box
nameField.setText(""); // clear
input 37
Method as Event Handler?
Using SceneBuilder to assign event handlers
we did not write inner classes or anonymous classes.
We just wrote a method, like this:
@FXML
public void greetTheUser(ActionEvent evt) {
String text = nameField.getText();
//TODO greet user using Alert box
nameField.setText(""); //
clear input
}
EventHandler<ActionEvent> buttonHandler =
(event) -> {
String text = nameField.getText();
//TODO greet user using Alert box
nameField.setText("");
} ;
button.setOnAction( buttonHandler );
40
5th Way to Define Event Handler
You can define the controller itself as "implements
EventHandler<T>" and use "setOnAction( this )".
class GreetController
implements EventHandler<ActionEvent> {
@FXML
public void initialize()
{ button.setOnAction( this
);
...
42