0% found this document useful (0 votes)
138 views23 pages

Javafx Events: Fariz Darari

The document discusses different ways to handle button click events in JavaFX, including using a standard class, anonymous class, and lambda expression. It then provides examples of creating buttons that change the size of a square or circle on click. Finally, it discusses using a choice box with an event handler, creating a wedding guest book, embedding a web page using WebView, and playing video files with MediaPlayer.

Uploaded by

Pembelajar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views23 pages

Javafx Events: Fariz Darari

The document discusses different ways to handle button click events in JavaFX, including using a standard class, anonymous class, and lambda expression. It then provides examples of creating buttons that change the size of a square or circle on click. Finally, it discusses using a choice box with an event handler, creating a wedding guest book, embedding a web page using WebView, and playing video files with MediaPlayer.

Uploaded by

Pembelajar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

JavaFX Events

Fariz Darari
Button Event Handler

2
Button Event Handler #1: Standard
Class
// ...

@Override
public void start(Stage mainStage) throws Exception {
Button but = new Button("Click me");
MyHandler handler = new MyHandler();
but.setOnAction(handler);
Scene sc = new Scene(but, 200, 100);
mainStage.setScene(sc);
mainStage.show();
}

// ...

3
Button Event Handler #1: Standard
Class
// ...

class MyHandler implements EventHandler<ActionEvent> {

@Override
public void handle(ActionEvent e) {
System.out.println("Clicked");
}

4
Button Event Handler #2:
Anonymous Class
// ...
@Override
public void start(Stage mainStage) throws Exception {
Button but = new Button("Click me");
but.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
System.out.println("Clicked");
}
});
Scene sc = new Scene(but, 200, 100);
mainStage.setScene(sc);
mainStage.show();
}
// ...
5
Button Event Handler #3: Lambda
Expression
// ...
@Override
public void start(Stage mainStage) throws Exception {
Button but = new Button("Click me");
but.setOnAction((e) -> {
System.out.println("Clicked");
});
Scene sc = new Scene(but, 200, 100);
mainStage.setScene(sc);
mainStage.show();
}
// ...

6
Quiztime: Create two buttons, and
bind different event handlers

7
Quiztime: Create two buttons, and bind different event
handlers
public void start(Stage mainStage) throws Exception {
VBox vb = new VBox();
vb.setAlignment(Pos.CENTER);
vb.setSpacing(20);

Button clickBut = new Button("Click me");


clickBut.setOnAction((e) -> { System.out.println("Nais boi ( ͡
° ͜
ʖ ͡
°)"); });

Button noClickBut = new Button("Don't click me");


noClickBut.setOnAction((e) -> {
System.out.println("Nooooo ༼ つ ͠ ° ͟͟ ° ༽ つ ");
ʖ ͡
System.exit(0);
});

vb.getChildren().add(clickBut);
vb.getChildren().add(noClickBut);

Scene sc = new Scene(vb, 200, 100);


mainStage.setScene(sc);
mainStage.show();
}
8
Square Size Control

9
Square Size Control
public class GUI018 extends Application {

int l = 50;

@Override
public void start(Stage mainStage) throws Exception {
BorderPane bp = new BorderPane();

Rectangle r = new Rectangle(0,0,l,l);


bp.setCenter(r);

HBox hb = new HBox();


hb.setAlignment(Pos.CENTER);
hb.setPadding(new Insets(10,10,10,10));
hb.setSpacing(10);
// ...

10
// ...
Button clickBut = new Button("Bigger");
clickBut.setOnAction((e) -> {
l += 5;
r.setWidth(l);
r.setHeight(l);
});
hb.getChildren().add(clickBut);

Button noClickBut = new Button("Smaller");


noClickBut.setOnAction((e) -> {
l -= 5;
r.setWidth(l);
r.setHeight(l);
});
hb.getChildren().add(noClickBut);

bp.setBottom(hb);

Scene sc = new Scene(bp, 500, 500);


mainStage.setScene(sc);
mainStage.show();
} 11
BorderPane
TOP

LEFT CENTER RIGHT

BOTTOM
12
Quiztime: Circle Size Control

13
Quiztime: Circle Size Control

Solution:
http://ocw.ui.ac.id/mod/resource/view.php?id=1122

14
ChoiceBox with Event Handler

15
public class GUI019 extends Application {

Text t = new Text();

@Override
public void start(Stage stg1) throws Exception {
VBox pn = new VBox();
pn.setAlignment(Pos.CENTER);
pn.setSpacing(20);
pn.getChildren().add(new Label("Pick your fav K-pop star!"));
ChoiceBox cb = new ChoiceBox(FXCollections.observableArrayList("Joo Ko-Wee", "Park
Bo-Wow"));
cb.setOnAction(e -> {
t.setText(" 사랑해 , " + ((ChoiceBox) e.getSource()).getValue() + " Oppa!");
});
pn.getChildren().add(cb);
pn.getChildren().add(t);
Scene scn = new Scene(pn, 180, 150);
stg1.setScene(scn); stg1.setResizable(false); stg1.show();
}

// ... usual
16
Wedding Guest Book

17
Wedding Guest Book

Solution:
http://ocw.ui.ac.id/mod/resource/view.php?id=1119

18
WebView

19
public void start(Stage stg) throws Exception {

WebView wv = new WebView();


WebEngine we = wv.getEngine();
we.load("http://example.com/");
VBox vb = new VBox(wv);
Scene sc = new Scene(vb);
stg.setScene(sc);
stg.show();

20
MediaPlayer

21
public void start(Stage stg) throws Exception {

StackPane root = new StackPane();


File f = new File("vids/video.mp4");
MediaPlayer player = new MediaPlayer(new Media(f.toURI().toURL().toString()));
MediaView mediaView = new MediaView(player);
root.getChildren().add(mediaView);
Scene scene = new Scene(root, 800, 600);
stg.setScene(scene);
stg.show();
player.play();

22
Inspired by:
Liang, Introduction to Java, 10th edition, Pearson.
Javadoc.

You might also like