Java Event Handling

Download Report

Transcript Java Event Handling

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!");
}
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
Event-driven Programming
location 37A4
Construct button
Application
Have it notify me about events
by passing my own address
helloButton.addActionListener(this);
37A4
Button notifies application
actionPerformed(ActionEvent e)
User
presses
Clock Object as Member Variable
ClockProject package
• Stores current hour and
clock
minute
• Allows hour and minute
to be set
• Increments to next
second or minute
Main.java
Clock.java
visual application
business logic
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) {…
Event-driven Programming
location 7981
helloButton: 7981
Application
Button notifies application
actionPerformed(ActionEvent e)
ActionEvent object
Source: 7981
User
presses
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);
actionPerformed
rightButton.addActionListener(this);
called if either pressed
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("");
}
}
Simple Event Handling
• Awkward if many event components
– Would need separate if for each
– Difficult to write, maintain
12 if statements
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]);
}
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
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]);
Construct button
from ith label in array
keys[i].addActionListener(this);
Listen for its events
buttonPanel.add(keys[i]);
}
Add it to the panel
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 …
}
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