GUI DYNAMICS Lecture 11 CS2110 – Fall 2009. GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,

Slides:



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

Event Handling.
Managing Input Events in Swing Week 5 Workshop Lyn Bartram.
Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
What is Covered Everything up to and including Lecture 11 Types Recursion (including grammars) Lists and Trees GUIs Does not include Big-O notation (Lecture.
Events and the AWT The objectives of this chapter are: To understand the principles of the Java 1.1 event model To understand how the event model is used.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
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.
For IST410 Students only Events-1 Event Handling.
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.
Graphical User Interfaces
CS 112 GUI 06 May 2008 Bilkent. Java GUI API Containers: ◦ contain other GUI components. E.g, Window, Panel, Applet, Frame Dialog. Components: ◦ Buttons,
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
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.
28-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Event Handling – GUI Part II.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
Graphical User Interface Components: Part 1 Chapter 11.
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
– 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.
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.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
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.
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.
Object Oriented Programming.  Interface  Event Handling.
Review_6 AWT, Swing, ActionListener, and Graphics.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
CS Lecture 04 Mice Lynda Thomas
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.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
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.
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.
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.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
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.
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 in Java Hao Jiang Boston College April, 2009.
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
Chapter 14 Event-Driven Programming
Welcome To java
CompSci 230 S Programming Techniques
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
Chapter 12 Event-Driven Programming
Aum Amriteshwaryai Namah
Event Handling Chapter 2 Objectives
Computer Science 209 Graphics and GUIs.
Programming in Java Event Handling
GUI Event Handling Nithya Raman.
GUI Programming III: Events
CSE Software Engineering Fall 1999 Updated by J. Brown
Event-driven programming for GUI
Events, Event Handlers, and Threads
Presentation transcript:

GUI DYNAMICS Lecture 11 CS2110 – Fall 2009

GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders, menus,...  Containers: components that contain other components frames, panels, dialog boxes,...  Layout managers: control placement and sizing of components  Dynamics: user interactions  Events  button-press, mouse-click, key-press,...  Listeners: an object that responds to an event  Helper classes  Graphics, Color, Font, FontMetrics, Dimension,... 2

Dynamics Overview 3  Dynamics = causing and responding to actions  What actions? Called events: mouse clicks, mouse motion, dragging, keystrokes We would like to write code (a handler) that is invoked when an event occurs so that the program can respond appropriately In Java, you can intercept events by providing an object that “hears” the event – a listener  What objects do we need to know about?  Events  Event listeners

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Intro extends JFrame { private int count = 0; private JButton myButton = new JButton("Push Me!"); private JLabel label = new JLabel("Count: " + count); public Intro() { setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new FlowLayout(FlowLayout.LEFT)); //set layout manager add(myButton); //add components add(label); label.setPreferredSize(new Dimension(60, 10)); myButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { count++; label.setText("Count: " + count); } }); pack(); setVisible(true); } public static void main(String[] args) { new Intro(); } Brief Example Revisited 4

5 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Intro extends JFrame { private int count = 0; private JButton myButton = new JButton("Push Me!"); private JLabel label = new JLabel("Count: " + count); public Intro() { setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new FlowLayout(FlowLayout.LEFT)); //set layout manager add(myButton); //add components add(label); label.setPreferredSize(new Dimension(60, 10)); myButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { count++; label.setText("Count: " + count); } }); pack(); setVisible(true); } public static void main(String[] args) { new Intro(); }

The Java Event Model 6  Timeline  User or program does something to a component clicks on a button, resizes a window,...  Java issues an event object describing the event  A special type of object (a listener) “hears” the event The listener has a method that “handles” the event The handler does whatever the programmer programmed  What you need to understand  Events: How components issue events  Listeners: How to make an object that listens for events  Handlers: How to write a method that responds to an event

Events: How your application learns when something interesting happens  Basic idea: You register a listener and Java calls it  The argument is an “event”: a normal Java object  Events are normally created by the Java runtime system  You can create your own, but this is unusual  Normally events are associated with a component  Most events are in java.awt.event and javax.swing.event  All events are subclasses of AWTEvent  ActionEvent  AdjustmentEvent  ComponentEvent  ContainerEvent  FocusEvent  HierarchyEvent  InputEvent  InputMethodEvent  InvocationEvent  ItemEvent  KeyEvent  MouseEvent  MouseWheelEvent  PaintEvent  TextEvent  WindowEvent 7

Types of Events 8  Each Swing Component can generate one or more types of events  The type of event depends on the component Clicking a JButton creates an ActionEvent Clicking a JCheckbox creates an ItemEvent  The different kinds of events include different information about what has occurred All events have method getSource() which returns the object (e.g., the button or checkbox) on which the Event initially occurred An ItemEvent has a method getStateChange() that returns an integer indicating whether the item (e.g., the checkbox) was selected or deselected

Event Listeners 9  ActionListener, MouseListener, WindowListener,...  Listeners are Java interfaces  Any class that implements that interface can be used as a listener  To be a listener, a class must implement the interface  Example: an ActionListener must contain a method public void actionPerformed(ActionEvent e)

Implementing Listeners 10  Which class should be a listener?  Java has no restrictions on this, so any class that implements the listener will work  Typical choices  Top-level container that contains whole GUI public class GUI implements ActionListener  Inner classes to create specific listeners for reuse private class LabelMaker implements ActionListener  Anonymous classes created on the spot b.addActionListener(new ActionListener() {...});

Listeners and Listener Methods 11  When you implement an interface, you must implement all the interface’s methods  Interface ActionListener has one method: void actionPerformed(ActionEvent e)  Interface MouseListener has five methods: void mouseClicked(MouseEvent e) void mouseEntered(MouseEvent e) void mouseExited(MouseEvent e) void mousePressed(MouseEvent e) void mouseReleased(MouseEvent e)  Interface MouseMotionListener has two methods: void mouseDragged(MouseEvent e) void mouseMoved(MouseEvent e)

Registering Listeners 12  How does a component know which listener to use?  You must register the listeners  This connects listener objects with their source objects  Syntax: component.addTypeListener(Listener)  You can register as many listeners as you like  Example: b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { count++; label.setText(generateLabel()); } });

Example 1: The Frame is the Listener 13  import javax.swing.*; import java.awt.*; import java.awt.event.*;  public class ListenerExample1 extends JFrame implements ActionListener {  private int count;  private JButton b = new JButton("Push Me!");  private JLabel label = new JLabel("Count: " + count);  public static void main(String[] args) {  JFrame f = new ListenerExample1();  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  f.setSize(200,100);  f.setVisible(true);  }  public ListenerExample1() {  setLayout(new FlowLayout(FlowLayout.LEFT));  add(b); add(label);  b.addActionListener(this);  }  public void actionPerformed(ActionEvent e) {  count++;  label.setText("Count: " + count);  }

Example 2: The Listener is an Inner Class 14  import javax.swing.*; import java.awt.*; import java.awt.event.*;  public class ListenerExample2 extends JFrame {  private int count;  private JButton b = new JButton("Push Me!");  private JLabel label = new JLabel("Count: " + count);  class Helper implements ActionListener {  public void actionPerformed(ActionEvent e) {  count++;  label.setText("Count: " + count);  }  public static void main(String[] args) {  JFrame f = new ListenerExample2();  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  f.setSize(200,100); f.setVisible(true);  }  public ListenerExample2() {  setLayout(new FlowLayout(FlowLayout.LEFT));  add(b); add(label); b.addActionListener(new Helper());  }

Example 3: The Listener is an Anonymous Class 15  import javax.swing.*; import java.awt.*; import java.awt.event.*;  public class ListenerExample3 extends JFrame {  private int count;  private JButton b = new JButton("Push Me!");  private JLabel label = new JLabel("Count: " + count);  public static void main (String[] args) {  JFrame f = new ListenerExample3();  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  f.setSize(200,100); f.setVisible(true);  }  public ListenerExample3() {  setLayout(new FlowLayout(FlowLayout.LEFT));  add(b); add(label);  b.addActionListener(new ActionListener() {  public void actionPerformed (ActionEvent e) {  count++;  label.setText("Count: " + count);  }  });  }

Adapters 16  Some listeners (e.g., MouseListener ) have lots of methods; you don’t always need all of them  For instance, you may be interested only in mouse clicks  For this situation, Java provides adapters  An adapter is a predefined class that implements all the methods of the corresponding Listener Example: MouseAdapter is a class that implements all the methods of interfaces MouseListener and MouseMotionListener  The adapter methods do nothing  To easily create your own listener, you extend the adapter class, overriding just the methods that you actually need

Using Adapters 17  import javax.swing.*; import javax.swing.event.*;  import java.awt.*; import java.awt.event.*;  public class AdapterExample extends JFrame {  private int count; private JButton b = new JButton("Mouse Me!");  private JLabel label = new JLabel("Count: " + count);  class Helper extends MouseAdapter {  public void mouseEntered(MouseEvent e) {  count++;  label.setText("Count: " + count);  }  public static void main(String[] args) {  JFrame f = new AdapterExample();  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  f.setSize(200,100); f.setVisible(true);  }  public AdapterExample() {  setLayout(new FlowLayout(FlowLayout.LEFT));  add(b); add(label); b.addMouseListener(new Helper());  }

Notes on Events and Listeners 18  A single component can have many listeners  Multiple components can share the same listener  Can use event.getSource() to identify the component that generated the event  For more information on designing listeners, see / uiswing/events/  For more information on designing GUIs, see /uiswing/

GUI Drawing and Painting 19  For a drawing area, extend JPanel and override the method public void paintComponent(Graphics g)  paintComponent contains the code to completely draw everything in your drawing panel  Do not call paintComponent directly – instead, request that the system redraw the panel at the next convenient opportunity by calling myPanel.repaint()  repaint() requests a call paintComponent() “soon”  repaint(ms) requests a call within ms milliseconds Avoids unnecessary repainting 16ms is a reasonable value

Java Graphics 20  The Graphics class has methods for colors, fonts, and various shapes and lines  setColor(Color c)  drawOval(int x, int y, int width, int height)  fillOval(int x, int y, int width, int height)  drawLine(int x1, int y1, int x2, int y2)  drawString(String str, int x, int y)  Take a look at  java.awt.Graphics (for basic graphics)  java.awt.Graphics2D (for more sophisticated control)  The 2D Graphics Trail:  examples on the web site