Modular Event Handling

Slides:



Advertisements
Similar presentations
1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
Advertisements

Basic Java – Interface design. Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven.
Drawing in a frame – Java GUI
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphic User Interfaces Layout Managers Event Handling.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Deitel Ch 11-part 1 Java GUIs 1 Java GUIs (Deitel, Chap 14-part1) Focus: Attaching GUI components + event handling  input dialog => only one value for.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Event Handling Events and Listeners Timers and Animation.
Layout Mangers CSC 171 FALL 2001 LECTURE 14. History: The Transistor William Shockley, John Bardeen, and Walter Brattain invent the transfer resistance.
Io package as Java’s basic I/O system continue’d.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Combo Box, Check Boxes, and Radio Buttons. Radio Buttons User can click radio buttons, just like other buttons BUT Radio buttons are mutually exclusive.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
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,
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
Layout Managers Arranges and lays out the GUI components on a container.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
For (int i = 1; i
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CMSC 341 Making Java GUIs Functional. 09/29/2007 CMSC 341 Events 2 More on Swing Great Swing demo at /demos/jfc/SwingSet2/SwingSet2Plugin.html.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
A simple swing example GETTING STARTED WITH WIND CHILL.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
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,
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
Event Handling CS 21a: Introduction to Computing I First Semester,
Frame Windows Application program, not applet Construct and show frame JFrame frame = new JFrame(); *** frame.show(); *** Set default close operation..
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
회원가입 - GUI ** 오지*.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A Quick Java Swing Tutorial
Lecture 15.1 Event Delegation.
GUIs and Events Rick Mercer.
CompSci 230 S Programming Techniques
Web Design & Development Lecture 11
A First Look at GUI Applications
CHAPTER 7 & 8 REVIEW QUESTIONS
Graphical User Interface (pronounced "gooey")
Chapter 4 Interface Types and Polymorphism Part 2
Ellen Walker Hiram College
Event Handling CS 21a: Introduction to Computing I
MVC Paradigm The MVC paradigm breaks applications or interfaces into three parts: the model, the view, and the controller. A --> 25 % B --> 60 % C -->
Event-driven programming for GUI
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
Introduction to Event Handling
A Quick Java Swing Tutorial
Constructors, GUI’s(Using Swing) and ActionListner
Making Java GUIs Functional
CiS 260: App Dev I Chapter 6: GUI and OOD.
Presentation transcript:

Modular Event Handling CSIS 3701: Advanced Object Oriented Programming

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

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

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]); keys[i].addActionListener(this); buttonPanel.add(keys[i]); } Construct button from ith label in array Listen for its events 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

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

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

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

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

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

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

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

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