35
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

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

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: 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

Lec 06 Agenda

1/ the 8 rules of Java2/ Casting primitives 3/ Swing and Threading4/ Java Event Model. Event api and support for

Swing5/ Swing labs, live templates, form designer

Page 2: 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

Java entity May be a reference? May be an instantiated?

Concrete Class YES YES

Abstract Class YES NO

Interface YES NO

https://www.youtube.com/watch?v=MPITE_Ine-c

Page 3: 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

Fight Club Rules of Java

1/ Only concrete classes can be instantiated.

Page 4: 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

Fight Club Rules of Java

1/ Only concrete classes can be instantiated. 2/ Only concrete classes can be instantiated.

Page 5: 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

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.

Page 6: 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

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.

Page 7: 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

Swing – and Threading

https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html

Page 8: 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

Java Event Model

• Used to program behavior in GUIs• Used extensively in Android.

Page 9: 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

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

Page 10: 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
Page 11: 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

Event(onClick)

No Event-Listener listening

No Catcher

Event-Source(Button)

No Event Listener

Page 12: 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

Event(onClick)

Event-Listener listening

Catcher ready to catch

Event-Source(Button)

ActionListener

Any Object

Page 13: 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

Wrong Event

Page 14: 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

Event source not registered

Page 15: 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

Event(Action)

Event-Listener listening

Catcher ready to catch

Action-Listener

Event-Source(Button)

Any Object

OnMouse-Listener

Event(Mouse)

Page 16: 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

Step 1/ define the event-listener object and override the appropriate methods

ActionListener mActionListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) {

//behavior here

}};

Step 2/ register (add) the event-listener to the event source.

mButton.addActionListener(mActionListener);

Page 17: 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

http://docs.oracle.com/javase/tutorial/uiswing/events/api.htmlhttp://docs.oracle.com/javase/tutorial/uiswing/events/eventsandcomponents.html

Page 18: 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

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).

Page 19: 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

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.

Page 20: 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

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.

Page 21: 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

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

Page 22: 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

The ColorSelector GUI App

Page 23: 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

The Leet Translator GUI App

Page 24: 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

http://download.oracle.com/otndocs/products/javafx/2/samples/Ensemble/index.html#SAMPLES

JavaFX From Firefox

Page 25: 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

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

Page 26: 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

Reference anonymous - gravity https://www.youtube.com/watch?v=cEkILY1h6fA

Concrete classes: rules of fight clubhttps://www.youtube.com/watch?v=vJMC_S-DB2I

Type anonymous : Fight Club - Robert Paulsonhttps://www.youtube.com/watch?v=GCi_PIz5ekU

Dubugger: x-men quicksilver https://www.youtube.com/watch?v=WGDXO9mlprM

Page 27: 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

References

Objects are like astronauts.

Page 28: 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

References

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

an Object.

Page 29: 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

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.

Page 30: 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

Reference Anonymous

Date dat1 = new Date(); Date dat2 = new Date(); dat1 = dat2;

The Object originally stored in dat1 is orphaned and becomes space junk.

Page 31: 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

Reflection

• Very useful to inspect the underlying object type.

• Very useful when first learning an OO language.

Page 32: 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

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

Page 33: 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

best video on Form Designer in Intellij (in German, genießen)https://www.youtube.com/watch?v=0I_1HYMUQrg

(ok video, no sound, English subtitles)https://www.youtube.com/watch?v=G4jMzEGMKfg

How to use the Form Designer in IntelliJ

Page 34: 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

Reflection

Name of Driver implemnts Implicit param

EventListener type Defined

TimeTestOuterActionListener

yes EventListenerOuter In separate java file

TimeTestInner ActionListener

yes EventListenerInner In same java file

TimeTestLocalActionListener

yes anonymous Same method

TimeTestAnonActionListener

no anonymous inline

See inner example

Page 35: 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

Layouts in Swing

• Layouts:https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html

https://docs.oracle.com/javase/tutorial/uiswing/layout/flow.html

http://stackoverflow.com/questions/17083657/make-bottom-panel-in-borderlayout-to-expand-like-center-panel