Jozef Goetz 2016 1 1 Credits: Copyright  2092-2016 Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.

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

Graphic User Interfaces Layout Managers Event Handling.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Event Handling Events and Listeners Timers and Animation.
Event-Driven Programming
Graphical User Interface Components: Part 1
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Introduction to Java Swing “We are the sultans of swing” – Mark Knopfler.
GUI Programming in Java
Java Programming Chapter 10 Graphical User Interfaces.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
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.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 - Graphical User Interface Concepts: Part.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Graphical User Interface Components: Part 1 Chapter 11.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– 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.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Dale Roberts GUI Programming using Java - GUI Components Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
Omer Boyaci.  GUIs are event driven.  When the user interacts with a GUI component, the interaction—known as an event—drives the program to perform.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
AGDER COLLEGEFACULTY OF ENGINEERING & SCIENCE GUI Components ikt403 – Object-Oriented Software Development.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
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.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
Object Oriented Programming.  Interface  Event Handling.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Javax.swing Class JOptionPane Java.lang.Object java.awt.Component java.awt.Container javax.swing.JComponent javax.swing.JOptionPane.
Graphical User Interface Components: Part 1 Chapter 11.
Creating User Interfaces Event-Driven Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
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.
 Figure illustrates a hierarchy containing many event classes from the package java.awt.event.  Used with both AWT and Swing components.  Additional.
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.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
GUIs & Event-Driven Programming Chapter 11 Review.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
© Copyright by Pearson Education, Inc. All Rights Reserved. Appendix I GUI Components and Event Handling Android How to Program, 3/e.
GUI Programming using Java - Event Handling
CSC 205 Programming II Lecture 5 AWT - I.
Chapter 14 Event-Driven Programming
CompSci 230 S Programming Techniques
Java FX: Scene Builder.
Appendix I GUI Components and Event Handling
A First Look at GUI Applications
Graphical User Interface (GUI) Components: Part 1 Java How to Program, 9 th Edition Chapter 14.
Introduction to Event Handling
Advanced Programming in Java
Presentation transcript:

Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016

Jozef Goetz

3

4 I.1 Introduction  A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an app.  Pronounced “GOO-ee”  Gives an app a distinctive “look” and “feel.”  Consistent, intuitive user-interface components give users a sense of familiarity  Learn new apps more quickly and use them more productively.

Jozef Goetz I.1 Introduction  Built from GUI components.  Sometimes called controls or widgets—short for window gadgets.  User interacts via the mouse, the keyboard or another form of input, such as voice recognition.

Jozef Goetz I.2 Nimbus Look-and-Feel  In our screen captures, we use Java’s elegant, cross-platform look-and-feel known as Nimbus.  Three ways to use Nimbus:  Set it as the default for all Java apps that run on your computer.  Set it as the look-and-feel when you launch an app by passing a command-line argument to the java command.  Set it as the look-and-feel programatically in your app.

Jozef Goetz I.2 Java’s Nimbus Look-and-Feel  To set Nimbus as the default for all Java apps:  Create a text file named swing.properties in the lib folder of both your JDK installation folder and your JRE installation folder.  Place the following line of code in the file: swing.defaultlaf= com.sun.java.swing.plaf.nimbus.NimbusLookAndFe el  For more information on locating these installation folders visit  ns ns  In addition to the standalone JRE, there is a JRE nested in your JDK’s installation folder.  If you are using an IDE that depends on the JDK (e.g., NetBeans), you may also need to place the swing.properties file in the nested jre folder’s lib folder.

Jozef Goetz I.2 Java’s Nimbus Look-and-Feel  To select Nimbus on an app-by-app basis:  Place the following command-line argument after the java command and before the app’s name when you run the app: -Dswing.defaultlaf= com.sun.java.swing.plaf.nimbus. NimbusLookAndFeel

Jozef Goetz I.3 Text Fields and an Introduction to Event Handling with Nested Classes  GUIs are event driven.  When the user interacts with a GUI component, the interaction—known as an event—drives the program to perform a task.  The code that performs a task in response to an event is called an event handler, and the overall process of responding to events is known as event handling.

Jozef Goetz I.3 Text Fields and an Introduction to Event Handling with Nested Classes  JTextField s and JPasswordField s (package javax.swing ).  JTextField extends class JTextComponent (package javax.swing.text ), which provides many features common to Swing’s text-based components.  Class JPasswordField extends JTextField and adds methods that are specific to processing passwords.  JPasswordField shows that characters are being typed as the user enters them, but hides the actual characters with an echo character.

