Inner Classes in Java CMSC 432 Shon Vick. 2 Conceptual Structure package p 1 class A 1 class A 2... class A n... package n class A 1 class A 2... class.

Slides:



Advertisements
Similar presentations
Event Handling.
Advertisements

Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the.
OOP in Java – Inner Classes Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Mouse Events and Keyboard Events
Java & Inner Classes L. Grewe. Kinds of of Classes Top level classes Top level classes Declared inside packageDeclared inside package Visible throughout.
Hoofdstuk 13 Object-georiënteerd ontwerp. Methode-aanroep: Type van parameters void paint(Graphics g) { g.drawRect( ); class Graphics { void drawRect(int.
Graphics Programming with Inheritance Template pattern (Chap 6, se SceneEditor)
OOP Java1 Event Handling Overview Listeners, Adapters and Event Sources Inner classes Event Handling Details Applets and GUI Applications Event.
1 Gui Programming (Part I) Graphical User Interfaces (Part I) l Introduction to events. l A Brief history. l Event sources and listeners. l The delegation.
OOP in Java – Inner Classes Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Intermediate Java1 An example that uses inner classes Week Four Continued.
GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.
OOP in Java – Inner Classes Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
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.
9/21/99www.cs.vt.edu/wwtut/1 Introduction To Abstract Windowing Toolkit (AWT) Part 3 – AWT Marc Abrams Virginia Tech CS Dept courses.cs.vt.edu/wwwtut/
CS2110 Recitation 07. Interfaces Iterator and Iterable. Nested, Inner, and static classes We work often with a class C (say) that implements a bag: unordered.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
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.
(c) University of Washington07b-1 CSC 143 Java Events, Event Handlers, and Threads Reading: Ch. 17.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans.
The Drawing program – Java Applets
Copyright © 2002, Systems and Computer Engineering, Carleton University b-Gui2.ppt * Object-Oriented Software Development Part 18-b Building.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Graphics in Java CS 21b. The paint() Method Method in a visual component that specifies what that component looks like Particularly important for applets,
Chapter 7: Pinball Game Construction Kit. Vectors Example of a “collection class” Must “import java.util.Vector” More flexible than arrays: will grow.
Programming for Interactivity Professor Bill Tomlinson Tuesday & Wednesday 6:00-7:50pm Fall 2005.
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.
Understand the difference between applets and applications Identify meaningful applet resources for academic subjects Create and demonstrate a basic Java.
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.
© Marty Hall, Larry Brown Web core programming 1 Handling Mouse and Keyboard Events.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
An Advanced Code Pattern: Inner Classes CSE301 University of Sunderland Harry R. Erwin, PhD Half Lecture.
CS Lecture 04 Mice Lynda Thomas
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
Laboratory Study November, Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.
Interfaces Describe what classes should do, without specifying how they should do it Not a class, but a set of requirements for classes that want to conform.
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.
Event Listeners ActionListener –button,list AdjustmentListener-scroll bar ComponentListener-when component hidden…. ContainerListener-comp added or removed.
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.
CSI 3125, Preliminaries, page 1 Adapter Class. CSI 3125, Preliminaries, page 2 Adapter Class Java provides a special feature, called an adapter class,
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Event Handling CS 21a: Introduction to Computing I First Semester,
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
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.
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.
Events and Event Handling
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Java Programming Language (6)
Computer Science 209 Graphics and GUIs.
GUI III IS
Programming in Java Event Handling
GUI Event Handling Nithya Raman.
GUI Programming III: Events
CSE Software Engineering Fall 1999 Updated by J. Brown
ITEC324 Principle of CS III
Printing in Java Printing in Java 1.1 is implemented through the new PrintJob class and PrintGraphics interface When a PrintJob object is created, the.
Events, Event Handlers, and Threads
ITEC324 Principle of CS III
Chapter 6 Inheritance.
Presentation transcript:

Inner Classes in Java CMSC 432 Shon Vick

2 Conceptual Structure package p 1 class A 1 class A 2... class A n... package n class A 1 class A 2... class A n... package n without inner classes package p1 class A 1 class A 2 class A 2_1 class A 2_2 class A 2_1 class A 2_ inner classes

3Example public x + z; } class OC { // outer class private int x = 2; public class IC { // inner class int z = 4; public int getSum() { return }

4Why?   Logical grouping of functionality   Data hiding – Yet another encapsulation mechanism   Linkage to outer class – We’ll see about this later

5 Simple Motivating Example public class MyList { private Object [ ] a; private int size; } Say we want to iterate over the elements of this guy …

6 1 st Design public class MyIterator { private MyList list; private int pos; MyIterator(MyList list) { this.list = list; pos = 0; } public boolean hasNext() { return (pos < list.size); } public Object next() { return list.a[pos++]; } }  Problems – Need to maintain reference to MyList – Need to access private data in MyList

7 2 nd Try – Using Inner Classes public class MyList { private Object [ ] a; private int size; public class MyIterator { private int pos; MyIterator() { pos = 0; } public boolean hasNext() { return (pos < size); } public Object next() { return a[pos++]; }

8 Inner Classes Example public class OC { // outer class private int x = 2; public class IC { // inner class int z = 4; public int getSum() { return x + z; }

9 Mechanics and Syntax   Class referencing syntax – OuterClass.InnerClass   Example OC oc = new OC( ); OC.IC ic; name of inner class ic = new OC.IC() doesn’t work! ic = oc.new IC(); instantiates inner class - ic now will "know about" oc, but not vice versa ic.getSum() yields 6 can access private x in oc!

10 Accessing Outer Scope public class OC { outer class int x = 2; public class IC { inner class int x = 6; public void getX() { inner class method int x = 8; System.out.println( x ); prints 8 System.out.println( this.x ); prints 6 System.out.println( OC.this.x ); prints 2 }

11 Instantiating Inner Class  Common Technique  Outer class method returns instance of inner class  Used by Java Collections Library for Iterators  Code Example public class MyList { public class IC implements Iterator { … } public Iterator iterator() { return new IC(); // creates instance of IC } MyList m = new MyList(); Iterator it = m.iterator();

12Listeners  Each event is represented by an object that gives information about the event and identifies the event source.  Event sources are typically components.  Each event source can have multiple listeners.Conversely, a single listener can register with multiple event sources. Event Source

13 Making the Applet the Listener import java.applet.*; import java.awt.*; import java.awt.event.*; public class Scribble extends Applet implements MouseListener, MouseMotionListener { private int last_x, last_y; public void init() { this.addMouseListener(this); this.addMouseMotionListener(this); } …

14 Mouse Events public void mousePressed(MouseEvent e) { last_x = e.getX(); last_y = e.getY(); } public void mouseDragged(MouseEvent e) { Graphics g = this.getGraphics(); int x = e.getX(), y = e.getY(); g.drawLine(last_x, last_y, x, y); last_x = x; last_y = y; }

15 Need to implement the whole Interface // The other, unused methods of the MouseListener interface. public void mouseReleased(MouseEvent e) {;} public void mouseClicked(MouseEvent e) {;} public void mouseEntered(MouseEvent e) {;} public void mouseExited(MouseEvent e) {;} // The other method of the MouseMotionListener interface. public void mouseMoved(MouseEvent e) {;} }

16 Using Adapters and Inner Classes to Handle Events  The empty method bodies of the MouseListener interface from the previous example make the code harder to read and maintain – Adapter classes are a means around this problem – An adaptor allows you to override just the methods for the events of interest

17 Adapters and Anonymous Classes public void init() { // Define, instantiate, and register a MouseListener object. this.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { last_x = e.getX(); last_y = e.getY(); More Info on Innerclasses } } );

18 References  05/cmsc132/lecs/lec27.pdf 05/cmsc132/lecs/lec27.pdf 05/cmsc132/lecs/lec27.pdf  al/java/javaOO/nested.html al/java/javaOO/nested.html al/java/javaOO/nested.html