1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.

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.
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.
Graphic User Interfaces Layout Managers Event Handling.
Bar Graph Design. Left-side/Right-side mechanical processing creative, abstract reasoning.
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.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
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.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Introduction to Java Swing “We are the sultans of swing” – Mark Knopfler.
Chapter 9: Applets Jim Burns Fall Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Creating User.
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,
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
– 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.
CS Lecture 00 Swing overview and introduction Lynda Thomas
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Event Handling. Event Driven Programming Flow of programs is determined by events. The Operating system recognizes events and passes them to the particular.
UID – Event Handling and Listeners Boriana Koleva
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.
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.
Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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.
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.
Lesson 28: More on the GUI button, frame and actions.
Event Handling CS 21a: Introduction to Computing I First Semester,
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
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.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
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
GUIs and Events Rick Mercer.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
CompSci 230 S Programming Techniques
Appendix I GUI Components and Event Handling
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Event Handling CS 21a: Introduction to Computing I
GUI Event Handling Nithya Raman.
Event-driven programming for GUI
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
Introduction to Event Handling
Constructors, GUI’s(Using Swing) and ActionListner
Chapter 5 Processing Input with Applets
Presentation transcript:

1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers Programming buttons Other GUI Components

2 Programming Actions: Implementing Listeners In Java, Listeners “listen” for events from components. 1. Create a listener (an object) 2. Attach listener to source component 3. When event occurs, source lets listener know Source Component Event Listener actionPerformed ( ActionEvent e ) ActionListener

3 Programming Actions: Implementing Listeners Can have multiple listeners listening for same event Source Component Event Listener 2 actionPerformed ( ActionEvent e ) ActionListener Listener 3 actionPerformed ( ActionEvent e ) ActionListener Listener 1 actionPerformed ( ActionEvent e ) ActionListener Event

4 Programming Actions: Implementing Listeners Listener acting on multiple events (of the same type) Source Components Event Listener actionPerformed ( ActionEvent e ) ActionListener Event

5 Implementing Listeners What does a listener class look like? import java.awt.*; import java.awt.event.*; // Simple listener class. class AL implements ActionListener { public void actionPerformed( ActionEvent e ) { System.out.println( “Something happened!”); }

6 Implementing Listeners What does a listener class look like? import java.awt.*; import java.awt.event.*; // Simple listener class. class AL implements ActionListener { public void actionPerformed( ActionEvent e ) { System.out.println( “Something happened!”); }

7 Implementing Listeners What does a listener class look like? import java.awt.*; import java.awt.event.*; // Simple listener class. class AL implements ActionListener { public void actionPerformed( ActionEvent e ) { System.out.println( “Something happened!”); }

8 Implementing Listeners What does a listener class look like? import java.awt.*; import java.awt.event.*; // Simple listener class. class AL implements ActionListener { public void actionPerformed( ActionEvent e ) { System.out.println( “Something happened!”); }

9 Implementing Listeners What does a listener class look like? import java.awt.*; import java.awt.event.*; // Simple listener class. class AL implements ActionListener { public void actionPerformed( ActionEvent e ) { System.out.println( “Something happened!”); } e.getActionCommand()

10 Implementing Listeners What does a listener class look like? import java.awt.*; import java.awt.event.*; // Simple listener class. class AL implements ActionListener { public void actionPerformed( ActionEvent e ) { System.out.println(“Something happened!”); }

11 Implementing Listeners How do you add a Listener to a component?... AL listener = new AL(); // create 1 listener JButton b1 = new JButton( “Button 1” ); JButton b2 = new JButton( “Button 2”); // Add listener to buttons b1.addActionListener(listener); b2.addActionListener(listener);...

12 Implementing Listeners What happens when I press a button? The Java system looks for the ActionListener object linked to the button and calls the actionPerformed method. listener actionPerformed ( ActionEvent e ) AL ActionEvent “Something happened!” ActionEvent

13 Implementing Listeners: Shorthand Don’t have to create a new class to implement listener. Can use an anonymous inner class: JButton b = new JButton(“Button 1”); b.addActionListener( new ActionListener() { public void actionPerformed (ActionEvent event){ System.out.println(“Something happened!”); } // end of method } // end of anon. inner class ); // end of addActionListener method

14 Implementing Listeners: Shorthand Don’t have to create a new class to implement listener. Can use an anonymous inner class: JButton b = new JButton(“Button 1”); b.addActionListener( new ActionListener() { public void actionPerformed (ActionEvent event){ System.out.println(“Something happened!”); } // end of method } // end of anon. inner class ); // end of addActionListener method

15 Implementing Listeners: Shorthand Don’t have to create a new class to implement listener. Can use an anonymous inner class: JButton b = new Jbutton(“Button 1”); b.addActionListener( new ActionListener() { public void actionPerformed (ActionEvent event){ System.out.println(“Something happened!”); } // end of method } // end of anon. inner class ); // end of addActionListener method Note: Look for “Sample$1.class” files Note: Look for “Sample$1.class” files

16 Listener Types: Act that results in the event type User clicks a button, presses Return while typing in a text field, or chooses a menu item. User moves a slider. User closes a frame (main window). User presses a mouse button while the cursor is over a component. User moves the mouse over a component. Listener ActionListener ChangeListener WindowListener MouseListener MouseMotionListener

17 Adapters: A Shortcut Listeners are interfaces To implement listeners, must define every method of the interface Adapters are classes that implement listeners with empty methods Simply extend adapter and override required method instead of implementing every method of listener

18 E.g. Shutting Down a Window: WindowListener declares many methods that must be implemented (for opening, closing, activating, deactivating, iconifing, deiconifing). WindowAdapter implements WindowListener with empty methods. To use, inherit and overwrite those of interest.

19 E.g. Shutting Down a Window: JFrame.EXIT_ON_CLOSE only available for Java 1.3 and up. Another way: frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } );

20 E.g. Shutting Down a Window: JFrame.EXIT_ON_CLOSE only available for Java 1.3 and up. Another way: frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } );

21 E.g. Shutting Down a Window: JFrame.EXIT_ON_CLOSE only available for Java 1.3 and up. Another way: frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } );

22 E.g. Shutting Down a Window: JFrame.EXIT_ON_CLOSE only available for Java 1.3 and up. Another way: frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } );

23 When to Use an Adapter If listener only has one method, no adapter is needed or defined.- e.g. ActionListener If you are going to implement every method, use either the listener or the adapter If you only need a few of the methods, use an adapter.- e.g. WindowAdapter

24 Other Components: JTextField JSlider User defined - ColorBlock

25 Input of Character Strings The JTextField class implements a small area into which the user can type a sequence of characters that the program can then extract and process in some way. The constructor for this class takes two arguments: the initial text string to appear in the text field, and an integer specifying the width. The listener interface to implement for JTextField is the ActionListener. The getText() method returns the string inside the field.

26 Sliders The JSlider class is used to let the user enter a numeric value bounded by a minimum and maximum value (Horizontal or Vertical). The listener interface to implement for JSlider is the ChangeListener. The method called when the user scrolls is called: stateChanged. Use getValue method to return position of the slider. Use setValue method to change position of the slider.

27 ColorBlock (RGB) extends JLabel JLabel methods : –setBackground, setForeground ( Color ) –setText New methods: setColor, setRed,... ?

28 Other components: Search the API Reference Go through the Swing Tutorials Sample Code

29 Tutorial 1