Download presentation
Presentation is loading. Please wait.
Published byNathaniel Wilcox Modified over 8 years ago
1
1 Event Driven Programs Rick Mercer
2
2 So what happens next? You can layout a real pretty GUI You can click on buttons, enter text into a text field, move the mouse, press a key — And NOTHING happens So let’s make something happen…
3
3 Java's Event Model Java lets the operating system notify graphical components of user interaction — JButton objects know when a user clicks it — JTextField objects with focus know when the user presses the enter (return) key — Event driven programs respond to many things mouse clicks and movements clicks on hyperlinks or menu items Users pressing any key, selecting a list item, moving a slider bar, checking a radio button, …
4
4 Example: Action Events Buttons and text fields don’t execute code — Instead JButton and JTextField objects send actionPerformed messages to their list of “listeners” We write the code we want to execute in actionPerformed methods — Write a class that implements an interface — Register an instance of this class to listen
5
5 ActionEvent / ActionListener When a JButton object is clicked, it constructs an ActionEvent object and sends it to the actionPerformed method of its listeners — We can usually ignore that parameter, other times we can't To register a listener to a JButton, send an addActionListener message to button public void addActionListener(ActionListener al) — We need an ActionListener object — There is no ActionListener class! — What can we do?????
6
6 Implement an interface Then your object can be treated as if it were an ActionListener Polymorphism in action: We can have any number of actionPerformed methods that do whatever they are supposed to.
7
7 Inner class Add an inner class — inner classes have access to the enclosing classes' instance variables Make it private since no one else needs to know about it Java added inner classes for the very purpose: to have listener objects respond to user interactions with GUI components
8
8 And register the listener with addActionListener // 6. Register the instance of the listener so the // component can later send messages to that object ButtonListener aListener = new ButtonListener(); clickMeButton.addActionListener(aListener); } // End constructor // inner class to listen to events private class ButtonListener implements ActionListener { // No constructor needed here. // Must have this method to implement ActionListener public void actionPerformed(ActionEvent anActionEvent) { System.out.println("Button was clicked."); } Caution: this is easy to forget. It is an error no one will tell you about
9
9 Another benefit of interfaces Can have many ActionListener objects — Any class that implements ActionListener — may need a different class for every button and text field in the GUI But they all can be treated as ActionListener objects They can be passed as arguments to this method public void addActionListener(ActionListener aL) Adds an action listener to receive action events from JButtons, TextFields,... aL - an instance of a class that implements the ActionListener interface
10
10 Assignment Compatible Can pass instances of classes implementing an interface to the interface type parameter addActionListener(ActionListener anyListener) addActionListener(new ButtonListener()); addActionListener(new TextFieldListener()); ButtonListener and TextFieldListener must implement interface ActionListener
11
11 Listen to a JTextField Add to the current GUI — Have the button click toggle the JTextField form upper to lower case — Need methods getText and setText See ShowListener.java
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.