CSCI E-70
HarvardUniversityExtensionSchool
Spring 2004
Java for Graphical User Applications
Lecture 3 Outline
16 Feb 2005
- The Basis for all Swing Graphical Interface building blocks: JComponentHierarchy
- java.lang.Object – 11 methods:
- synchronization: multiple threads or GC thread:6
- identity: distinguising/distinguishing/outputting/type:5
- java.awt.Component
- 206new methods:
- deprecated methods: 35
- AWT only (not used if only lightweight used): 19
- 2 – popups
- 2 – native peer
- 9 – image
- 4 – event
- 2 – drag and drop
- internals or rarely used: 56
- 4 – layout
- 3 – properties
- 11 – painting management
- 11 – event propagation, processing
- 17 – obscure focus manipulations
- 9 – rarely used listeners
- likely to use or at least of interest:96
- 25 – commonly used listeners
- 8 get, 8 remove, 8 add, 1 special
- 2 – painting management
- paint(), repaint()
- 25 – geometry and layout (setting/querying)
- e.g. setWidth(), getAlignmentX()...
- 20 – appearance
- 10 getters, 10 setters
- e.g.: setFont(), getBackground() ...
- 8 – properties
- 4 queriers/getters(), 4 setters
- e.g.: setEnabled, setName()...
- 7 – debugging
- paramString(), toString(), list()...
- 7 – internationalization / accessibility
- 2 input-method related (but not listeners)
- get/setLocale()
- get/setComponentOrientation()
- getAccessibleContext()
- 2 – printing
- A quiet workhorse: capabilities confused with JComponent
- much overridden/deprecated/non-lightweight
- but a lot of basics still important: listeners, properties, geometry, repaint()...
- see in JComponent: “Methods inherited from Component”
- java.awt.Container – 69 new methods: two broad categories
- managing containment
- add, remove direct children
- propagate layout changes up/down hierarchy
- control propagation
- find components based on position
- who would use these methods?
- overriding various Component methods to be container-like
- printing
- sizing
- debugging
- doing property changes differently (new properties!)
- javax.swing.JComponent
- 339 total methods (remember, many deprecated/internal/rare)
- 10 inherited from Object
- 137 inherited from Component
- 51 inherited from Container
- 69 overridden from Component
- 18 overridden from Container
- 53 brand new ones
- 8 broad categories of new ones, in order of importance for us
- Borders/Insets
- Client properties (extensible!)
- Double buffering – avoid flicker
- Opacity management (very simple)
- Keystroke handling
- Tooltips
- Focus management (actually mostly in Component)
- Other (will not be covering unless time allows)
- Accessibility support
- Debug Graphics
- Auto-Scrolling
- The JComponent architecture
- Inheritance used sparingly (only one parent!)
- Components concrete and useful out-of-the-box
- lots of model/listener instantiation & setup done for you
- but all can be overridden
- sizing example, p. 141
- this IS object-oriented programming; you choose your level of customization!
- change value of a property
- compose new object into collection
- override individual methods
- add data members, write new methods
- write own minor classes:
- new event types
- write own major class: model, UI delegate, CellRenderer:
- bare, from the requisite base class or interface
- write own class suitefrom scratch
- JComponent
- Look and Feel / Theme
- wacky stuff
- custom 2-D rendering, image manipulation
- native 3-D rendering
- Everything Else in the World: tying it to your GUI...:-)
- invalidate, revalidate, validate, paint, repaint: sorting it out
- Borders
- composability
- factory design pattern
- insets and paint() vs paintComponent()
- Client Properties
- can avoid subclassing, stay flexible
- bound -- implications
- Double buffering – offscreen rendering
- Opacity control – background painted?
- Keystroke – example
- Tooltips – example 4.18
- Focus – basics, focusRoot
- Brief description of accessiblity, debug graphics, auto-scroll
- The Basic JComponents: Labels and Buttons
- Lots of overlap in functionality
- text
- images
- relative & absolute alignment, spacing control
- think of label as non-interactive button
- default button properties – p. 392
- Overview of remainder of hierarchy (Ch. 9)
- ToggleButtons
- checkboxes
- radio buttons
- mutually exclusive grouping
- A complex JComponent: JList
- Get to know models for real now: ListModel
- Customizable at interface, abstract, or concrete level
- Very simple concept
- Emphasize model/UI separability: the Contract
- Scroll containment: JScrollPane
- must set size
- is a validation root!
- Example code: 17-2: Using a DefaultListModel (concrete impl. of ListModel)
- First component with:
- open-ended choices on data representation
- need not be one actual object per list item
- need not be a Vector or array, even if so
- data model that generates events useful to others: when data changes
- multiple models: also a ListSelectionModel: a GUI state model!
- customizable smaller units: CellRenderers (oh no! more later)
- new listeners of interest to handle selections: ItemListeners
– Page 1 of 3–