Jozef Goetz I.3 Text Fields and an Introduction to Event Handling with Nested Classes  Must attach each GUI component to a container, such as a JFrame.

Jozef Goetz I.3 Text Fields and an Introduction to Event Handling with Nested Classes  Must register an object as the event handler for each text field.  addActionListener registers an ActionListener object to handle ActionEvent s.  After an event handler is registered the object listens for events.

Jozef Goetz  When the user types data into a JTextField or a JPasswordField, then presses Enter, an event occurs.  You can type only in the text field that is “in focus.”  A component receives the focus when the user clicks the component.

Jozef Goetz  When the user presses Enter in a JTextField or JPasswordField, an ActionEvent (package java.awt.event ) occurs.  Processed by an object that implements the interface ActionListener (package java.awt.event ).  To handle ActionEvent s, a class must implement interface ActionListener and declare method actionPerformed.  This method specifies the tasks to perform when an ActionEvent occurs.  The GUI component with which the user interacts is the event source.  ActionEvent method getSource (inherited from class EventObject ) returns a reference to the event source.  ActionEvent method getActionCommand obtains the text the user typed in the text field that generated the event.  JPasswordField method getPassword returns the password’s characters as an array of type char.

Jozef Goetz I.3 Text Fields and an Introduction to Event Handling with Nested Classes  Must attach each GUI component to a container, such as a JFrame.  You typically must decide where to position each GUI component.  Known as specifying the layout of the GUI components.  Java provides several layout managers that can help you position components.  Many IDEs provide GUI design tools in which you can specify the exact size and location of a component  IDE generates the GUI code for you  Greatly simplifies GUI creation  To ensure that this book’s examples can be used with any IDE, we did not use an IDE to create the GUI code  We use Java’s layout managers in our GUI examples

Jozef Goetz I.3 Text Fields and an Introduction to Event Handling with Nested Classes  FlowLayout layout manager  GUI components are placed on a container from left to right in the order in which the program attaches them to the container.  When there is no more room to fit components left to right, components continue to display left to right on the next line.  If the container is resized, a FlowLayout reflows the components to accommodate the new width of the container, possibly with fewer or more rows of GUI components.  Method setLayout is inherited from class Container.  argument must be an object of a class that implements the LayoutManager interface (e.g., FlowLayout ).

Jozef Goetz I.3 Text Fields and an Introduction to Event Handling with Nested Classes  Before an app can respond to an event for a particular GUI component, you must:  Create a class that represents the event handler and implements an appropriate interface—known as an event-listener interface.  Indicate that an object of the class from Step 1 should be notified when the event occurs—known as registering the event handler.  All the classes discussed so far were so-called top-level classes—that is, they were not declared inside another class.  Java allows you to declare classes inside other classes— these are called nested classes.  Can be static or non- static.  Non- static nested classes are called inner classes and are frequently used to implement event handlers.

Jozef Goetz I.3 Text Fields and an Introduction to Event Handling with Nested Classes  Before an object of an inner class can be created, there must first be an object of the top-level class that contains the inner class.  This is required because an inner-class object implicitly has a reference to an object of its top-level class.  There is also a special relationship between these objects —the inner-class object is allowed to directly access all the variables and methods of the outer class.  A nested class that is static does not require an object of its top-level class and does not implicitly have a reference to an object of the top-level class.  Nested classes can be declared public, protected or private.  Since event handlers tend to be specific to the app in which they are defined, they are often implemented as private inner classes or as anonymous inner classes. 

Jozef Goetz I.3 Text Fields and an Introduction to Event Handling with Nested Classes  GUI components can generate many events in response to user interactions.  Each event is represented by a class and can be processed only by the appropriate type of event handler.  Normally, a component’s supported events are described in the Java API documentation for that component’s class and its superclasses.

Jozef Goetz

Jozef Goetz I.4 Common GUI Event Types and Listener Interfaces  Figure I.3 illustrates a hierarchy containing many event classes from the package java.awt.event.  Additional event types are declared in package javax.swing.event.

