CS11 Java. Fall Lecture 3

Similar documents
CS11 Java. Fall Lecture 4

Introduction to the JAVA UI classes Advanced HCI IAT351

Lecture 5: Java Graphics

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

PIC 20A GUI with swing

Lecture 3: Java Graphics & Events

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

Java Programming Lecture 6

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

2. (True/False) All methods in an interface must be declared public.

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

CS 11 java track: lecture 3

CSE 331. Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Packages: Putting Classes Together

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Starting Out with Java: From Control Structures Through Objects Sixth Edition

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Graphical User Interfaces (GUIs)

CS 251 Intermediate Programming GUIs: Components and Layout

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

GUI Components: Part 1

CS 2113 Software Engineering

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Java: Graphical User Interfaces (GUI)

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

BASICS OF GRAPHICAL APPS

GUI in Java TalentHome Solutions

Inheritance. One class inherits from another if it describes a specialized subset of objects Terminology:

Window Interfaces Using Swing Objects

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

Systems Programming Graphical User Interfaces

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7...

Lecture 19 GUI Events

Window Interfaces Using Swing Objects

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Jonathan Aldrich Charlie Garrod

CSE 331 Software Design & Implementation

Graphical interfaces & event-driven programming

CS Exam 1 Review Suggestions

Java. GUI building with the AWT

GUI Event Handlers (Part I)

CS506 Web Programming and Development Solved Subjective Questions With Reference For Final Term Lecture No 1

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Educational Fusion. Implementing a Production Quality User Interface With JFC

Basicsof. JavaGUI and SWING

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

What is Widget Layout? Laying Out Components. Resizing a Window. Hierarchical Widget Layout. Interior Design for GUIs

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

Java Programming Lecture 7

Graphical User Interfaces. Comp 152

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

Laying Out Components. What is Widget Layout?

