GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Graphical User Interface (GUI) Programming III. Lecture Objectives Exploring more GUI programming elements in Java Using labels in GUIs Using colors to.
Event Handling Events and Listeners Timers and Animation.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 : Event-Driven Programming and GUI Objects.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Chapter 7 Event-Driven Programming and Basic GUI Objects.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 14 GUI and Event-Driven Programming.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
Java Programming Chapter 10 Graphical User Interfaces.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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,
Graphical User Interface in Java
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
FEN IntroJava2006 AAU1 GUI: Graphical User Interface AWT/SWING: Components Drag and Drop in NetBeans Events Listeners.
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
OOP (Java): GUI Intro/ OOP Objectives – –use an image viewer application to introduce Java's GUI features Semester 2,
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 15 Creating User.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
DCS2133 Object Oriented Programming Graphical User Interface & Event-Driven Programming.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-2 ( Book Chapter 14) GUI and Event-Driven Programming.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
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,
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.
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
GUIs & Event-Driven Programming Chapter 11 Review.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
GUIs and Events Rick Mercer.
CSC 205 Programming II Lecture 5 AWT - I.
Chapter 8 Event Handling.
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Introduction to Event Handling
Chapter 7-2 (Book Chapter 14)
Chapter 7 (Book Chapter 14)
Chapter 7-2 (Book Chapter 14)
GUI and Event-Driven Programming
Presentation transcript:

GUI and Event-Driven Programming Part 2

Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism to process events is called event handling. The event-handling model of Java is based on the concept known as the delegation-based event model. With this model, event handling is implemented by two types of objects: – event source objects – event listener objects.

Event Sources and Listeners event source generates An event source object is a GUI object where an event occurs. An event source generates events. event listener methodgets executed in response to the generated events An event listener object is an object that includes a method that gets executed in response to the generated events. When an event is generated, the system notifies the relevant event listener objects.

Events and Event Listeners There are many different kinds of events, but the most common one is an action event. For the generated events to be processed, we must associate, or register, event listeners to the event sources. If event sources have no registered listeners, the generated events are ignored.

Events and Event Listeners An object that can be registered as an action listener must be an instance of a class that is declared specifically for the purpose. We call such classes action listener classes. To associate an action listener to an action event source, we call the event source’s addActionListener method with the action listener as its argument.

Events and Event Listeners A single listener can be associated to multiple event sources. Likewise, multiple listeners can be associated to a single event source. When an event source generates an event, the system checks for matching registered listeners. – If there is no matching listener, the event is ignored. – If there is a matching listener, the system notifies the listener by calling the listener’s corresponding method.

Action Listener Classes In the case of action events, the method called is actionPerformed. To ensure the programmer includes the necessary actionPerformed method in the action listener class, the class must be defined in a specific way. import java.awt.event.*; public class ButtonHandler implements ActionListener {... }

The ActionListener interface ActionListener is an interface, not a class. Like a class, an interface is a reference data type, but unlike a class, an interface includes only constants and abstract methods. An abstract method has only the method header, or prototype; it has no method body. A class that implements a Java interface must provide the method body to all the abstract methods defined in the interface.

The ActionListener interface By requiring an object we pass as an argument to the addActionListener method to be an instance of a class that implements the ActionListener interface, the system ensures that this object will include the necessary actionPerformed method. Implementation of an interface is Java’s mechanism for providing a form of multiple inheritance

Example Suppose we want to change the title of the frame, depending on which button is clicked We can do this using the actionPerformed method. The method model is: public void actionPerformed(ActionEvent evt) { String buttonText = get the text of the event source; JFrame frame = the frame that contains this event source; frame.setTitle(“You clicked “ + buttonText); }

Handling Button Events The first statement may be handled in one of two ways. The first way is via the getActionCommand method of the action event object evt: String buttonText = evt.getActionCommand(); The second way is via the getSource method of the action event object evt.: JButton clickedButton = (JButton) evt.getSource(); String buttonText = clickedButton.getText();

