Lec 06 Agenda 1/ the 8 rules of Java 2/ Casting primitives 3/ Swing and Threading 4/ Java Event Model. Event api and support for Swing 5/ Swing labs, live.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES Lecture: Interfaces Slides adapted from Prof. Steven Roehrig.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
Object-Oriented Analysis and Design
Ch 13. Features Found Only in Java Timothy Budd Oregon State University.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
Events in Java Swing Chris North cs3724: HCI. Typical command line program Non-interactive Linear execution program: main() { code; }
CSCI-383 Object-Oriented Programming & Design Lecture 15.
Copyright © 2003 ProsoftTraining. All rights reserved. Sun Certified Java Programmer Exam Preparation Guide.
Programming Languages and Paradigms Object-Oriented Programming.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Inner and Anonymous Classes Though you are captive in the OO paradigm, you can use inner classes and anonymous classes to get around this constraint and.
DUE Hello World on the Android Platform.
"If I can't picture it, I can't understand it.". Reference anonymous - gravity Concrete classes: rules of.
Exceptions: Checked versus Unchecked Exceptions.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Piazza Piazza: 037/home Course website:
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
1 Interfaces Reading for today: Sec and corresponding ProgramLive material. Also, compare with previous discussions of abstract classes (Sec. 4.7).
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
OOP (Java): GUI Intro/ OOP Objectives – –use an image viewer application to introduce Java's GUI features Semester 2,
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
GUIs Graphical User Interfaces. Everything coming together Known: – Inheritance – Interfaces – Abstract classes – Polymorphism – Exceptions New: – Events.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Video Games list lab 6  At the end of this lab you will be expected to know:  What Views, View Groups, Layouts, and Widgets are and how they relate to.
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
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,
Agenda Lec05 Exceptions API Reference- and Type-Anonymity Swing Review.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Object Oriented Programming in Java Habib Rostami Lecture 10.
Object-Oriented Programming: Polymorphism Chapter 10.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Events and Event Handling
Sections Inheritance and Abstract Classes
Agenda Lec05 Exceptions API
Lec 06 Agenda 1/ the 6 rules of Java
Java Programming Language
EE 422C Java FX.
Abstract Classes Page
Quizz Results hw1 quizz mean std-dev
Events, Event Handlers, and Threads
SE-1021 Software Engineering II
Chapter 15 Event-Driven Programming and Animations Part 1
Elipses int... passed into method as int[]
Inner Classes.
Chapter 6 Inheritance.
Presentation transcript:

Lec 06 Agenda 1/ the 8 rules of Java 2/ Casting primitives 3/ Swing and Threading 4/ Java Event Model. Event api and support for Swing 5/ Swing labs, live templates, form designer

Java entityMay be a reference?May be an instantiated? Concrete ClassYES Abstract ClassYESNO InterfaceYESNO

Fight Club Rules of Java 1/ Only concrete classes can be instantiated.

Fight Club Rules of Java 1/ Only concrete classes can be instantiated. 2/ Only concrete classes can be instantiated.

Fight Club Rules of Java 1/ Only concrete classes can be instantiated. 2/ Only concrete classes can be instantiated. 3/ The type of any Object must be of type Concrete_class. (corollary to 1 and 2) 4/ References may be concrete, abstract, or interface.

Fight Club Rules of Java 5/ You may create type-anonymous abstract- classes and interfaces by overriding ALL their contract methods when you declare them. 6/ The “type” of a type-anonymous class is $x aka Robert Paulson.

Swing – and Threading

Java Event Model Used to program behavior in GUIs Used extensively in Android.

If a tree falls in a forest and no one is around to hear it, does it make a sound?

Event (onClick) No Event-Listener listening No Catcher Event-Source (Button) No Event Listener

Event (onClick) Event-Listener listening Catcher ready to catch Event-Source (Button) ActionList ener Any Object

Wrong Event

Event source not registered

Event (Action) Event-Listener listening Catcher ready to catch Action- Listener Event-Source (Button) Any Object OnMouse- Listener Event (Mouse)

Step 1/ define the event-listener object and override the appropriate methods ActionListener mActionListener = new ActionListener() public void actionPerformed(ActionEvent e) { //behavior here }}; Step 2/ register (add) the event-listener to the event source. mButton.addActionListener(mActionListener);

Casting Casting down (opening the aperture) is potentially very dangerous and could produce a class-cast-exception. You must be a good free-mason. Casting up (closing the aperture) is called automatic promotion and an explicit cast is not required because a subclass object may always be stored in a superclass reference (or an interface that it implements).

Inner and Anonymous Classes In Java7, functions are second class citizens. You can only pass objects around. Though you are captive in the OO paradigm, you can use inner classes and anonymous classes to get around this constraint and write lambda-like code. Lamdas in Java8 replace anonymous inner classes. Often times, no one but the enclosing class cares about an object. In this case, you may consider using inner or anonymous classes.

Write a very simple application for a contact manager. The the sake of simplicity, each contact will have a name and a phone number only. The user should be able to create new contacts and diplay all contacts. Create a Latin dictionary with entries for Latin and English equivalents. Store them in a list and allow the user to delete them.

Interfaces A class implements an interface rather than extends it. Any class that implements the interface must override all the interface methods with it's own methods. Interface names often end with "able" to imply that they add to the capabilty of the class. An interface is a contract; it defines the methods

The ColorSelector GUI App

The Leet Translator GUI App

samples/Ensemble/index.html#SAMPLES JavaFX From Firefox

"If I can't picture it, I can't understand it."

Reference anonymous - gravity Concrete classes: rules of fight club Type anonymous : Fight Club - Robert Paulson Dubugger: x-men quicksilver

References Objects are like astronauts.

References Object objDate = new Date(); This date has a reference. The reference is an Object.

Reference Anonymous MyTime myTime = new MyTime(new Date()); The date is reference-anonymous, but we can still get a reference to it myTime.getDate(); new Date(); The date is reference-anonymous and we have no reference to it – it is space-junk and will be garbage collected.

Reference Anonymous Date dat1 = new Date(); Date dat2 = new Date(); dat1 = dat2; The Object originally stored in dat1 is orphaned and becomes space junk.

Reflection Very useful to inspect the underlying object type. Very useful when first learning an OO language.

Reflection Reflection allows you to inspect the type (class) of the implicit parameter at runtime. We will use reflection to gain a deeper understanding of polymorphism and the java event model. Every class has a class object which you can access like so: java.util.Date.class, or like so: Class.forName(strFullyQualifiedClass); See reflection example

best video on Form Designer in Intellij (in German, genießen) (ok video, no sound, English subtitles) How to use the Form Designer in IntelliJ

Reflection Name of Driverimplemnts Implicit param EventListener typeDefined TimeTestOuter ActionListener yesEventListenerOuter In separate java file TimeTestInner ActionListener yesEventListenerInnerIn same java file TimeTestLocal ActionListener yesanonymousSame method TimeTestAnon ActionListener noanonymousinline See inner example

Layouts in Swing Layouts: /layout/border.html ng/layout/flow.html make-bottom-panel-in-borderlayout-to- expand-like-center-panel