Presentation is loading. Please wait.

Presentation is loading. Please wait.

Modular Event Handling

Similar presentations


Presentation on theme: "Modular Event Handling"— Presentation transcript:

1 Modular Event Handling
CSIS 3701: Advanced Object Oriented Programming

2 Simple Event Handling public class Main extends JFrame implements ActionListener { private JButton b; public Main() { b = new JButton(“press”); b.addActionListener(this); … } public void actionPerformed( ActionEvent e) { … } } JButton object -- stores reference to Main object in list of type ActionListener -- calls actionPerformed method of that Main object when pressed

3 Simple Event Handling Can use getSource to find which component caused event 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(""); } }

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

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

6 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

7 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

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

9 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

10 Event Handler Classes Can create separate classes to handle events
Constructed by application Passed to buttons using addActionListener Its actionPerformed called by buttons when pressed Application JButton button Listeners Passes reference to handler Handler actionPerformed constructs

11 Event Handler Objects Must implement ActionListener interface
Application no longer has to implement ActionListener Has actionPerformed method to handle event Handles event independently from application Often passed information to constructor from application Example: KeypadHandler One for each button on application Passed string corresponding to number on its button by constructor Displays number when it button pressed

12 KeypadHandler Class public class Handler2 implements ActionListener {
private String key; public Handler2(String k) { key = k; } public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, key); String corresponding to label on its button Set in constructor Displayed when that button calls its actionPerformed method

13 Using KeypadHandler public Keypad() {
for (int i = 0; i < KEYS; i++) { keys[i] = new JButton(labels[i]); keys[i].addActionListener( new KeypadHandler(labels[i])); } For each button with a given label Construct a KeypadHandler with that label Pass it to the button KeypadHandler key KeypadHandler key 3 4

14 Accessing Application from Handler
What if handler must access visual components on application? Example: KeypadHandler should append to textfield instead of popping up dialog Application can also pass reference to visual component the handler must manipulate KeypadHandler key field 4

15 KeypadHandler Class public class Handler3 implements ActionListener {
private String key; private JTextField field; public Handler3(String k, JTextField f) { key = k; field = f; } public void actionPerformed(ActionEvent e) { field.setText(field.getText()+key); Reference to component to append to Set in constructor Manipulate that component when its actionPerformed method is called by its button

16 Event Handlers as Inner Classes
Event handler classes often implemented as inner classes of application Automatically have access to all components of the application Less modular/reusable, but easier to implement Application Visual component Inner class reference to visual component

17 KeypadHandler Inner Class
public class Keypad4 extends JFrame { private class Handler4 implements ActionListener { private String key; private Handler4(String k) {key = k;} public void actionPerformed(ActionEvent e) { keyField.setText(keyField.getText()+key); } } // end inner class … private JTextField keyField = new JTextField(15); direct access


Download ppt "Modular Event Handling"

Similar presentations


Ads by Google