5
Introduction to Software Systems 1110/1140/1510/6710 JavaFX and Event Handling JavaFX 2 X2

JavaFX 2 - Research School of Computer Science · JavaFX 2 X2 . Introduction to Software Systems 1110/1140/1510/6710 JavaFX and Event Handling ... • Methods (a method call nests

  • Upload
    vannga

  • View
    220

  • Download
    2

Embed Size (px)

Citation preview

Page 1: JavaFX 2 - Research School of Computer Science · JavaFX 2 X2 . Introduction to Software Systems 1110/1140/1510/6710 JavaFX and Event Handling ... • Methods (a method call nests

Introduction to Software Systems 1110/1140/1510/6710

JavaFX and Event Handling

JavaFX 2 X2

Page 2: JavaFX 2 - Research School of Computer Science · JavaFX 2 X2 . Introduction to Software Systems 1110/1140/1510/6710 JavaFX and Event Handling ... • Methods (a method call nests

Introduction to Software Systems 1110/1140/1510/6710

JavaFX and Event Handling

Java FX Scene Graph Tree of nodes, with a single ‘branch’ at the root •  branch (may have children, eg Group, Region) •  leaf (may not have children, eg Rectangle, Circle)

10

Copyright Oracle (http://docs.oracle.com/javafx/2/scenegraph/jfxpub-scenegraph.htm)

X2

Page 3: JavaFX 2 - Research School of Computer Science · JavaFX 2 X2 . Introduction to Software Systems 1110/1140/1510/6710 JavaFX and Event Handling ... • Methods (a method call nests

Introduction to Software Systems 1110/1140/1510/6710

JavaFX and Event Handling

Event Handling Event handling is another control flow construct. •  Branches (a conditional or switch selects control flow)

•  Loops (a loop repeats control flow)

•  Methods (a method call nests control flow)

•  Events (the occurrence of event changes control flow) –  Event handling in UIs –  Exception handling (later)

11 375-388

X2

Page 4: JavaFX 2 - Research School of Computer Science · JavaFX 2 X2 . Introduction to Software Systems 1110/1140/1510/6710 JavaFX and Event Handling ... • Methods (a method call nests

Introduction to Software Systems 1110/1140/1510/6710

JavaFX and Event Handling

Events and Passing Code in Java

An event handler will execute code you specify executed when a certain event occurs.

Q: How do I pass code as an argument in Java? A: Lambda expressions (since Java 8), see unit J09

12 375-388

X2

Page 5: JavaFX 2 - Research School of Computer Science · JavaFX 2 X2 . Introduction to Software Systems 1110/1140/1510/6710 JavaFX and Event Handling ... • Methods (a method call nests

Introduction to Software Systems 1110/1140/1510/6710

JavaFX and Event Handling

Events in JavaFX Events are instances of javafx.event.Event•  Event properties:

–  Event type –  Source –  Target

•  Event handlers –  Pass the Lambda expression. For example: scene.setOnKeyTyped(event -> { ...your code... })

13

X2