Come & Join Us at VUSTUDENTS.net

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Overloading Example. Overloading. When to Overload. Overloading Example (cont'd) (class Point continued.)

Casting -Allows a narrowing assignment by asking the Java compiler to "trust us"

COMPSCI 230. Software Design and Construction. Swing

Graphic User Interfaces. - GUI concepts - Swing - AWT

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

SD Module-1 Advanced JAVA

Final Exam CS 251, Intermediate Programming December 13, 2017

The AWT Event Model 9

SD Module-1 Advanced JAVA. Assignment No. 4

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Sri Vidya College of Engineering & Technology

Programming graphics

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

Today. cisc3120-fall2012-parsons-lectiii.3 2

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Unit 7: Event driven programming

SELF-STUDY. Glossary

Object Oriented Design & Patterns. Part 1

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

Event Driven Programming

Lab & Assignment 1. Lecture 3: ArrayList & Standard Java Graphics. Random Number Generator. Read Lab & Assignment Before Lab Wednesday!

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Lecture 9. Lecture

CS 251 Intermediate Programming GUIs: Event Listeners

HW#1: Pencil Me In Status!? How was Homework #1? Reminder: Handouts. Homework #2: Java Draw Demo. 3 Handout for today! Lecture-Homework mapping.

UI Software Organization

Introduction This assignment will ask that you write a simple graphical user interface (GUI).

Eclipsing Your IDE. Figure 1 The first Eclipse screen.

Review sheet for Final Exam (List of objectives for this course)

Transcription:

CS11 Java Fall 2014-2015 Lecture 3

Today s Topics! Class inheritance! Abstract classes! Polymorphism! Introduction to Swing API and event-handling! Nested and inner classes

Class Inheritance! A third of the four big OOP concepts! A class can extend another class to build on its functionality! Terminology: " Parent class, or superclass, or base class " Child class, or subclass, or derived class! Child classes inherit all methods and fields within parent class " Can add new functionality " Can also override parent-class methods

Class Inheritance (2)! Class inheritance models an is-a relationship " Example class hierarchy: Vehicle Wheeled Vehicle Water Vehicle Dump Truck Sailboat Barge! The child class is a specialization of the parent class! Child class also has characteristics of parent class " Can treat child class as if it were any parent-type! A dump truck is a wheeled vehicle.! A sailboat is a vehicle.! A water vehicle is a vehicle.

Class Inheritance (3)! Example class hierarchy: Vehicle Wheeled Vehicle Water Vehicle Dump Truck Sailboat Barge! Sibling types do not model an is-a relationship! " These statements are clearly false:! A dump truck is a water vehicle.! A wheeled vehicle is a barge.! What about these statements?! A vehicle is a dump truck.! A water vehicle is a sailboat. " Depends on the actual vehicle being considered!! Need to examine a specific vehicle to verify the statement

Example Class Hierarchy! The number classes in Java java.lang.object java.lang.number java.lang.integer " Integer is a Number, is an Object " Integer extends Number, which extends Object " Integer inherits all methods that Object defines! boolean equals(object o)! int hashcode()! String tostring()! Class getclass() " Integer also overrides some of these methods

Overriding Object.toString()! Really useful idea, especially for debugging! Used in string concatenation " You type this: String msg = "Point is " + pt; " Compiler automatically does this: String msg = "Point is " + pt.tostring();! Simple to define: @Override public String tostring() { return "(" + xcoord + "," + ycoord + ")";

Classes and Objects! A class parent-class methods can be called without any special syntax. Integer intobj = new Integer(53);... Class c = intobj.getclass(); // Get type info " Integer is also an Object can call methods declared and/or implemented on Object! Child class can also provide its own methods System.out.println("Value is " + intobj.intvalue()); " Integer extends Object s functionality! intvalue() returns an int version of the Integer

Reference Types! Every reference has a class-type associated with it Object obj; // A reference of type Object Integer val; // A reference of type Integer! The variable s type dictates what is accessible! Example: Object obj = new Integer(38);... System.out.println(obj.intValue()); COMPILE ERROR " Compile error, because Object doesn t define intvalue() " intvalue() is declared in Number class (parent of Integer) " Even though obj refers to an Integer object, only the Object methods are visible

Navigating the Hierarchy Object Number! Number hierarchy is like this: Integer Float Double! Moving down the hierarchy requires a run-time test. Object obj = new Integer(453);... int i = ((Integer) obj).intvalue(); // Cast obj " You could also try this: float f = ((Float) obj).floatvalue(); // Runtime error! This code compiles, but it will report an error at runtime " Java can t assume the actual object-type at compile time!! (Even when it s obvious to a human ) " So, we have a runtime type-check, and a potential error.

What Child Classes Don t Get! Child classes cannot access private members in parent classes! protected access-modifier allows the child class to access parent-class members " Only available within the class, and to subclasses " Looser than private, but still not public!! Child classes also don t inherit static fields and methods " They can be accessed, but they are not inherited

A Generic Task Class public class Task { private String name; private boolean done; public Task(String taskname) { name = taskname; done = false; /** Just record that the task is done. */ public void dotask() { done = true; /** Report if the task is done or not. */ public boolean isdone() { return done;

Making Useful Tasks! Our Task class is very generic " so generic that it s nearly useless!! Extend Task class to provide useful tasks public class FileUploadTask extends Task { public FileUploadTask() { // Call parent-class constructor super("upload file");... " Parent-class constructors are not inherited! " If parent class doesn t have a default constructor, we must explicitly call one in the child class, using super keyword

Overriding Parent-Class Methods! FileUploadTask should provide its own implementation of dotask() public class FileUploadTask extends Task {... /** Perform the file-upload operation. */ @Override public void dotask() {... // Open a connection, read a file, etc. " Method s signature is same as parent-class method signature " This overrides Task s implementation of dotask()

Polymorphism! Now we want to upload a file: Task t = new FileUploadTask(); t.dotask(); " Which implementation of dotask() does this call?! In Java, all instance-methods are virtual " Even though t is a Task reference, the FileUploadTask implementation is called " Reason: t refers to an object of type FileUploadTask! This is called polymorphism " The fourth Big OOP Concept " A statement s behavior changes, depending on the type of the objects involved

Calling Parent-Class Methods! Problem: " FileUploadTask.doTask() doesn t set done to true " Also, done is private!! One solution: " FileUploadTask.doTask() implementation can call the parent-class implementation: /** Perform the file-upload operation. */ @Override public void dotask() {... // Open a connection, read file, etc. // All done! super.dotask();

The Task Abstraction! Actually doesn t make much sense for Task to have an implementation of dotask() " Change Task to be an abstract class " An abstract class declares a set of behaviors, but only partially defines it.! Abstract classes cannot be instantiated " Child classes must be provided, that implement the missing functionality " Example: FileUploadTask must provide an implementation of dotask(), that uploads a file.

The New, Abstract Task Class! Our abstract Task class: // A class that represents a generic task public abstract class Task { private String name; private boolean done; public Task(String taskname) { name = taskname; done = false; // Child classes implement this method. public abstract void dotask();... // Rest of class! Abstract classes can still have fields and non-abstract methods

The New FileUploadTask! FileUploadTask doesn t override dotask() " There s nothing to override! " FileUploadTask implements dotask()! Again, the signatures must match up /** Implement dotask() to upload a file. */ public void dotask() {... // Open a connection, read the file, etc. " (Without the abstract modifier, of course!) " Of course, we can t do super.dotask() anymore! Child class must provide an implementation of every abstract parent-class method " If not, child class must also be declared abstract.

Completing the Abstraction! How can a task be marked as done?! A simple solution: set done to be protected! Another good solution: " Task can provide another protected method to do this: protected void reporttaskdone() { if (done) {... // Task was already done! Complain. done = true; " Now only child classes can report that the task is done! Which solution is more extensible? " Might want to add other processing when a task is finished " Can easily add this to reporttaskdone() later

Task References! You can t instantiate the abstract Task class Task t = new Task("send e-mail"); " The implementation of Task is incomplete!! You can have a Task-reference COMPILE ERROR Task t = new FileUploadTask(); t.dotask(); // Calls FileUploadTask.doTask() t = new SendEMailTask(); t.dotask(); // Calls SendEMailTask.doTask() " The correct implementation of dotask() gets called because of polymorphism! APIs are made generic by using the base-class type void enqueuetask(task t) { pendinglist.store(t);

Swing: A Quick Tour! First GUI framework in Java was the AWT " Abstract Windowing Toolkit " Could perform basic operations " Not very pretty, or extensible! Java 1.2 introduced the Swing API " Built on top of some AWT functionality " Reimplemented many higher-level AWT classes " Customizable look-and-feel " Very extensible, feature-rich API " A bit slower than AWT, since it s Pure Java

Swing Classes! Most Swing classes are in javax.swing package (and some sub-packages)! Quite a few AWT classes are used by Swing! " Events, event-handlers, geometry, images, drag-and-drop, etc.! Swing UI widgets derive from JComponent " Represents any UI component in Swing " JComponent derives from java.awt.container " Custom Swing components can also use JComponent as their parent class

Heavyweight Components! AWT UI components are heavyweight " Each component has its own native graphics resources " Components don t use pure Java code to draw their graphics! Actually use operating-system calls " Overlapping components overwrite each other

Lightweight Components! Swing UI components are lightweight " Components use only Java to draw themselves " Native graphics resources are shared by Swing components, as much as possible " Example:! A popup menu fully within an app s window is drawn using that window s resources! A popup menu extending outside an app s window will get its own window " Swing can provide transparent regions more easily, since components share graphics resources

Mixing AWT and Swing! Lightweight and heavyweight components don t mix well! " Heavyweight components are always drawn on top of lightweight components.! Avoid mixing Swing UI components and AWT components if possible

Windows and Containers! JWindow represents simple windows " but no title bar, menus, min/max/close buttons!! JFrame represents application windows " Complete with title bar, menus, window-buttons " Typically use this for Java GUI applications! JPanel groups together UI components " A lightweight, general purpose container " Great for building up structure in your GUI!! Use add(...) method to add child-components " Child-components can also be containers, e.g. JPanel

Laying Out Components! Containers position/size child-components with layout managers " Call setlayout(layoutmanager lm) on the container " java.awt.layoutmanager is an interface! Many different layout managers " FlowLayout arranges components line-by-line; wraps to next line when current line is full " BoxLayout arranges components in a single row or column " BorderLayout can place a component in one of five regions: NORTH, SOUTH, EAST, WEST, and CENTER " GridLayout arranges components in a fixed-size 2D grid " GridBagLayout very sophisticated layout manager " And several more! (See implementers of LayoutManager ) # Default layout manager is FlowLayout

Events and Listeners! When something happens, UI widgets fire events " User clicks mouse on something " User presses some keys " Window is closed or minimized " User moves or drags mouse " etc.! To catch events, must implement event-listeners in your program " Listeners are exposed as interfaces to implement " Contained in java.awt.event package " Typically named [Something]Listener

ActionListener Interface! Example: java.awt.event.actionlistener " One method to implement: void actionperformed(actionevent e) " ActionEvent contains details of what happened! What UI component reported the event! When the event occurred! Any modifier keys (Ctrl, Alt, Shift, etc.)! Other things too! (See API docs ) " ActionEvent is reported by most Swing components

Implementing ActionListener! Swing components provide a registration method: addactionlistener(actionlistener l)! Implement ActionListener: public class ActionHandler implements ActionListener {... public void actionperformed(actionevent e) {... // Do something clever.! Register your listener: ActionHandler handler = new ActionHandler(); JButton button = new JButton("Start"); button.addactionlistener(handler);

Other AWT/Swing Listener Interfaces! MouseListener mouse enter/exit/click events! MouseMotionListener mouse move/drag events! KeyListener keyboard press/release events! FocusListener component gets/loses focus! ComponentListener component shown, hidden, resized! WindowListener window opened, closed, maximized, minimized

Listeners and Adapters! Some listeners are more complicated: " MouseListener interface specifies these methods:! mouseentered(), mouseexited()! mousepressed(), mousereleased()! mouseclicked()! Frequently only want to implement one or two of these! Java often provides adapters for event-listener interfaces! Example: java.awt.event.mouseadapter " Implements MouseListener interface, among others " All provided implementations are no-ops " Derive your event-handler from MouseAdapter, and then override just the methods you want to implement

Nested Classes in Java! Can declare a class within a class " Called a nested class class Outer { /* A nested class */ class Inner {... " When Outer.java is compiled, compiler generates two files: Outer.class and Outer$Inner.class

Nested Classes in Java (2)! The nested class is a member of the outer class, and can have an access modifier " e.g. a private nested class cannot be referred to directly from outside the outer class! The nested class can also be declared with or without the static keyword " Has some dramatic impacts on how the nested class can be used, and what it can do! class Outer { static class StaticNested {... class NonStaticNested {...

Static Nested Classes! Static nested classes are simply related classes contained within the outer class! Example: java.awt.geom.rectangle2d " An abstract class that represents 2D rectangles! Contains two static nested classes: " Rectangle2D.Double derives from Rectangle2D, and specifies coordinates of type double " Rectangle2D.Float is similar, but float coords! To use: " import java.awt.geom.rectangle2d; " Refer to nested classes by Rectangle2D.Float or Rectangle2D.Double

Non-static Nested Classes! Non-static nested classes are also called inner classes! Like instance methods, inner classes must be used in the context of a containing object! " They actually reference their containing object " They can directly access the containing object s fields and methods! Cannot create inner-class objects in a static method on the outer class! " Can only create in instance methods

Inner Classes and Event Listeners! Inner classes are great for event-listeners! " Listeners often need to access application state " Inner class can even access private members of the outer class! Also keeps outer class public interface clean " Don t want to have a whole bunch of public listener interface-methods exposed on outer class! When necessary, can also create multiple inner-class objects associated with a single outer-class object

Event Handler, Inner Class Style public class MyApp { /** Current state of application. **/ private boolean started; /** Handler for ActionEvents. **/ private class ActionHandler implements ActionListener { public void actionperformed(actionevent e) { started = true;... void initui() { // Create button, then use inner class to handle events JButton button = new JButton("Start"); button.addactionlistener(new ActionHandler());