Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,

Similar presentations


Presentation on theme: "1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,"— Presentation transcript:

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, mouse 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  The buttons and text fields don’t execute code — Instead JButton and JTextField objects send actionPerformed messages to other objects that have been registered to “listen”  We write the code we want to execute in actionPerformed methods — This requires a class that implements an interface, for example — Registering an instance of that class to listen

5 5 But no one is "Listening"  Wanted: An object to listen to the button that understands a message such as — actionPerformed  Also need to tell the button who it can send the actionPerformed message to  Register the listener with this method addActionListener(ActionListener al)

6 6 Handling Events Add a private inner class that can listen to the event that the graphical component will generate Your class must implement a listener interface to guarantee that it has the expected methods: First up: ActionListener Register the listener object to the GUI component (JButton JTextField) so it can later send actionPerformed messages to that listener when the use clicks the button or presses enter. events occur anytime in the future--the listener is listening (waiting for user generated events such as clicking a button or entering text into a text field)

7 7 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) — You need an ActionListener object There is no ActionListener class! What can we do?????

8 8 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. The Button does not care what happened

9 9 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

10 10 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 // 5. 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

11 11 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

12 12 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

13 13 Listen to JTextField  Add to the current GUI — Have the button click toggle the JTextField form upper to lower case Need methods getText and setText

14 14 JList and DefaultListModel  Code demo: — Show a JList GUI component with a DefaultListModel set as the model will contain a list of strings  Respond to selections by showing the selected string in a dialog box

15 15 public class ShowJListListModel extends JFrame { public static void main(String[] args) { JFrame window = new ShowJListListModel(); window.setVisible(true); } private JList guiList; private DefaultListModel allStrings; public ShowJListListModel() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(250, 300); setLocation(100, 100); setLayout(null); initializeAllStrings(); guiList = new JList(allStrings); guiList.setSelectedIndex(allStrings.getSize() - 1); guiList.setSize(150, 250); guiList.setLocation(20, 10); guiList.setFont(new Font("Arial", Font.BOLD + Font.ITALIC, 14)); add(guiList); registerListeners(); } private void registerListeners() { ListSelectionListener listener = new GuiListListListener(); guiList.addListSelectionListener(listener); } private class GuiListListListener implements ListSelectionListener { public void valueChanged(ListSelectionEvent arg0) { String str = allStrings.get(guiList.getSelectedIndex()).toString(); System.out.println(str + " selected"); } private void initializeAllStrings() { allStrings = new DefaultListModel(); allStrings.addElement("Riley"); allStrings.addElement("Dakota"); allStrings.addElement("Casey"); allStrings.addElement("Angel"); allStrings.addElement("Reese"); }


Download ppt "1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,"

Similar presentations


Ads by Google