TCU CoSc 10403 Programming with Java Handling Events.

Slides:



Advertisements
Similar presentations
Event handling and listeners What is an event? user actions and context event sources and listeners Why should my programs be event- driven? User interaction.
Advertisements

1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
CS18000: Problem Solving and Object-Oriented Programming.
Graphic User Interfaces Layout Managers Event Handling.
Graphical User Interfaces, 2D Graphics & Game Programming.
Event-Driven Programming You might have realized in creating a GUI and clicking on the JButtons that nothing happens Clicking on a JButton causes the JVM.
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.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
Event Handling Events and Listeners Timers and Animation.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
EVENTS CSC 171 FALL 2004 LECTURE 16. “Traditional” Input In console applications, user input is under control of the program The program asks the user.
GUI Programming in Java
Chapter 11 Java AWT Part I: Mouse Events (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
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,
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching.
Graphical User Interface Components: Part 1 Chapter 11.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.
Layout Managers Arranges and lays out the GUI components on a container.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
For (int i = 1; i
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
UID – Event Handling and Listeners Boriana Koleva
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
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.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
Object Oriented Programming.  Interface  Event Handling.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
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.
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an 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.
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.
1 Lecture 8: User Interface Components with Swing.
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
GUI Programming using Java - Event Handling
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Events and Event Handling
CompSci 230 S Programming Techniques
Chapter 12 Event-Driven Programming
A First Look at GUI Applications
Advanced User Interfaces
Lecture 09 Applets.
Graphical User Interface (pronounced "gooey")
Programming in Java Event Handling
CSE 114 – Computer Science I Event Programming
Ellen Walker Hiram College
GUI Programming III: Events
Event-driven programming for GUI
Introduction to Event Handling
Web Design & Development Lecture 12
Presentation transcript:

TCU CoSc Programming with Java Handling Events

What is an Event in Java? Def n : - in programming, an event is (in most cases) an action taken by the user. Types of events: 1)the user presses a key or clicks the mouse button. 2)the user clicks on a button, a choice, a checkbox, etc. Thus, rather than a program be executed as a long sequence of instructions, like the following: We have sections of the program which can be activated by the occurrence of an event. Sequential Execution

Listeners GUI Program- specific Action handlers Add Listeners Handle the Events Event Effects Basic Event Handling The GUI is responsible for constructing the user interface and for connecting (registering) widgets to listeners The listener part implements the appropriate interface for the type of event(s) of interest. The code (in the action handler) that performs the program’s action associated with the event(s).

The Event Loop In Java, the programmer creates a section(s) of code (methods) which will be automatically invoked whenever an event happens. Details of the event (the name of the button that was pressed, the choice box that was changed, the radio button that was pressed, etc) are returned to the programmer so that the required processing takes place. In Java, events are classified into several classes (we will study the following): 1)ActionEvents - generated whenever a change is made to a JButton, JCheckBox, JComboBox, JTextField, or JRadioButton.. 2)TextEvents-generated whenever a change is made to an AWT TextArea or an AWT TextField (not implemented in swing). 3)ItemEvents-generated whenever a change is made to a JButton, JCheckBox, JComboBox, or JRadioButton. Button pressed? Choice made? Radio button pressed? Etc… Event loop The software cycles around in a loop waiting for an event to occur:

Event Handlers that We Will Study Listener interfaces and related classes Event ClassListener Interface MethodsAssociation MethodsGenerated by Class ActionEventinterface ActionListeneraddActionListenerJButton actionPerformed(ActionEvent)removeActionListenerJCheckBox JComboBox JTextField JRadioButton ItemEventinterface ItemListeneraddItemListenerJCheckBox itemStateChanged(ItemEvent)removeItemListenerJRadioButton JComboBox MouseEventinterface MouseListeneraddMouseListenergenerated by mouse mousePressed(MouseEvent)removeMouseListenerevent on any component mouseReleased(MouseEvent) mouseEntered(MouseEvent) mouseExited(MouseEvent) mouseClicked(MouseEvent)

