Components Containers and Layout Menus Dialog Windows Event Handling The Abstract Window Toolkit (AWT), from Java :
Abstract Window Toolkit
Interface to the GUI Interface to platform's components: Layout: Placing GUI event keyboard, window system Buttons, text components handling mouse, … (Win, Mac, …) fields, …
Uses operating system components Don't use these! . Looks like a native application . One must sometimes be aware of differences between operating systems… . Small set of components . , … – no table, no color chooser, … The Java Foundation Classes, from Java :
Java Foundation Classes (JFC)
Java : AWT, Swing More advanced Abstract Window Toolkit graphics classes
Components based on pure Java "Painting on the screen" . Won't always look "native”, . The basis of Swing but works identically on all platforms components – and your own . Replaces AWT components, adds more . Discussed next lecture . We still use many other parts of AWT Components: JTable, JButton, … extending JComponent
Containers: JFrame – a top level window; JPanel – a part of a window, grouping some components together
Layout Managers: Decide how to place components inside containers Swing: Can replace the look and feel dynamically . Nimbus (current Java standard)
. Metal (earlier Java standard)
. Windows classic
Running example: A very simple word processor Ordinary window in Swing: JFrame . A top-level container: Not contained in anything else ▪ AWT
Base class for all Swing components
Common implementation details
Has two states, on/off Radio buttons: Only one per Standard button active at a time
Checkbox, on / off Editing styled text: Abstract base class, HTML, RTF, common functionality custom formats
A single line of text Multi-line text area
Special formatting for Passwords are not shown dates, currency, … as they are entered . Let's add components… ▪
Nothing is shown on the screen yet!
We don’t want to see individual components pop up… When we have added all components: . Give the window a size… ▪ ▪ ▪
. And then display it! ▪
Tiny text area
We have to know more about layout…
The structure of a container: . Can contain other “subcontainers” – a containment hierarchy
Can be added if desired
Contains all components except the menu bar
Title Content pane Subcontainers can simplify layout We decide that we should have three ▪ : Contains menus subcomponents: ▪ : Contains toolbar buttons A JPanel, a JTextPane ▪ : A rectangular section and a JLabel. ▪ : A scrollable section The JPanel should contain many buttons. Let's try! ▪
▪
We get the intended grouping, but not the intended layout… How should subcomponents be positioned and sized? . Simplest approach: Specify absolute x and y coordinates ▪ ▪ . Advantages / disadvantages: ▪ (+) Easy to write a GUI designer ▪ (-) What about resizeable GUIs? (Typical old-style Windows dialogs don’t allow resizing…) ▪ (-) What about larger fonts? ▪ (-) What about translations into other languages, where words are longer? Preferred Sizes
Most components can calculate a preferred size : Label size for the given font + padding
Text pane is empty preferred size is tiny!
Containers have a layout manager
Asks subcomponents for preferred size Applies layout rules ( default: Left to right “flow layout”) Calculates the container’s preferred size
Continues hierarchically… Defining Preferred Sizes . Dynamic calculation: ▪
. For constant preferred sizes, no subclasses are needed: ▪
. Also: ▪ (): Maximum possible size ▪ (): Minimum possible size ▪ Used when components can be elastically resized Packing and resizing
If you pack() the top-level window… …it asks its layout manager for a preferred size
If you setSize() the top-level window… What happens depends on the layout managers’ overflow handling Several simple layout managers built in
Can be combined into complex layouts MigLayout – strongly recommended! By default: . Grid-based, modern, extremely flexible Preferred size used; ▪ continue on this row; add a gap suitable for related components
A gap suitable for two unrelated components New row after this component Span 3 columns, … make the field grow larger than its preferred size Layout parameters for each component Code skeleton: ▪
Strange: We create an object, forget about it, and then we’re done?
As long as any frame is active, Java will not exit! We can still interact with the frame through GUI events… Buttons are contained in their own panel
▪
Separation
points margin to the left of the component ▪ Layout constraints
Column constraints
Our goal… Row constraints ▪ ▪
Separators Creates an invisible component, pixels tall, up to infinity wide
How do you know when a button has been pressed? Swing is not thread-safe: AWT receives ”raw” keyboard/mouse input Once a component has been shown, . In a background event dispatch thread only manipulate it in this thread! . Generates low-level events: ,
Calls event handlers in relevant components . Keyboard event sent to component with keyboard focus
Built-in event handlers can generate semantic events . Click inside a button, then release
You can register your own event handlers -- listeners! . A listener is interested in a particular type of event . Example: A particular button has been pressed How do listeners work? Observer design pattern! ▪ Listeners are observers, observing specific event types Components are observable, can tell others ▪ when something happens Keep track of listeners
Call this method to add your own listener
Key events end up here… One event at a time! ...in the event dispatch thread, If your listener takes a long and the method goes on to call time, the UI will freeze. If so, all registered listeners let the listener start a thread! How are listeners used? ▪
Create local variables for the buttons, so you can refer to them later!
▪
A lot of trivial code around “the real work”
Called by the event system in the event dispatch thread when Bold is pressed Alternative: Inner Class ▪
Define a class inside another – even inside a method!
Gives access to fields of the enclosing class, final local variables of the method
We still need a name… Alternative: Anonymous Inner Class ▪
Special “syntactic sugar”! 1) Declares an unnamed class implementing 2) Gives it an implementation for () 3) Creates a new object of this unnamed class First alternative: One listener per button
You know where the event came from – the bold button
A little bit of overhead from defining many classes Second alternative: A combined listener class
Since many components share listeners, you must check the source of each event Alternative for actions that can be triggered from many places: . Use (interface) and (helper class)! ▪
. Many components can be constructed from an action object ▪ Event handling for menus: No difference in principle . To close a window programmatically: Call . . Frees all resources related to the window To configure what the close button does: . Use , where x is… ▪ ▪ ▪ ▪ . To add a yes/no dialog: ▪ Use ; add your own An AWT adapter provides empty implementations for an interface with many methods you only implement those you are interested in . Some of the most important listeners: ▪ Button pushed, menu item selected, … ▪ Slider or scroll bar adjusted ▪ Items selected in list ▪ Mouse button pushed/released, … ▪ Mouse pointer moved ▪ Key pressed, typed, released Sent to the component that has keyboard focus If not processed, passed on to its container, etc. Alternative to KeyListener: Key Bindings . Works if keyboard focus is in THIS component
Check the KeyStroke Javadoc An action name (next slide!) for examples
Works if keyboard focus is here or in a subcomponent
Works if keyboard focus is in the same window Alternative to KeyListener: Key Bindings . Define a standard (can be used in menus or other places as well)
Define which is called for the action name ”moveLeft” Standard Dialog Windows Additional Features Debugging a GUI Program Standard dialog windows: ▪ Additional dialog windows: . File dialogs ▪
. Color selection dialogs ▪ Additional features we will not discuss: . Data transfer support: java.awt.datatransfer ▪ Transfer objects between applications ▪ Clipboard support (copy/paste) ▪ Retrieving text from the system clipboard:
. Help system: JavaHelp (download from ) . Printing: . Drag and Drop support: . The Undo/Redo Framework: You can view the component hierarchy . Select a frame (window) and press . The component hierarchy is dumped to standard error
.
▪ ▪