16
Birds perspective on Java GUI Programming Introduction to the major concepts

Birds perspective on Java GUI Programming

  • Upload
    melora

  • View
    26

  • Download
    3

Embed Size (px)

DESCRIPTION

Birds perspective on Java GUI Programming. Introduction to the major concepts. Two GUI class-libraries. AWT (Abstract Windowing Toolkit) Came first – has ”Look & Feel” as platform (Windows, Linux, Apple...) Swing Newer – common L&F across platforms L&F Programmable - PowerPoint PPT Presentation

Citation preview

Page 1: Birds perspective on  Java GUI Programming

Birds perspective on Java GUI Programming

Introduction to the major concepts

Page 2: Birds perspective on  Java GUI Programming

kelk & sh 2004 02312 Indledende Datalogi

Two GUI class-libraries

• AWT (Abstract Windowing Toolkit)

– Came first – has ”Look & Feel” as platform

(Windows, Linux, Apple...)

• Swing

– Newer – common L&F across platforms

– L&F Programmable

– Class names mostly as AWT + ”J” in front

– Not a complete replacement for AWT

Page 3: Birds perspective on  Java GUI Programming

kelk & sh 2004 02312 Indledende Datalogi

Two basic App’s

• Applications– Based on Frame or JFrame– Run with ”java” – to get a console (nice for debug)– Run with ”javaw” to have no console– Same ”rights” as ”native” programs

• Applets– Based on Applet or JApplet– Embedded in HTML– Runs in Browser – e.g. IExplorer– Sandbox model for security (restrictions on I/O)

Page 4: Birds perspective on  Java GUI Programming

kelk & sh 2004 02312 Indledende Datalogi

Applications in Swing

• Your class ”extends” JFrame (inheritance)- we get a lot for free this way

• The GUI is ”wired up” in the constructor

• Components are added to the pane which is found with ”getContentPane()”(in AWT directly added to the Frame)

• A LayoutManager places components- e.g. ”Border”, ”Grid”, ”Flow”...

• E.g. in ”main” the class is instantiated

Page 5: Birds perspective on  Java GUI Programming

kelk & sh 2004 02312 Indledende Datalogi

Application Code Skeleton

MyClass extends JFrame { MyClass() { JButton hit = new JButton(”Hit!”); hit.<setup button+actionhandlers> getContentPane().add(hit); ....Setup Layout+more components }

public static void main(String[] a){ MyClass frame = new MyClass(); frame.pack(); frame.setVisible(true);}}

Page 6: Birds perspective on  Java GUI Programming

kelk & sh 2004 02312 Indledende Datalogi

From Application to Applet

• Change constructor into ”void init()”

- and remove calls to ”super” if any

• Remove ”main” completely

- no instantiation (done by browser)

• No setting of tittle

• Make an HTML-file with <applet > tag

Page 7: Birds perspective on  Java GUI Programming

kelk & sh 2004 02312 Indledende Datalogi

The wellknown console paradigm

• Client program is in charge

• Client calls OS when doing

I/O etc

• Client continues after call

• Typically asks one

question at the time and

waits for an answer

Client

Client

Client

OS

OS

time

Page 8: Birds perspective on  Java GUI Programming

kelk & sh 2004 02312 Indledende Datalogi

The Event-Based Paradigm

• The OS is in charge

• OS calls fragments

of client code -

based on events

• Not easy to predict

calling sequences

• You create code that

you never call

OS

client clientclient

client clienttime

Page 9: Birds perspective on  Java GUI Programming

kelk & sh 2004 02312 Indledende Datalogi

MVC-pattern: Model-View-Control

• Model is the data in e.g. attributes/fields or externally

- can be complex data-structures.

• View is the GUI-components setup in the constructor

(Buttons, Lists, Checkboxes etc)

• Control is program-flow - done via event-handlers

(With MS MFC View and Control is mixed)

Page 10: Birds perspective on  Java GUI Programming

kelk & sh 2004 02312 Indledende Datalogi

Listeners & Event-Handlers

Appl. Comp. Event Object Listener Object

The OS generates an event with help from the application’s component, and sends it to a handler. A handler is a method on a listener object

Your ”listener” is registered at your component – typically in Constructor (Frame) or ”init()” (Applet)

Page 11: Birds perspective on  Java GUI Programming

kelk & sh 2004 02312 Indledende Datalogi

Listener Groups

• ActionListener for buttons and menus

• KeyListener for keyboard

• MouseListener

• WindowListener for Window opened, closed etc.

There are 4 ways to implement listeners & handlers…following in (personal) prioritized order

Page 12: Birds perspective on  Java GUI Programming

kelk & sh 2004 02312 Indledende Datalogi

1. Way: Inner Class

• Inside the client class a new class is coded

• Register listener using e.g. ”new MyListener()”

• It has access to all fields in the outer class

• There may be several instances of the inner class per

instance of the outer class

• This concept encapsulates the listeners code – without

mixing MVC too much

Page 13: Birds perspective on  Java GUI Programming

kelk & sh 2004 02312 Indledende Datalogi

2. Way: Application is Listener

• As with inner-class, all code is in the same file

– full access to fields.

• Register listener using ”this”

• MVC not as mixed as anonymous classes

• MVC not as separated as inner classes

• Need to use ”implements” in order to extend

JFrame/Frame/JApplet/Applet.

...see later slide...

Page 14: Birds perspective on  Java GUI Programming

kelk & sh 2004 02312 Indledende Datalogi

3. Way: External Class

• The listener is coded in it’s own file – exactly as any other

class.

• Register listener using e.g. ”new MyListener()”

• Control & View is completely separated

- maybe too much.

• No access to private fields of View class.

Page 15: Birds perspective on  Java GUI Programming

kelk & sh 2004 02312 Indledende Datalogi

4. Way: Anonymous class

• The listener is registered with new without a class name at

the exact place in the code where the component is

inserted.

• This can be done automatically by many IDE’s

• This concept encapsulates the code

– but mixes control and view

• It is confusing and difficult to read – drop it!

Page 16: Birds perspective on  Java GUI Programming

kelk & sh 2004 02312 Indledende Datalogi

Two ways to code listeners

• To assure that it contains the necessary methods with the

right parameters, you can use ”implements” – the

drawback is that all methods (actually rather few) must be

implemented.

• Alternatively ”extend” an ”adapterclass”

- the drawback is that it’s only possible to extend from one

class