Jozef Goetz I.4 Common GUI Event Types and Listener Interfaces  Delegation event model —an event’s processing is delegated to an object (the event listener) in the app.  For each event-object type, there is typically a corresponding event-listener interface.  Each event-listener interface specifies one or more event-handling methods that must be declared in the class that implements the interface.  When an event occurs, the GUI component with which the user interacted notifies its registered listeners by calling each listener’s appropriate event-handling method. 

Jozef Goetz I.5 How Event Handling Works  How the event-handling mechanism works:  Every JComponent has a variable listenerList that refers to an EventListenerList (package javax.swing.event ).  Maintains references to registered listeners in the listenerList.  When a listener is registered, a new entry is placed in the component’s listenerList.  Every entry also includes the listener’s type.

Jozef Goetz I.5 How Event Handling Works  How does the GUI component know to call actionPerformed rather than another method?  Every GUI component supports several event types, including mouse events, key events and others.  When an event occurs, the event is dispatched only to the event listeners of the appropriate type.  Dispatching is simply the process by which the GUI component calls an event-handling method on each of its listeners that are registered for the event type that occurred.

Jozef Goetz I.5 How Event Handling Works  Each event type has one or more corresponding event-listener interfaces.  ActionEvent s are handled by ActionListener s  MouseEvent s are handled by MouseListener s and MouseMotionListener s  KeyEvent s are handled by KeyListener s  When an event occurs, the GUI component receives (from the JVM) a unique event ID specifying the event type.  The component uses the event ID to decide the listener type to which the event should be dispatched and to decide which method to call on each listener object.  For an ActionEvent, the event is dispatched to every registered ActionListener ’s actionPerformed method.  For a Mouse-Event, the event is dispatched to every registered MouseListener or MouseMotionListener, depending on the mouse event that occurs.  The MouseEvent ’s event ID determines which of the several mouse event-handling methods are called.

Jozef Goetz I.6 JButton  A button is a component the user clicks to trigger a specific action.  Several types of buttons  command buttons  checkboxes  toggle buttons  radio buttons  Button types are subclasses of AbstractButton (package javax.swing ), which declares the common features of Swing buttons.

Jozef Goetz I.6 JButton  A command button generates an ActionEvent when the user clicks it.  Command buttons are created with class JButton.  The text on the face of a JButton is called a button label.

Jozef Goetz

Jozef Goetz  JButton s, like JTextField s, generate ActionEvent s that can be processed by any ActionListener object.  A JButton can display an Icon.  A JButton can also have a rollover Icon  displayed when the user positions the mouse over the JButton.  The icon on the JButton changes as the mouse moves in and out of the JButton ’s area on the screen.  AbstractButton method setRolloverIcon specifies the image displayed on the JButton when the user positions the mouse over it.

Jozef Goetz I.6 JButton

Jozef Goetz I.6 JButton

Jozef Goetz I.7 JComboBox ; Using an Anonymous Inner Class for Event Handling  The first item added to a JComboBox appears as the currently selected item when the JComboBox is displayed.  Other items are selected by clicking the JComboBox, then selecting an item from the list that appears.  JComboBox method setMaximumRowCount sets the maximum number of elements that are displayed when the user clicks the JComboBox.  If there are additional items, the JComboBox provides a scrollbar that allows the user to scroll through all the elements in the list.

Jozef Goetz I.7 JComboBox ; Using an Anonymous Inner Class for Event Handling

Jozef Goetz I.7 JComboBox ; Using an Anonymous Inner Class for Event Handling  ItemEvent method getStateChange returns the type of state change.  ItemEvent.SELECTED indicates that an item was selected.  JComboBox method getSelectedIndex returns the index of the selected item.  For each item selected from a JComboBox, another item is first deselected —so two ItemEvent s occur when an item is selected.

Jozef Goetz

Jozef Goetz I.7 JComboBox ; Using an Anonymous Inner Class for Event Handling  An anonymous inner class is an inner class that is declared without a name and typically appears inside a method declaration.  As with other inner classes, an anonymous inner class can access its top-level class’s members.  An anonymous inner class has limited access to the local variables of the method in which it’s declared.  Since an anonymous inner class has no name, one object of the anonymous inner class must be created at the point where the class is declared.

Jozef Goetz Adapter Classes  Many event-listener interfaces contain multiple methods.  An adapter class implements an interface and provides a default implementation (with an empty method body) of each method in the interface.  You extend an adapter class to inherit the default implementation of every method and override only the method(s) you need for event handling.