Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Event Handling CSIS 3701: Advanced Object Oriented Programming.

Similar presentations


Presentation on theme: "Java Event Handling CSIS 3701: Advanced Object Oriented Programming."— Presentation transcript:

1 Java Event Handling CSIS 3701: Advanced Object Oriented Programming

2 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!"); }

3 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

4 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

5 Clock Object as Member Variable ClockProject package Main.java visual application Stores current hour and minute Allows hour and minute to be set Increments to next second or minute Clock.java Clock.java business logic clock

6 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) {…

7 Event-driven Programming Application User presses Button notifies application actionPerformed(ActionEvent e) location 7981 ActionEvent object Source : 7981 helloButton : 7981

8 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

9 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(""); } }

10 Simple Event Handling Awkward if many event components –Would need separate if for each –Difficult to write, maintain 12 if statements

11 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]); }

12 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

13 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 i th label in array Listen for its events Add it to the panel

14 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 … }

15 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 i th button caused the event, add the corresponding i th label to the output


Download ppt "Java Event Handling CSIS 3701: Advanced Object Oriented Programming."

Similar presentations


Ads by Google