java event handling csis 3701: advanced object oriented programming

15
Java Event Handling CSIS 3701: Advanced Object Oriented Programming

Upload: alexandrina-sanders

Post on 16-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Event Handling CSIS 3701: Advanced Object Oriented Programming

Java Event Handling

CSIS 3701: Advanced Object Oriented Programming

Page 2: Java Event Handling CSIS 3701: Advanced Object Oriented Programming

Event-driven Programming

• Code executed at startup to initialize– Code implemented in constructor

• Additional code executed on demand when user causes events (button press, etc.)– Usually done in actionPerformed method of the

application

public void actionPerformed(ActionEvent e) { responseField.setText("Welcome to CSIS 3701!");

}

Page 3: Java Event Handling CSIS 3701: Advanced Object Oriented Programming

Event-driven Programming

• Application registers as an “ActionListener”– Can handle “action events” created by components

public class Main extends JFrame implements ActionListener {

• Application tells button to notify it when pressed– Adds itself as an ActionListener to the button

helloButton.addActionListener(this);

Reference to the application itself

Page 4: Java Event Handling CSIS 3701: Advanced Object Oriented Programming

Event-driven Programming

Application

Construct button

Have it notify me about events by passing my own address

User presses

helloButton.addActionListener(this);

Button notifies application

actionPerformed(ActionEvent e)

location 37A4

37A4

Page 5: Java Event Handling CSIS 3701: Advanced Object Oriented Programming

Clock Object as Member Variable

ClockProject package

Main.javavisual application

• Stores current hour and minute• Allows hour and minute to be set• Increments to next second or minute

Clock.javabusiness logic

clock

Page 6: Java Event Handling CSIS 3701: Advanced Object Oriented Programming

Event Objects

• Java events are objects with properties/methods– Event object passed to application as parameter

public void actionPerformed(ActionEvent e)

• Contain useful information about event– Example: which component caused event

public Object getSource()

– Can use to execute different code for different events

if (e.getSource() == helloButton) {…

Page 7: Java Event Handling CSIS 3701: Advanced Object Oriented Programming

Event-driven Programming

Application

User presses

Button notifies application

actionPerformed(ActionEvent e)

location 7981

ActionEvent object

Source: 7981

helloButton: 7981

Page 8: Java Event Handling CSIS 3701: Advanced Object Oriented Programming

Event Objects• Example: application to move text either left or right

based on which button pressed

private JButton leftButton; private JButton rightButton;

public Main() {

leftButton = new JButton("MOVE LEFT");

rightButton = new JButton("MOVE RIGHT");

leftButton.addActionListener(this);

rightButton.addActionListener(this);actionPerformed called if either pressed

Page 9: Java Event Handling CSIS 3701: Advanced Object Oriented Programming

Event Objects

• Use getSource to find whether to move text left or right

public void actionPerformed(ActionEvent e) {

String temp;

if (e.getSource() == leftButton) {temp = rightField.getText();

leftField.setText(temp); rightField.setText(""); }

if (e.getSource() == rightButton) { temp = leftField.getText(); rightField.setText(temp); leftField.setText(""); }

}

Page 10: Java Event Handling CSIS 3701: Advanced Object Oriented Programming

Simple Event Handling

• Awkward if many event components– Would need separate if for each– Difficult to write, maintain

12 if statements

Page 11: Java Event Handling CSIS 3701: Advanced Object Oriented Programming

Arrays of Components

• Can store groups of similar components as array

private compType[] comps = new compType[size];

• Use loop to construct, add listeners, add to panel

for (int i = 0; i < size; i++) { comps[i] = new compType[parameters]; comps[i].addActionListener(this); panel.add(comps[i]); }

Page 12: Java Event Handling CSIS 3701: Advanced Object Oriented Programming

Keypad Example

public class Keypad1 extends JFrame implements ActionListener {

private JTextField keyField;

private static final int KEYS = 12;

private JButton[] keys = new JButton[KEYS];

private String[] labels = new String[] {"1","2","3","4","5","6","7","8","9","*","0","#"};

Will use this array to add different labels to each button

Page 13: Java Event Handling CSIS 3701: Advanced Object Oriented Programming

Keypad Example

public Keypad1() {

JPanel buttonPanel = new JPanel(new GridLayout(4, 3));

for (int i = 0; i < KEYS; i++) {

keys[i] = new JButton(labels[i]);

keys[i].addActionListener(this);

buttonPanel.add(keys[i]);

}

Construct button from ith label in array

Listen for its events

Add it to the panel

Page 14: Java Event Handling CSIS 3701: Advanced Object Oriented Programming

Loops for Event Handling

• Can iterate through array to handle events– Loop through array in actionPerfrormed– Compare each component to source of event

public void actionPerformed(ActionEvent e) {for (int i = 0; i < size; i++) if (e.getSource() == comps[i]) { … code to handle event on that object … }

Page 15: Java Event Handling CSIS 3701: Advanced Object Oriented Programming

Loops for Event Handling

public void actionPerformed(ActionEvent e) {

for (int i = 0; i < KEYS; i++) {

if (e.getSource() == keys[i]) {

keyField.setText(keyField.getText()+labels[i]);

}

}

}Since we know the ith button caused the event, add the corresponding ith label to the output