Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

Slides:



Advertisements
Similar presentations
Event Handling.
Advertisements

Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 12 Event-Driven Programming 找出画中真谛 — 保罗. 塞尚.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 15 Event-Driven Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
For IST410 Students only Events-1 Event Handling.
Unit 111 Event-Driven Programming Listener or Event handler Example: Handling Button Events Example: Handling Mouse Events Example: Handling Keyboard Events.
Event-Driven Programming
Unit 12 Object-oriented programming: Event-driven programming for GUI Jin Sa.
Event Handling n Events: an event is an object that describes a state change in a source. n Event Sources: A source is an object that generates an event.
Intermediate Java1 An example that uses inner classes Week Four Continued.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 16 Event-Driven Programming.
GUI Event Handling Nithya Raman. What is an Event? GUI components communicate with the rest of the applications through events. The source of an event.
1 Event-Driven Programming Just like designing GUIs, you also will probably not write Java code like the program examples given in these notes. You will.
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,
1 CSE 331 More Events (Mouse, Keyboard, Window, Focus, Change, Document...) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Chapter 12 Event Handling. Chapter Goals To understand the Java event model To install action and mouse event listeners To accept input from buttons,
Tables CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 Event-Driven Programming.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
Introduction to GUI Programming with Java Graphical User Interfaces With AWT and Swing Towson University *Ref:
Event Handling. Event Driven Programming Flow of programs is determined by events. The Operating system recognizes events and passes them to the particular.
Layout Managers CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
For (int i = 1; i
Event Handling. 2 GUIs are event driven –Generate events when user interacts with GUI e.g., moving mouse, pressing button, typing in text field, etc.
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.
Lesson 6 Programming Techniques Event Handling /EvH/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Object Oriented Programming.  Interface  Event Handling.
Event Handling. The signals that a program receives from the operating system as a result of the actions are called events. A window based program is.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Option Panes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
CHAPTER 10 EVENT HANDLING. CHAPTER GOALS To understand the Java event model To install mouse and action listeners To accept mouse and text input To display.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
Events in JAVA Needed for Applets and GUI based programs. Applet uses GUI to interact with user. Most of the events are generated by Mouse, Keyboard.
GUI Components CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0.
Event Listeners ActionListener –button,list AdjustmentListener-scroll bar ComponentListener-when component hidden…. ContainerListener-comp added or removed.
1 Chapter 3 Event-Driven Programming. 2 Objectives F To explain the concept of event-driven programming (§12.2). F To understand event, event source,
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Menus and Toolbars CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
Events and Event Handling
Chapter 14 Event-Driven Programming
Object-Orientated Analysis, Design and Programming
CHAPTER Reacting to the user.
Chapter 12 Event-Driven Programming
Event Handling Chapter 2 Objectives
GUI Event Handling Nithya Raman.
GUI Programming III: Events
Event-driven programming for GUI
Web Design & Development Lecture 12
Chapter 16 Event-Driven Programming
Programming Graphical User Interface (GUI)
Presentation transcript:

Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Outline USC CSCI 201L2/23 ▪E▪Event-Driven Programming Overview ▪E▪Event-Driven Programming Example ▪A▪Adapter Classes ▪P▪Program

Event-Driven Programming Overview ▪The code we have written so far executed deterministically from top to bottom, calling methods and utilizing other classes ›We utilized user input to determine code that was executed in some cases, but we had prompted the user and waited for a response ▪In GUI programming, the code that gets called is based on events, such as a user clicking or a user typing something ›We don’t know the order in which users will perform the actions though ›We need to associate an action with a method USC CSCI 201L3/23 Event-Driven Programming Overview

Fired Events Source ObjectUser ActionEvent Type Fired JButtonClick the buttonActionEvent JTextFieldPress enter keyActionEvent JComboBoxSelect an itemItemEvent, ActionEvent JListSelect one or more itemsListSelectionEvent JCheckBoxClick a check boxItemEvent, ActionEvent JRadioButtonClick a radio buttonItemEvent, ActionEvent JMenuItemSelect a menu itemActionEvent JScrollBarMove the scroll barAdjustmentEvent JSliderMove the slider barChangeEvent WindowOpen, closing, closed, iconify, deiconify, activate, deactive the window WindowEvent ContainerComponent added or removed from the containerContainerEvent Component Mouse pressed, released, clicked, entered, or exitedMouseEvent Mouse moved or draggedMouseEvent Key pressed or releasedKeyEvent Component moved, resized, hidden, or shownComponentEvent Component gained or lost focusFocusEvent USC CSCI 201L4/23 Event-Driven Programming Overview Note: If a component can fire an event, any subclass of the component can fire the same type of event  Every Component can fire a MouseEvent, KeyEvent, FocusEvent, and ComponentEvent since Component is the superclass of GUI components

Listeners ▪Java uses a delegation-based model for event handling ›A source object fires an event ›An object interested in the event, called a listener, handles it A listener must be an instance of a listener interface and must be registered with a source component ›From the last lecture, we saw 25 JButton button2 = new JButton("2"); 26 button2.addActionListener(new ButtonClicked("first", outerPanel)); ›And then 35 class ButtonClicked implements ActionListener { 36 private String numberString; 37 private JPanel jp; 38 public ButtonClicked(String numberString, JPanel jp) { 39 this.numberString = numberString; 40 this.jp = jp; 41 } 42 public void actionPerformed(ActionEvent ae) { 43 CardLayout cl = (CardLayout)jp.getLayout(); 44 cl.show(jp, numberString); 45 } 46 } USC CSCI 201L5/23 Event-Driven Programming Overview

Fired Events Event ClassListener InterfaceListener Methods ActionEventActionListeneractionPerformed(ActionEvent) ItemEventItemListeneritemStateChanged(ItemEvent) MouseEvent MouseListener mousePressed(MouseEvent) mouseReleased(MouseEvent) mouseEntered(MouseEvent) mouseExited(MouseEvent) mouseClicked(MouseEvent) MouseMotionListener mouseDragged(MouseEvent) mouseMoved(MouseEvent) KeyEventKeyListener keyPressed(KeyEvent) keyReleased(KeyEvent) keyTyped(KeyEvent) WindowEventWindowListener windowClosing(WindowEvent) windowOpened(WindowEvent) windowIconified(WindowEvent) windowDeiconified(WindowEvent) windowClosed(WindowEvent) windowActivated(WindowEvent) windowDeactivated(WindowEvent) ContainerEventContainerListener componentAdded(ContainerEvent) componentRemoved(ContainerEvent) ComponentEventComponentListener componentMoved(ComponentEvent) componentHidden(ComponentEvent) componentResized(ComponentEvent) componentShown(ComponentEvent) FocusEventFocusListener focusGained(FocusEvent) focusLost(FocusEvent) AdjustmentEventAdjustmentListeneradjustmentValueChanged(AdjustmentEvent) ChangeEventChangeListenerstateChanged(ChangeEvent) ListSelectionEventListSelectionListenervalueChanged(ListSelectionEvent) USC CSCI 201L6/23 Event-Driven Programming Overview

Outline USC CSCI 201L7/23 ▪E▪Event-Driven Programming Overview ▪E▪Event-Driven Programming Example ▪A▪Adapter Classes ▪P▪Program

GUI with Events USC CSCI 201L8/23 Event-Driven Programming Example Create the following GUI

Event-Driven Programming Example 1 import java.awt.event.ActionEvent; 2 import java.awt.event.ActionListener; 3 4 import javax.swing.BoxLayout; 5 import javax.swing.JButton; 6 import javax.swing.JFrame; 7 import javax.swing.JLabel; 8 import javax.swing.JPanel; 9 import javax.swing.JTextField; public class Test extends JFrame { 12 private JTextField num1TF, num2TF, totalTF; 13 private JLabel number1Label, number2Label; 14 private JButton concatenateButton; 15 public Test() { 16 super("Event-Driven Programming Example"); 17 number1Label = new JLabel("Number 1"); 18 num1TF = new JTextField("", 15); 19 number2Label = new JLabel("Number 2"); 20 num2TF = new JTextField("", 15); 21 totalTF = new JTextField("", 20); 22 concatenateButton = new JButton("Concatenate"); 23 concatenateButton.addActionListener( 24 new ConcatenateAdapter(num1TF, num2TF, totalTF)); JPanel row1Panel = new JPanel(); 27 row1Panel.add(number1Label); 28 row1Panel.add(num1TF); JPanel row2Panel = new JPanel(); 31 row2Panel.add(number2Label); 32 row2Panel.add(num2TF); JPanel row3Panel = new JPanel(); 35 row3Panel.add(concatenateButton); JPanel row4Panel = new JPanel(); 38 row4Panel.add(totalTF); USC CSCI 201L9/23 Event-Driven Programming Example 39 JPanel verticalPanel = new JPanel(); 40 verticalPanel.setLayout(new BoxLayout(verticalPanel, BoxLayout.Y_AXIS)); 41 verticalPanel.add(row1Panel); 42 verticalPanel.add(row2Panel); 43 verticalPanel.add(row3Panel); 44 verticalPanel.add(row4Panel); add(verticalPanel); 47 setSize(250, 175); 48 setLocationRelativeTo(null); 49 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 50 setVisible(true); 51 } public static void main(String args[]) { 54 Test t = new Test(); 55 } 56 } class ConcatenateAdapter implements ActionListener { 59 private JTextField num1TF, num2TF, totalTF; 60 public ConcatenateAdapter(JTextField num1TF, JTextField num2TF, JTextField totalTF) { 61 this.num1TF = num1TF; 62 this.num2TF = num2TF; 63 this.totalTF = totalTF; 64 } public void actionPerformed(ActionEvent ae) { 67 totalTF.setText(num1TF.getText() + num2TF.getText()); 68 } 69 }

Event-Driven Programming Example 1 import java.awt.event.ActionEvent; 2 import java.awt.event.ActionListener; 3 4 import javax.swing.BoxLayout; 5 import javax.swing.JButton; 6 import javax.swing.JFrame; 7 import javax.swing.JLabel; 8 import javax.swing.JPanel; 9 import javax.swing.JTextField; public class Test extends JFrame { 12 private JTextField num1TF, num2TF, totalTF; 13 private JLabel number1Label, number2Label; 14 private JButton concatenateButton; 15 public Test() { 16 super("Event-Driven Programming Example"); 17 number1Label = new JLabel("Number 1"); 18 num1TF = new JTextField("", 15); 19 number2Label = new JLabel("Number 2"); 20 num2TF = new JTextField("", 15); 21 totalTF = new JTextField("", 20); 22 concatenateButton = new JButton("Concatenate"); 23 concatenateButton.addActionListener( 24 new ConcatenateAdapter(num1TF, num2TF, totalTF)); JPanel row1Panel = new JPanel(); 27 row1Panel.add(number1Label); 28 row1Panel.add(num1TF); JPanel row2Panel = new JPanel(); 31 row2Panel.add(number2Label); 32 row2Panel.add(num2TF); JPanel row3Panel = new JPanel(); 35 row3Panel.add(concatenateButton); JPanel row4Panel = new JPanel(); 38 row4Panel.add(totalTF); USC CSCI 201L10/23 Event-Driven Programming Example 39 JPanel verticalPanel = new JPanel(); 40 verticalPanel.setLayout(new BoxLayout(verticalPanel, BoxLayout.Y_AXIS)); 41 verticalPanel.add(row1Panel); 42 verticalPanel.add(row2Panel); 43 verticalPanel.add(row3Panel); 44 verticalPanel.add(row4Panel); add(verticalPanel); 47 setSize(250, 175); 48 setLocationRelativeTo(null); 49 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 50 setVisible(true); 51 } public static void main(String args[]) { 54 Test t = new Test(); 55 } class ConcatenateAdapter implements ActionListener { 58 private JTextField num1TF, num2TF, totalTF; 59 public ConcatenateAdapter(JTextField num1TF, JTextField num2TF, JTextField totalTF) { 60 this.num1TF = num1TF; 61 this.num2TF = num2TF; 62 this.totalTF = totalTF; 63 } public void actionPerformed(ActionEvent ae) { 66 totalTF.setText(num1TF.getText() + num2TF.getText()); 67 } 68 } 69 }

Event-Driven Programming Example 1 import java.awt.event.ActionEvent; 2 import java.awt.event.ActionListener; 3 4 import javax.swing.BoxLayout; 5 import javax.swing.JButton; 6 import javax.swing.JFrame; 7 import javax.swing.JLabel; 8 import javax.swing.JPanel; 9 import javax.swing.JTextField; public class Test extends JFrame { 12 private JTextField num1TF, num2TF, totalTF; 13 private JLabel number1Label, number2Label; 14 private JButton concatenateButton; 15 public Test() { 16 super("Event-Driven Programming Example"); 17 number1Label = new JLabel("Number 1"); 18 num1TF = new JTextField("", 15); 19 number2Label = new JLabel("Number 2"); 20 num2TF = new JTextField("", 15); 21 totalTF = new JTextField("", 20); 22 concatenateButton = new JButton("Concatenate"); 23 concatenateButton.addActionListener( 24 new ConcatenateAdapter()); JPanel row1Panel = new JPanel(); 27 row1Panel.add(number1Label); 28 row1Panel.add(num1TF); JPanel row2Panel = new JPanel(); 31 row2Panel.add(number2Label); 32 row2Panel.add(num2TF); JPanel row3Panel = new JPanel(); 35 row3Panel.add(concatenateButton); JPanel row4Panel = new JPanel(); 38 row4Panel.add(totalTF); USC CSCI 201L11/23 Event-Driven Programming Example 39 JPanel verticalPanel = new JPanel(); 40 verticalPanel.setLayout(new BoxLayout(verticalPanel, BoxLayout.Y_AXIS)); 41 verticalPanel.add(row1Panel); 42 verticalPanel.add(row2Panel); 43 verticalPanel.add(row3Panel); 44 verticalPanel.add(row4Panel); add(verticalPanel); 47 setSize(250, 175); 48 setLocationRelativeTo(null); 49 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 50 setVisible(true); 51 } public static void main(String args[]) { 54 Test t = new Test(); 55 } class ConcatenateAdapter implements ActionListener { 65 public void actionPerformed(ActionEvent ae) { 66 Test.this.totalTF.setText(Test.this.num1TF.getText() + Test.this.num2TF.getText()); 67 } 68 } 69 }

Event-Driven Programming Example 1 import java.awt.event.ActionEvent; 2 import java.awt.event.ActionListener; 3 4 import javax.swing.BoxLayout; 5 import javax.swing.JButton; 6 import javax.swing.JFrame; 7 import javax.swing.JLabel; 8 import javax.swing.JPanel; 9 import javax.swing.JTextField; public class Test extends JFrame { 12 private JTextField num1TF, num2TF, totalTF; 13 private JLabel number1Label, number2Label; 14 private JButton concatenateButton; 15 public Test() { 16 super("Event-Driven Programming Example"); 17 number1Label = new JLabel("Number 1"); 18 num1TF = new JTextField("", 15); 19 number2Label = new JLabel("Number 2"); 20 num2TF = new JTextField("", 15); 21 totalTF = new JTextField("", 20); 22 concatenateButton = new JButton("Concatenate"); 23 concatenateButton.addActionListener( 24 new ConcatenateAdapter()); JPanel row1Panel = new JPanel(); 27 row1Panel.add(number1Label); 28 row1Panel.add(num1TF); JPanel row2Panel = new JPanel(); 31 row2Panel.add(number2Label); 32 row2Panel.add(num2TF); JPanel row3Panel = new JPanel(); 35 row3Panel.add(concatenateButton); JPanel row4Panel = new JPanel(); 38 row4Panel.add(totalTF); USC CSCI 201L12/23 Event-Driven Programming Example 39 JPanel verticalPanel = new JPanel(); 40 verticalPanel.setLayout(new BoxLayout(verticalPanel, BoxLayout.Y_AXIS)); 41 verticalPanel.add(row1Panel); 42 verticalPanel.add(row2Panel); 43 verticalPanel.add(row3Panel); 44 verticalPanel.add(row4Panel); add(verticalPanel); 47 setSize(250, 175); 48 setLocationRelativeTo(null); 49 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 50 setVisible(true); 51 } public static void main(String args[]) { 54 Test t = new Test(); 55 } class ConcatenateAdapter implements ActionListener { 65 public void actionPerformed(ActionEvent ae) { 66 totalTF.setText(num1TF.getText() + num2TF.getText()); 67 } 68 } 69 }

Event-Driven Programming Example 1 import java.awt.event.ActionEvent; 2 import java.awt.event.ActionListener; 3 4 import javax.swing.BoxLayout; 5 import javax.swing.JButton; 6 import javax.swing.JFrame; 7 import javax.swing.JLabel; 8 import javax.swing.JPanel; 9 import javax.swing.JTextField; public class Test extends JFrame { 12 private JTextField num1TF, num2TF, totalTF; 13 private JLabel number1Label, number2Label; 14 private JButton concatenateButton; 15 public Test() { 16 super("Event-Driven Programming Example"); 17 number1Label = new JLabel("Number 1"); 18 num1TF = new JTextField("", 15); 19 number2Label = new JLabel("Number 2"); 20 num2TF = new JTextField("", 15); 21 totalTF = new JTextField("", 20); 22 concatenateButton = new JButton("Concatenate"); 23 class ConcatenateAdapter implements ActionListener { 24 public void actionPerformed(ActionEvent ae) { 25 Test.this.totalTF.setText(Test.this.num1TF.getText() + Test.this.num2TF.getText()); 26 } 27 } 28 concatenateButton.addActionListener( 29 new ConcatenateAdapter()); 30 USC CSCI 201L13/23 Event-Driven Programming Example 31 JPanel row1Panel = new JPanel(); 32 row1Panel.add(number1Label); 33 row1Panel.add(num1TF); JPanel row2Panel = new JPanel(); 36 row2Panel.add(number2Label); 37 row2Panel.add(num2TF); JPanel row3Panel = new JPanel(); 40 row3Panel.add(concatenateButton); JPanel row4Panel = new JPanel(); 43 row4Panel.add(totalTF); 44 JPanel verticalPanel = new JPanel(); 45 verticalPanel.setLayout(new BoxLayout(verticalPanel, BoxLayout.Y_AXIS)); 46 verticalPanel.add(row1Panel); 47 verticalPanel.add(row2Panel); 48 verticalPanel.add(row3Panel); 49 verticalPanel.add(row4Panel); add(verticalPanel); 52 setSize(250, 175); 53 setLocationRelativeTo(null); 54 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 55 setVisible(true); 56 } public static void main(String args[]) { 59 Test t = new Test(); 60 } 61 }

Event-Driven Programming Example 1 import java.awt.event.ActionEvent; 2 import java.awt.event.ActionListener; 3 4 import javax.swing.BoxLayout; 5 import javax.swing.JButton; 6 import javax.swing.JFrame; 7 import javax.swing.JLabel; 8 import javax.swing.JPanel; 9 import javax.swing.JTextField; public class Test extends JFrame { 12 private JTextField num1TF, num2TF, totalTF; 13 private JLabel number1Label, number2Label; 14 private JButton concatenateButton; 15 public Test() { 16 super("Event-Driven Programming Example"); 17 number1Label = new JLabel("Number 1"); 18 num1TF = new JTextField("", 15); 19 number2Label = new JLabel("Number 2"); 20 num2TF = new JTextField("", 15); 21 totalTF = new JTextField("", 20); 22 concatenateButton = new JButton("Concatenate"); 23 class ConcatenateAdapter implements ActionListener { 24 public void actionPerformed(ActionEvent ae) { 25 totalTF.setText(num1TF.getText() + num2TF.getText()); 26 } 27 } 28 concatenateButton.addActionListener( 29 new ConcatenateAdapter()); 30 USC CSCI 201L14/23 Event-Driven Programming Example 31 JPanel row1Panel = new JPanel(); 32 row1Panel.add(number1Label); 33 row1Panel.add(num1TF); JPanel row2Panel = new JPanel(); 36 row2Panel.add(number2Label); 37 row2Panel.add(num2TF); JPanel row3Panel = new JPanel(); 40 row3Panel.add(concatenateButton); JPanel row4Panel = new JPanel(); 43 row4Panel.add(totalTF); 44 JPanel verticalPanel = new JPanel(); 45 verticalPanel.setLayout(new BoxLayout(verticalPanel, BoxLayout.Y_AXIS)); 46 verticalPanel.add(row1Panel); 47 verticalPanel.add(row2Panel); 48 verticalPanel.add(row3Panel); 49 verticalPanel.add(row4Panel); add(verticalPanel); 52 setSize(250, 175); 53 setLocationRelativeTo(null); 54 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 55 setVisible(true); 56 } public static void main(String args[]) { 59 Test t = new Test(); 60 } 61 }

Event-Driven Programming Example 1 import java.awt.event.ActionEvent; 2 import java.awt.event.ActionListener; 3 4 import javax.swing.BoxLayout; 5 import javax.swing.JButton; 6 import javax.swing.JFrame; 7 import javax.swing.JLabel; 8 import javax.swing.JPanel; 9 import javax.swing.JTextField; public class Test extends JFrame { 12 private JTextField num1TF, num2TF, totalTF; 13 private JLabel number1Label, number2Label; 14 private JButton concatenateButton; 15 public Test() { 16 super("Event-Driven Programming Example"); 17 number1Label = new JLabel("Number 1"); 18 num1TF = new JTextField("", 15); 19 number2Label = new JLabel("Number 2"); 20 num2TF = new JTextField("", 15); 21 totalTF = new JTextField("", 20); 22 concatenateButton = new JButton("Concatenate"); 23 concatenateButton.addActionListener(new ActionListener() { 24 public void actionPerformed(ActionEvent ae) { 25 totalTF.setText(num1TF.getText() + num2TF.getText()); 26 } 27 }); 28 USC CSCI 201L15/23 Event-Driven Programming Example 29 JPanel row1Panel = new JPanel(); 30 row1Panel.add(number1Label); 31 row1Panel.add(num1TF); JPanel row2Panel = new JPanel(); 34 row2Panel.add(number2Label); 35 row2Panel.add(num2TF); JPanel row3Panel = new JPanel(); 38 row3Panel.add(concatenateButton); JPanel row4Panel = new JPanel(); 41 row4Panel.add(totalTF); 42 JPanel verticalPanel = new JPanel(); 43 verticalPanel.setLayout(new BoxLayout(verticalPanel, BoxLayout.Y_AXIS)); 44 verticalPanel.add(row1Panel); 45 verticalPanel.add(row2Panel); 46 verticalPanel.add(row3Panel); 47 verticalPanel.add(row4Panel); add(verticalPanel); 50 setSize(250, 175); 51 setLocationRelativeTo(null); 52 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 53 setVisible(true); 54 } public static void main(String args[]) { 57 Test t = new Test(); 58 } 59 }

Outline USC CSCI 201L16/23 ▪E▪Event-Driven Programming Overview ▪E▪Event-Driven Programming Example ▪A▪Adapter Classes ▪P▪Program

Listeners ▪The listeners are all interfaces, which means all of the methods in them are abstract ›Even if you only need one of the methods, you will still need to implement all of them, though methods you don’t need would have empty bodies USC CSCI 201L17/23 Adapter Classes 1 public class Test extends JFrame { 2 public Test() { 3 super("Event-Driven Programming Example"); 4 5 addWindowListener(new WindowListener() { 6 public void windowDeiconified(WindowEvent we) { 7 8 } 9 public void windowIconified(WindowEvent we) { } 12 public void windowActivated(WindowEvent we) { } 15 public void windowDeactivated(WindowEvent we) { } 18 public void windowOpened(WindowEvent we) { 19 System.out.println("opened"); 20 } 21 public void windowClosing(WindowEvent we) { } 24 public void windowClosed(WindowEvent we) { 25 System.out.println("closed"); 26 System.exit(1); 27 } 28 }); 29 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 30 setSize(250, 175); 31 setLocationRelativeTo(null); 32 setVisible(true); 33 } public static void main(String args[]) { 36 Test t = new Test(); 37 } 38 }

Adapters ▪Java provides support classes, called adapters, which provide default implementations for all the methods in corresponding listener interfaces ›The default implementation of a method is an empty body with the method signature ›Adapter only exist for interfaces that have more than one method. Why? USC CSCI 201L18/23 Adapter Classes

USC CSCI 201L19/23 Adapter Classes Event ClassListener InterfaceAdapter ClassListener Methods ActionEventActionListeneractionPerformed(ActionEvent) ItemEventItemListeneritemStateChanged(ItemEvent) MouseEvent MouseListenerMouseAdapter mousePressed(MouseEvent) mouseReleased(MouseEvent) mouseEntered(MouseEvent) mouseExited(MouseEvent) mouseClicked(MouseEvent) MouseMotionListenerMouseMotionAdapter mouseDragged(MouseEvent) mouseMoved(MouseEvent) KeyEventKeyListenerKeyAdapter keyPressed(KeyEvent) keyReleased(KeyEvent) keyTyped(KeyEvent) WindowEventWindowListenerWindowAdapter windowClosing(WindowEvent) windowOpened(WindowEvent) windowIconified(WindowEvent) windowDeiconified(WindowEvent) windowClosed(WindowEvent) windowActivated(WindowEvent) windowDeactivated(WindowEvent) ContainerEventContainerListenerContainerAdapter componentAdded(ContainerEvent) componentRemoved(ContainerEvent) ComponentEventComponentListenerComponentAdapter componentMoved(ComponentEvent) componentHidden(ComponentEvent) componentResized(ComponentEvent) componentShown(ComponentEvent) FocusEventFocusListenerFocusAdapter focusGained(FocusEvent) focusLost(FocusEvent) AdjustmentEventAdjustmentListeneradjustmentValueChanged(AdjustmentEvent) ChangeEventChangeListenerstateChanged(ChangeEvent) ListSelectionEventListSelectionListenervalueChanged(ListSelectionEvent)

Adapter Example ▪With an adapter, we only need to override the methods that we want to use instead of all the methods in the listener USC CSCI 201L20/23 Adapter Classes 1 public class Test extends JFrame { 2 public Test() { 3 super("Event-Driven Programming Example"); 4 5 addWindowListener(new WindowAdapter() { 6 public void windowOpened(WindowEvent we) { 7 System.out.println("opened"); 8 } 9 public void windowClosed(WindowEvent we) { 10 System.out.println("closed"); 11 System.exit(1); 12 } 13 }); 14 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 15 setSize(250, 175); 16 setLocationRelativeTo(null); 17 setVisible(true); 18 } public static void main(String args[]) { 21 Test t = new Test(); 22 } 23 }

Outline USC CSCI 201L21/23 ▪E▪Event-Driven Programming Overview ▪E▪Event-Driven Programming Example ▪A▪Adapter Classes ▪P▪Program

Program ▪Write a program that has three text fields for reading the loan amount, annual interest rate, and number of years from the user. When the user clicks the Calculate button or hits the Enter key, calculate the amount of the loan after the number of years assuming no money is paid to the principal. Print this value in a label. ›KeyEvent has a method called getKeyChar() ›KeyListener has a method called keyTyped(KeyEvent) USC CSCI 201L22/23 Program

▪Write a program to concatenate two strings together from two text fields into a third text field when the Concatenate button is clicked. USC CSCI 201L23/23 Program