• No results found

EventProgramming

N/A
N/A
Protected

Academic year: 2020

Share "EventProgramming"

Copied!
104
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Event Handling

Java graphic components generate events

Buttons = when pressed, released, clicked

– TextFields = when characters are types, enter key

– CheckBoxes = checked or unchecked

Etc…

Based on these events we might want certain

actions to happen

Therefore we need to have code to detect these

(3)

Button Example Code

private Scene scene; private Button button; @Override

public void start(Stage mainFrame) {

button = new Button("Ok");

scene = new Scene(button, 300, 300); mainFrame.setScene(scene);

(4)
(5)

Event Handling

Currently nothing happens when the button is

clicked

• We need to create an event handler

Then we need to register the button with the

(6)

Creating an Event Handler

1. Can create a private inner class

2. Can create an anonymous inner class 3. Can create a separate class

4. Can use Lambda Expressions

(7)

Private Inner Class

This is a class inside your current class • Advantages

Can see all class variables

Will likely need to use them in the event handlingQuick to code

• Disadvantages

For several different types of events and many

(8)

Anonymous Inner Class

• This is a class created on-the-fly with no name

• Advantages

Quick to code

Can be created inside the parameters for setting the

event handler

• Disadvantages

Only works for one componentsUgly code

(9)

Separate Event Handling Class

Make separate classes for handling events • Advantages

– Separates event handling from UI

– Makes programs more modular

• Disadvantages

– Much more work to code

– Does not have access to the UI components directly,

(10)

Lambda Expressions

New to java 8

• Cleaner way to create an anonymous inner class

Advantages

Same as anonymous inner classesLooks nicer too

• Disadvantages

(11)

When to Use a Private Inner Class

Private Inner Classes

My preferred method

Have a lot of experience with thisNever had a problem

– The only method you really need to know

Only reasons not to use

• If you only have one or two buttons an anonymous inner class would be sufficient

(12)

When to Use an Anonymous Inner Class

Anonymous Inner Class

Never?

(13)

When to Use the Lambda Expression

Only use if you have a very small number of

components that need handlers

• The components should need separate handlers

(14)

When to Use a Separate Class

Only use if you must separate UI from event

handling

(15)

Example – ButtonHandlerExample1.java

(16)

Private Inner Class Button Handler

private class ButtonHandler implements EventHandler<ActionEvent> {

@Override

public void handle(ActionEvent e) {

if(e.getTarget() == button1) {

System.out.println("Button clicked"); }

(17)

Component Visibility

Notice how the Button object was declared

with class scope outside of the constructor

(18)

Component Visibility

private Scene scene;

private Button button;

@Override

public void start(Stage mainFrame) {

button = new Button("Ok");

scene = new Scene(button, 300, 300); mainFrame.setScene(scene);

(19)

Events

There are many types of Event

• In this case a button click corresponds to an ActionEvent

Therefore our handler implements

(20)

Event Type

private class ButtonHandler implements EventHandler<ActionEvent>

{

@Override

public void handle(ActionEvent e) {

if(e.getTarget() == button) {

System.out.println("Button clicked"); }

(21)

Handle Method

All handlers must override the handle method • Since we implement EventHandler we must

implement the handle method

The handle method is what gets called when

an event is detected

(22)

Handle Method

private class ButtonHandler implements EventHandler<ActionEvent> {

@Override

public void handle(ActionEvent e)

{

if(e.getTarget() == button) {

System.out.println("Button clicked"); }

(23)

Implementing handle

• We should always check the source of the

event to know what component generated the event

• Always check the source even if there is only one component registered with the handler

To ensure odd things don’t happen

(24)

Handle Method

private class ButtonHandler implements EventHandler<ActionEvent> {

@Override

public void handle(ActionEvent e) {

if(e.getTarget() == button)

{

System.out.println("Button clicked"); }

(25)

getTarget Method

All events have the getTarget method

• This is basically returning the component that fired the event

Use this to compare to the button you are

looking for

(26)

Overview

We have created a button

• We have created the event handler class

• We have NOT created an event handler object

(27)

EventHandler Object

private ButtonHandler buttonHandler;

@Override

public void start(Stage mainFrame) {

button = new Button("Ok");

scene = new Scene(button, 300, 300);

buttonHandler = new ButtonHandler(); button.setOnAction(buttonHandler);

(28)

setOnAction Method

All buttons have the method setOnAction • This method is in the ButtonBase class

Therefore all classes that inherit (directly or

indirectly) from this class have this method

(29)
(30)

Anonymous Inner Class Example

@Override

public void start(Stage mainFrame) {

button = new Button("Ok");

scene = new Scene(button, 300, 300);

button.setOnAction(new EventHandler<ActionEvent>(){ @Override

public void handle(ActionEvent e) { System.out.println("Button clicked"); } }); mainFrame.setScene(scene); mainFrame.show(); }

(31)

Example – ButtonHandlerExample2.java

(32)

Lambda Function Example

@Override

public void start(Stage mainFrame) {

button = new Button("Ok");

scene = new Scene(button, 300, 300);

button.setOnAction((ActionEvent e) -> { System.out.println("Button clicked"); });

mainFrame.setScene(scene); mainFrame.show();

(33)

Example – ButtonHandlerExample3.java

(34)

Separate Event Handling Class

Take your private inner class

• Make it into a separate regular class

• Everything else should work similarly

• Note: if your event handler needs things in

your regular class, you need to create the event handler with an instance of your class

Also need to proved get methods for the required

(35)

Quick Summary

Covered how to set up all event types of event

handlers for a button

• From here on we will just use private inner classes

• We looked at how to handle a button click

(36)

Multiple Buttons

Can register multiple buttons to a single

handler

Better than making a new handler for every

(37)

Example – MultipleButtonsExample.java

(38)
(39)

Multiple Buttons Code

private class ButtonHandler implements EventHandler<ActionEvent>

{

@Override

public void handle(ActionEvent e) {

if(e.getTarget() == button1)

{

System.out.println("Button 1 clicked"); }

else if(e.getTarget() == button2)

{

System.out.println("Button 2 clicked"); }

else if(e.getTarget() == button3)

(40)

Multiple Buttons

Use the getTarget method to determine which

one of the buttons has been clicked

if(e.getTarget() == button1)

• This allows for simpler code when multiple buttons can be registered to a single handler

(41)

Checkbox

Checkboxes also use the setOnAction method

to register a handler

• The handler is the same ActionEvent handler as regular buttons

• What makes Checkboxes different is the call e.getTarget()

(42)

Example – CheckBoxHandlerExample.java

(43)

CheckBox Code

@Override

public void start(Stage mainFrame) {

checkBox1 = new CheckBox("Check Box 1"); checkBox2 = new CheckBox("Check Box 2"); checkBox3 = new CheckBox("Check Box 3");

checkBoxHandler = new CheckBoxHandler();

checkBox1.setOnAction(checkBoxHandler); checkBox2.setOnAction(checkBoxHandler);

(44)

CheckBox Code

private class CheckBoxHandler implements EventHandler<ActionEvent> {

@Override

public void handle(ActionEvent e) {

if(e.getTarget() == checkBox1) {

System.out.println("Checkbox 1 clicked"); }

else if(e.getTarget() == checkBox2) {

System.out.println("checkBox 2 clicked"); }

else if(e.getTarget() == checkBox3) {

System.out.println("checkBox 3 clicked"); }

(45)
(46)

CheckBox Selected or Deselected

Usually want to do something based on

whether or not the checkbox was just selected or deselected

(47)

CheckBox Selected Code

@Override

public void handle(ActionEvent e) {

if(e.getTarget() == checkBox1) {

if(checkBox1.isSelected()) {

System.out.println("Checkbox 1 selected"); }

else {

(48)
(49)

Radio Buttons

These work the same way CheckBoxes do

• Probably want to create a ToggleGroup object to add all the radio buttons to

This has no impact on the handlers

(50)

Example – RadioButtonHandlerExample.java

(51)

Radio Button Code

radioButton1 = new RadioButton("Radio Button 1"); radioButton2 = new RadioButton("Radio Button 2"); radioButton3 = new RadioButton("Radio Button 3");

radioButtonHandler = new radioButtonHandler();

(52)

Radio Button Code

private class RadioButtonHandler implements EventHandler<ActionEvent> {

@Override

public void handle(ActionEvent e) {

if(e.getTarget() == radioButton1) {

System.out.println("Radio Button 1 selected"); }

else if(e.getTarget() == radioButton2) {

System.out.println("Radio Button 2 selected"); }

else if(e.getTarget() == radioButton3) {

System.out.println("Radio Button 3 selected"); }

(53)
(54)

ComboBox

ComboBoxes also work the same was as all

other buttons

• ComboBoxes also can use a ChangeListener

(55)

Example – ComboBoxHandlerExample.java

(56)

ComboBox Code

comboBox1 = new ComboBox();

comboBox1.getItems().addAll("Choice 1", "Choice 2", "Choice 3");

comboBox1.setMaxWidth(Double.MAX_VALUE); comboBox1.setVisibleRowCount(10);

(57)

ComboBox Code

@Override

public void handle(ActionEvent e) {

if(e.getTarget() == comboBox1) {

System.out.println("ComboBox 1 choice " +

(comboBox1.getSelectionModel().getSelectedIndex() + 1) + " selected");

(58)

ComboBox Methods

comboBox1.getSelectionModel()

This method returns the selection model

getSelectedIndex()

Returns the index of the ComboBox choice selected

Can also get the selected item

(59)
(60)

ChangeListener

• ChangeEvents for detecting changes

• Different from ActionEvents

• Need another handler to listen for these

Need to register the ComboBox to this listener

too

• Used for when we just want to detect the state has changed

(61)

ComboBox ChangeListener Code

comboBoxHandler2 = new ComboBoxHandler2();

comboBox1.valueProperty().addListener(comboBoxHan dler2);

This adds a Listener to the comboBox

This is generic for all listeners for the ComboBox

(62)

ComboBox ChangeListener Code

private class ComboBoxHandler2 implements ChangeListener<String> {

@Override

public void changed(ObservableValue<? extends String> o, String from, String to) {

System.out.println("Changed from " + from + " to " + to); }

(63)
(64)

ComboBox Events

• Notice how choosing a choice fired both an action event and a change event

(65)

TextField

TextFields have two main types of events

(66)

Example – TextHandlerExample.java

(67)

TextField ActionEvent Code

textHandler2 = new TextHandler2(); • textField1.setOnAction(textHandler2);

• This adds textHandler2 as an ActionEvent listener

(68)

TextField ActionEvent Code

private class TextHandler2 implements EventHandler<ActionEvent> {

@Override

public void handle(ActionEvent e) {

if(e.getTarget() == textField1) {

System.out.println(textField1.getText()); }

(69)
(70)

KeyEvent

Can listen events for keys

KeyPressed = when a key is pushed down

setOnKeyPressed()

KeyReleased = when a key is let back up

setOnKeyReleased()

KeyTyped = when a key is pressed and released

(71)

KeyEvent Code

textHandler = new TextHandler();
(72)

KeyEvent Code

private class TextHandler implements EventHandler<KeyEvent>

{

@Override

public void handle(KeyEvent e) {

if(e.getTarget() == textField1) {

if(e.getEventType() == KeyEvent.KEY_PRESSED) {

System.out.println(e.getText() + " Pressed"); }

else if(e.getEventType() == KeyEvent.KEY_RELEASED) {

System.out.println(e.getText() + " Released"); }

else if(e.getEventType() == KeyEvent.KEY_TYPED) { System.out.println(e.getText() + " Typed");

(73)

getEventType()

In this example the same handler is set to

listen to three events

KeyEvent.KEY_PRESSEDKeyEvent.KEY_RELEASEDKeyEvent.KEY_TYPED

(74)
(75)

KeyEvent

Can also detect certain keys • KeyEvent class methods

getCode = returns the key code

getText = returns the text of the key

(76)

MouseEvent

• Components can listen for various MouseEvents

• MouseEvent

– MOUSE_CLICKED

• when the mouse is pressed and released

– MOUSE_PRESSED

• when the mouse is pressed

– MOUSE_RELEASED

(77)

MouseEvent

MouseEvent

– MOUSE_MOVED

when the mouse moves inside a component

MOUSE_ENTERED

when the mouse enters a component

MOUSE_EXITED

• when the mouse exits a component

MOUSE_DRAGGED

(78)

MouseEvent

Can have any node listen for mouse events

Labels

TextFieldsShapes

– Buttons

(79)

Functions – Node class

setOnMouseClicked • setOnMouseDragged

• setOnMouseEntered

• setOnMouseExited

• setOnMouseMoved

• setOnMousePressed

(80)

Example – MouseHandlerExample.java

(81)

Adding Handlers Code

label1 = new label("Click Here");

mouseHandler = new MouseHandler();

(82)

MouseEvent Handler

private class MouseHandler implements EventHandler<MouseEvent>

{

@Override

public void handle(MouseEvent e) {

if(e.getTarget() == label1) {

(83)

MouseEvent Functions

getButton()

Determine which mouse button was clicked

getScreenX()

X position the mouse was clicked at

getScreenY()

(84)

MouseButton

Class has constants to compare the mouse button to

PRIMARY = primary mouse button (usually left)

– SECONDARY = secondary mouse button (usually right) – MIDDLE = middle mouse button

(85)

MouseHandler Code

if(e.getButton() == MouseButton.PRIMARY)

{

if(e.getEventType() == MouseEvent.MOUSE_PRESSED) {

System.out.println("Left Mouse Pressed"); }

else if(e.getEventType() == MouseEvent.MOUSE_RELEASED) {

System.out.println("Left Mouse Pressed"); }

(86)
(87)

Logic Note

It might be possible to have a released event

without a pressed event

Click in one node

Do not released the mouseMove into another nodeRelease the mouse

Try this and see what happens.

(88)

Restructure Code For more Events

Check for the source

if(e.getTarget() == label1)

Check for the event type

– if(e.getEventType() == MouseEvent.MOUSE_PRESSED)

• Check for the button type (if applicable)

(89)

More Mouse Handling Code

else if(e.getEventType() == MouseEvent.MOUSE_CLICKED) {

if(e.getButton() == MouseButton.PRIMARY) { System.out.println("Left Mouse Clicked"); }

else if(e.getButton() == MouseButton.SECONDARY) {

(90)
(91)
(92)

Event Process

Target Selection

• Route Construction

• Event Capturing

(93)

Target Selection

Determine which node fired the event

(94)

Target Selection Example

Clicking on the triangle will choose the

(95)

Route Construction

Initial route determined by event dispatch

chain

• Initial route can be modified by

Event filters

Event Handlers

(96)

Route Construction Example

(97)

Event Capturing

Event flows along the event dispatch chain • If there are any nodes that have filters, those

filters are called

Filters are the same as handlers

They capture things on the way down the chain

Can consume the event and stop the event flow

Event reaches the bottom of the chain and the

(98)

Event Bubbling

Once the event is processed it returns back up

the chain of events

The event is processed by any registered

(99)

Example

Register filter for rectangle • Register handler for triangle

(100)

Example

When I click on the triangle the following

happens

Event filter for rectangle is executed

(101)

Example 2

Register filter for rectangle

Consume the event in the filter

(102)

Example

When I click on the triangle the following

happens

Event filter for rectangle is executed • Event is consumed

(103)

Event Chain Reference

http://

(104)

When to Consume Events

Scenario

You have one component that contains another

Let component x contain component y

Both components listen for button clicksYou click on component y

You do not want the event handler for component

x to execute

You should consume the event in the event handler

http://docs.oracle.com/javafx/2/events/processing.htm

References

Related documents

In order to verify or create a class list in the US-EDI teacher portal, you will need to receive a class list from your district that contains, for each child in your class,

If the program fails to satisfactorily demonstrate incremental progress, the full Board may revoke the program's approval; and, continued to require the program to admit no

One feature emerging from Reddy and Hill’s (2002) research on placement assessment was that the sandwich year was clearly valued by students to the extent that a ‘placement

The elastic body of the sensor mechanical structure comprises of central support beam, cross elastic beams, compliant beams and base of the body.. Here the

- 5 years corporate income tax exemption, capped at the amount of actual invest- ment (excluding cost of land and work- ing capital) unless specified in the list of activities

Operation: Status will be true if any of the following cases are true: • Monitor alarm inhibit is active (see Monitor Alarm Inhibit) • Any enabled channel is in bypass (see

The 2010 IRTS Peer Review was not meant to be an inspection or an audit, nor was there a pass/fail component to the evaluation. Similarly, it was not associated with a

In particular, we aim at finding out how many indexes HAIL can create for a given dataset in the same time standard Hadoop needs to upload the same dataset with the default