Handling Button Events A frame window contains nested layers of panes. The topmost pane is called the root pane. To find the frame that contains the event source, we – get the root pane to which the event source belongs, then – get the frame that contains this root pane. JRootPane rootPane = clickedButton.getRootPane(); Frame frame = (JFrame) rootPane.getParent(); The frame can be the event listener of the GUI objects it contains. cancelButton.addActionListener(this); okButton.addActionListener(this);

Swing Classes for Text Handling The Swing GUI classes JLabel, JTextField, and JTextArea all deal with text. – A JLabel object displays uneditable text. – A JTextField object allows the user to enter a single line of text. – A JTextArea object allows the user to enter multiple lines of text. It can also be used for displaying multiple lines of uneditable text.

Text Fields and Action Events An instance of JTextField generates action events when the object is active and the user presses the ENTER key. The actionPerformed method must determine which of the event sources (for example, a button or a text field) generated the event. The instanceof operator determines the class to which the event source belongs. The general model is thus: if (event.getSource() instanceof JButton ){ // event source is either cancelButton // or okButton... } else { // event source must be inputLine... }

Text Fields and Action Events The getText method of JTextField may be used to retrieve the text that the user entered. public void actionPerformed(ActionEvent event) { if (event.getSource() instanceof JButton){ JButton clickedButton = (JButton) event.getSource(); String buttonText = clickedButton.getText(); setTitle(“You clicked ” + buttonText); } else {// the event source is inputLine setTitle(“You entered ‘” + inputLine.getText() + “’”); }

JLabel Objects A JLabel object is useful for displaying a label indicating the purpose of another object, such as a JTextField object. prompt = new JLabel(“Please enter your name”); JLabel objects may also be used to display images. When a JLabel object is created, we can pass an ImageIcon object instead of a string. To create the ImageIcon object, we must specify the filename of the image.

JLabels and Images We declare the data member private JLabel image; then create it in the constructor as public TextFrame2 () {... image = new JLabel(new ImageIcon(“cat.gif”); // assumes the.gif is/in the same directory as the program image.setBounds(10, 20, 50, 50); contentPane.add(image);... }

Example with JLabel, JButton and JTextField objects

TextArea & Related Methods A TextArea is like a TextField, but can accommodate more text Methods include: setBorder(BorderFactory); // creates a border around a JTextArea object setEditable(boolean); // enables/disables changes to a JTextArea object The append(String) method allows us to add text to the text area without replacing old content. Using the setText (String) method of JTextArea will replace old content with new content.

Example frame with JTextArea object

Adding Scrollbars to a JTextArea JScrollPane scrollText = new JScrollPane(textArea); contentPane.add(scrollText);

Menus The javax.swing package contains three useful menu-related classes: JMenuBar, JMenu, and JMenuItem. – MenuBar is a bar where the menus are placed. – Menu (such as File or Edit) is a single item in the MenuBar. – MenuItem (such as Copy, Cut, or Paste) is an individual menu choice in the Menu.

Menus When a MenuItem is selected, it generates a menu action event. We process menu selections by registering an action listener to menu items. The following example will create the two menus shown and illustrate how to display and process the menu item selections.

Menus We will follow this sequence of steps: 1. Create a JMenuBar object and attach it to a frame. 2. Create a JMenu object. 3. Create JMenuItem objects and add them to the JMenu object. 4. Attach the JMenu object to the JMenuBar object.

Menus We create a fileMenu object as fileMenu = new Menu(“File”); The argument to the Menu constructor is the name of the menu. Menu items appear from the top in the order in which they were added to the menu.

Menus To create and add a menu item New to fileMenu, we execute item = new MenuItem(“New”); item.addActionListener(this); fileMenu.add(item); And to add a horizontal line as a separator between menu items, we execute fileMenu.addSeparator();

Menus We then attach the menus to a menu bar. We create a MenuBar object in the constructor, call the frame’s setMenuBar method to attach it to the frame, and add the two Menu objects to the menu bar. MenuBar menuBar = new MenuBar(); setMenuBar(menuBar); menuBar.add(fileMenu); menuBar.add(editMenu);