Java for Graphical User Applications

estrapadesherbetSoftware and s/w Development

Nov 18, 2013 (3 years and 10 months ago)

118 views

CSCI E
-
70

Harvard University Extension School

Spring 2004



Page
1

of
3



Java for Graphical User Applications

Lecture 3 Outline

1
6 Feb 2005


I.

The Basis for all Swing Graphical Interface building blocks: JComponent

Hierarchy

A.

java.lang.Object


11
methods:

i.

synchroniz
ation:

multiple threads or GC thread
:


6

ii.

identity: distinguising
/
distinguishing/
outputt
ing/type:


5

B.

java.awt.Component


i.

20
6

new
methods
:

1.

deprecated methods
:




3
5

2.

AWT only (not used if only lightweight used):


19

a.

2


popups

b.

2


native peer

c.

9


image

d.

4


event

e.

2


drag and drop

3.

internals or rarely used:




5
6

a.

4


layo
ut

b.

3


properties

c.

11


painting management

d.

11



event propagation, processing

e.

1
7



obscure focus manipulations

f.

9



rarely used listener
s

4.

likely to use

or at least of interest
:



9
6

a.

25



commonly used listeners

i.

8 get, 8 remove, 8 add, 1 special

b.

2


painting

management

i.

paint(), repaint()

c.

25


geometry and layout (setting/querying)

i.

e.g. setWidth(), getAlignmentX()...

d.

20


appearance

i.

10 getters, 10 setters

ii.

e.g.:
setFont(), getBackground() ...

e.

8


properties

i.

4 queriers/getters(), 4

setters

ii.

e.g.:
setEnabled, setN
ame()...

f.

7


debugging

i.

paramString(), toString(), list()...

g.

7



internationalization / accessibility

i.

2 input
-
m
ethod related (but not listeners
)

ii.

get/set
Locale()

iii.

get/setC
omponentO
rientation
()

iv.

getAccessibleContext()

h.

2


printing

ii.

A quiet workhorse: capabilitie
s confused with JComponent

1.

much overridden/deprecated/non
-
lightweight

2.

but a lot of basics still important: listeners, properties, geometry,
repaint()...

a.

see in JComponent: “Methods inherited from Component”

CSCI E
-
70

Harvard University Extension School

Spring 2004



Page
2

of
3



C.

java.awt.Co
ntainer



69 new

methods
: two broad ca
tegories

i.

managing containment

1.

add, remove

direct children

2.

propagate layout changes up/down hierarchy

3.

control propagation

4.

find

components based on position

a.

who would use these methods?

ii.

overriding various Component methods to be container
-
like

1.

printing

2.

sizin
g

3.

debugging

4.

doing property change
s differently (new properties!)

D.

javax.swing.JComponent

i.

33
9
total
methods

(remember, many deprecated/internal/rare)

1.

10 inherited from Object

2.

137 inherited from Component

3.

51 inherited from Container

4.

69 overridden from Compon
ent

5.

18

overridden

from Container

6.

53
brand n
ew

ones

ii.

8

broad categories

of new ones
, in order of importance for

us

1.

Borders
/Insets

2.

Client properties (extensible!)

3.

Double buffering


avoid flicker

4.

Opacity management (very simple)

5.

Keystroke handling

6.

Tooltips

7.

F
o
cus management (actually mostly in Component)

8.

Other (will not be covering unless time allows)

a.

Accessibility support

b.

Debug Graphics

c.

Auto
-
Scrolling


II.

The JComponent architecture

A.

Inheritance used sparingly (only one parent!)

B.

Components concrete and useful out
-
of
-
the
-
box

i.

lots of model/listener instantiation & setup done for you

ii.

but all can be overridden

1.

sizing example, p. 141

2.

this
IS
object
-
oriented programming
; y
ou choose your level of
customization!

a.

change
value of a property


b.

compose new object into collecti
on

c.

override individual methods

d.

add data members, write new methods

e.

write own minor classes:

i.

new event types

f.

write own
major class: model, UI delegate, CellRenderer:

i.

bare, from the requisite base class or interface

g.

write own
class suite

from scratch

i.

JCompo
nent

ii.

Look and Feel / Theme

CSCI E
-
70

Harvard University Extension School

Spring 2004



Page
3

of
3



h.

wacky stuff

i.

custom 2
-
D rendering, image manipulation

ii.

native 3
-
D rendering

iii.

Everything Else in the

World: tying it to your GUI...
:
-
)

C.

invalidate, revalidate, validate
, paint, repaint
: sorting it out

D.

Borders

i.

composability

ii.

factory des
ign pattern

iii.

insets and paint() vs paintComponent()

E.

Client Properties

i.

can avoid subclassing, stay flexible

ii.

bound
--

implications

F.

Double buffering


offscreen rendering

G.

Opacity control


background painted?

H.

Keystroke


example

I.

Tooltips


example 4.18

J.

Focus


basics, focusRoot

K.

Brief description of accessiblity, debug graphics, auto
-
scroll

III.

The Basic JComponents: Labels and Buttons

A.

Lots of overlap

in functionality

i.

text

ii.

images

iii.

relative & absolute alignment, spacing control

iv.

think of label as non
-
interactive butto
n

B.

default button properties


p. 392

C.

Overview of remainder of hierarchy

(Ch. 9)

i.

ToggleButtons

1.

checkboxes

2.

radio buttons

3.

mutually exclusive grouping

IV.

A complex JComponent: JList

A.

Get to know models for real now: ListModel

i.

Customizable at interface, abstract, o
r concrete level

ii.

Very simple concept

iii.

Emphasize model/UI separability: the Contract

B.

Scroll containment: JScrollPane

i.

must set size

ii.

is a validation root!

C.

Example code: 17
-
2
: Using a DefaultListModel (concrete impl. of ListModel)

D.

First component with:

i.

open
-
end
ed choices on data representation

1.

need not be one actual object per list item

2.

need not be a Vector or array, even if so

ii.

data model that generates events useful to others: when data changes

iii.

multiple models: also a ListSelectionModel: a
GUI state

model!

iv.

cust
omizable smaller units: CellRenderers (oh no! more later)

v.

new listeners of interest to handle selections: ItemListeners