Using Action Events 1.Import the required event classes; thus giving your applet the power to receive and handle messages, such as those sent when a button is clicked or the mouse is moved. import java.awt.event.*; 2.State that the class implements an ActionListener. public class IRS extends JApplet implements ActionListener 3&4.Declare and create a button variable, giving it an appropriate name. JButton calculate = new JButton("Calculate now"); 5.Add the button to the applet window. add(calculate); 6.Inform the button that this object will respond to button events, using addActionListener. calculate.addActionListener(this); Note:this acts like the word “me” in English. It refers to the applet itself. The message says, in effect, “send me a message whenever the calculate button it clicked: I’m listening.” 7.Provide a method called actionPerformed() to be invoked when a button click event occurs. public void actionPerformed( ActionEvent e )

What are the Parts? import java.awt.*; import javax.swing.*; import java.awt.event.*; public class IRS extends JApplet implements ActionListener { //normal declarations for JTextFields, JLabels, JPanels, and //other widgets needed for the user interface JButton calculate = new JButton("Calculate now"); // button for user to use public void init() { //define layout methods to be used and add the various //GUI components to create the user interface somepanel.add(calculate); calculate.addActionListener(this); } public void actionPerformed( ActionEvent e ) { //local variable declarations and Java code required to calculate //taxes owed and net pay given an employee name, ssn, gross //salary earned, number of dependents, and marital status } } Step #1 Step #2 Steps #3 & 4 Step #5 Step #6 Step #7

Action Event Demo1 import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ActionEventDemo1 extends JApplet implements ActionListener { JButton b1 = new JButton("Submit"); public void init() { setLayout(new FlowLayout()); add(b1); b1.addActionListener(this); } public void actionPerformed(ActionEvent e) { System.out.println("Button pressed"); }

Action Event Demo2 import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ActionEventDemo1 extends JApplet implements ActionListener { JButton b1 = new JButton("Submit"); public void init() { setLayout(new FlowLayout()); add(b1); b1.addActionListener(this); } public void actionPerformed(ActionEvent e) { String s = b1.getText(); //Gets the label off of the button System.out.println(s + " Button pressed"); }

Action Event Demo3 import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ActionEventDemo1 extends JApplet implements ActionListener { JButton b1 = new JButton("Submit"); public void init() { setLayout(new FlowLayout()); add(b1); b1.addActionListener(this); } public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); //returns the command string associated with event System.out.println(s + " Button pressed"); }

Action Event Demo4 import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ActionEventDemo1 extends JApplet implements ActionListener { JButton b1 = new JButton(”Finis"); public void init() { setLayout(new FlowLayout()); add(b1); b1.addActionListener(this); b1.setActionCommand("Finish up button pressed"); } public void actionPerformed(ActionEvent e) { String s = b1.getActionCommand(); //returns the parameter string for setActionCommand System.out.println(s + " Button pressed"); } There may be times that a different internal name is more meaningful than the string used to label the button (e.g., an application meant to be used in France might have a button with a French label – but but with a more meaningful internal English name.

Action Event Demo5 import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ActionEventDemo1 extends JApplet implements ActionListener { JButton b1 = new JButton("Submit"); JButton b2 = new JButton("Cancel"); public void init() { setLayout(new FlowLayout()); add(b1); b1.addActionListener(this); add(b2); b2.addActionListener(this); } public void actionPerformed(ActionEvent e) { System.out.println("Button pressed"); } The problem is that this program does not distinguish between the two buttons.

Action Event Demo6 import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ActionEventDemo1 extends JApplet implements ActionListener { JButton b1 = new JButton("Submit"); JButton b2 = new JButton("Cancel"); public void init() { setLayout(new FlowLayout()); add(b1); b1.addActionListener(this); b1.setActionCommand("Process a Submit Order"); add(b2); b2.addActionListener(this); b2.setActionCommand("Process a Order Cancellation"); } public void actionPerformed(ActionEvent e) { System.out.println(e.getActionCommand()); }

Event Demo(getActionCommand on JTextField ) import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ActionEventDemo1 extends JApplet implements ActionListener { JTextField tf1 = new JTextField(8); JTextField tf2 = new JTextField(20); public void init() { setLayout(new FlowLayout()); add(tf1); tf1.addActionListener(this); tf1.setActionCommand("Text entered into Name Field"); add(tf2); tf2.addActionListener(this); tf2.setActionCommand("Text entered into Address Field"); } public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); System.out.println(s); }

import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ActionEventDemo1 extends JApplet implements ActionListener { JButton b1 = new JButton("Submit"); JButton b2 = new JButton("Cancel"); public void init() { setLayout(new FlowLayout()); add(b1); b1.addActionListener(this); add(b2); b2.addActionListener(this); } public void processSubmit() { //code for processing an order submission System.out.println("Order submission"); } public void processCancel() { //code for processing an order cancellation System.out.println("Order cancellation"); } public void actionPerformed(ActionEvent e) { if (e.getSource()==b1) processSubmit(); //invoke the processSubmit method else processCancel(); //invoke the processCancel method } Action Event Demo7

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ItemEventDemo extends JApplet implements ItemListener { JCheckBox dbRed = new JCheckBox("Red"); JCheckBox dbBlue = new JCheckBox("Blue"); JRadioButton rbWhite = new JRadioButton("White"); JRadioButton rbGreen = new JRadioButton("Green"); ButtonGroup grp = new ButtonGroup(); JComboBox cbb = new JComboBox(); public void init() { setLayout(new FlowLayout()); add(dbRed); dbRed.addItemListener(this); add(dbBlue); dbBlue.addItemListener(this); grp.add(rbWhite); grp.add(rbGreen); add(rbWhite); rbWhite.addItemListener(this); add(rbGreen); rbGreen.addItemListener(this); add(cbb); cbb.addItem("TCU"); cbb.addItem("SMU"); cbb.addItemListener(this); } public void itemStateChanged(ItemEvent e) { if (e.getSource() == dbRed) System.out.println("Red CheckBox clicked on"); else if (e.getSource()==dbBlue) System.out.println("Blue CheckBox clicked on"); else if (e.getSource()==rbWhite) System.out.println("White RadioButton clicked on"); else if (e.getSource()==rbGreen) System.out.println("Green RadioButton clicked on"); else if (e.getSource()==cbb) System.out.println("JComboBox clicked on -" + (String)cbb.getSelectedItem()); } Other Events (ItemEvents)

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MouseEventDemo extends JApplet implements MouseListener { JLabel l1 = new JLabel("Click Here"); JButton b1 = new JButton("Cancel"); JTextField tf = new JTextField(10); public void init() { setLayout(new FlowLayout()); add(l1); l1.addMouseListener(this); add(b1); b1.addMouseListener(this); add(tf); tf.addMouseListener(this); } public void mousePressed(MouseEvent e) { System.out.println("Mouse pressed - # of clicks: " + e.getClickCount()); } public void mouseReleased(MouseEvent e) { System.out.println("Mouse released - # of clicks: " + e.getClickCount()); } public void mouseEntered(MouseEvent e) { System.out.println("Mouse entered"); } public void mouseExited(MouseEvent e) { System.out.println("Mouse exited"); } public void mouseClicked(MouseEvent e) { System.out.println("Mouse clicked - # of clicks: " + e.getClickCount()); } Other Events (MouseEvents)

Mouse events are generated by the following types of user interaction: –A mouse click –A mouse entering a component's area –A mouse leaving a component's area Any component can generate these events, and a class must implement the MouseListener interface to support them. Methods: void mouseClicked(MouseEvent e) – invoked when the mouse button has been clicked (pressed and released) on a component. void mouseDragged(MouseEvent e) – invoked when a mouse button is pressed on a component and then dragged. void mouseEntered(MouseEvent e) - Invoked when the mouse enters a component. void mouseExited(MouseEvent e) - Invoked when the mouse exits a component. void mouseMoved(MouseEvent e) - Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed. void mousePressed(MouseEvent e) - Invoked when a mouse button has been pressed on a component. void mouseReleased(MouseEvent e) - Invoked when a mouse button has been released on a component. void mouseDragged(MouseEvent e) -Invoked when a mouse button is pressed on a component and then dragged. MouseEvents