3. AWT Review


AWT Review

  • This course assumes familiarity with AWT
  • This review will be fast!
  • Concentrate on layout managers and interface design

AWT Concepts

  • AWT = Abstract Windowing Toolkit
  • Component = basic GUI element in AWT
  • Container = a component that can contain other components
  • Layout Manager = a class that figures out component position/size
  • Event = a external stimulus to an object

AWT Component Hierarchy

Component

  • Highest level AWT element
  • All other AWT elements are extended from this
  • Abstract
  • Can extend directly from this to create a "lightweight" component
  • Most functionality is defined here

Container

  • Can contain other AWT elements
  • Abstract
  • Can extend directly from this to create a "lightweight" container
  • If extending, override addImpl() to track additions
  • Can have layout manager

Button

  • Graphical representation of a push button
  • Clicking it dispatches an ActionEvent event.

Canvas

  • Can be decorated using its paint() method.
  • Is usually the class extended when creating a custom component.

Checkbox

  • A text label with a box that can be checked/unchecked
  • Has state of true = checked, false = unchecked

CheckboxGroup

  • When associated with Checkboxes, the Checkboxes become "radio buttons"
  • Only one Checkbox in a given CheckboxGroup can be checked at a time

Choice

  • Presents a selected item.
  • Clicking the down arrow opens a list of selections to choose from.
  • Selected item cannot be edited (not a "ComboBox").

Label

  • A piece of text written to the screen.
  • More useful than just graphically drawing text, as it can be positioned by a layout manager.

List

  • Presents a list of text items.
  • One or more may be selected depending on its setting.

ScrollBar

  • Visually displays a range,and a position within that range,

TextComponent

  • Provides common functionality for TextArea and TextField

TextArea

  • Allows user to enter a block of text.
  • Primitive editing functions provided.
  • 32K limit on most platforms.
    • Don't use this if you're writing an editor!

Panel

  • A container (can contain other components)
  • Provides a canvas-like area on which graphics can be drawn.
    • Note that it is not a Canvas
  • Is a good candidate for creating new container components that need graphics drawn on them.

Applet

  • A panel (so it's a container as well...)
  • Can be displayed in a web page.

ScollPane

  • A container that provides scrollbars to move around its contained component.
  • It is only allowed to contain one component.
    • But that component could be a container...

Window

  • A native window on the current platform
  • Does not have window decorations like
    • border
    • title bar
    • system menu
    • minimize/maximize/close buttons

Frame

  • A window that can be manipulated as a standard application window on the target platform.
  • Has all standard window decorations for the platform.
  • Is the only component that can contain a menubar!

Dialog

  • A frame-like window that is used for requesting information from the user.
  • Can be modal or non-modal.
  • Read dialogs are extended from this parent class.

FileDialog

  • A standard dialog that can be used to load or save files.
  • Uses the platform's native file dialog(s).

Menus

  • Provide a hierarchical means for the user to select an action to perform.
  • Items in a menu can themselves be menus
  • Items can be checkbox-like items.

Menu Components

  • Several components that work together:
    • MenuBar -- attaches to a Frame to hold Menus.
    • Menu -- the high-level name in the menu bar. Can contain other Menus, MenuItems, CheckboxMenuItems or MenuSeparators.

Menu Components (continued)

  • Several components that work together (continued):
    • MenuItem -- a selection within a menu. It can dispatch an ActionEvent like a Button.
    • CheckboxMenuItem -- a selection within a menu that has a checkbox in it. The state of the checkbox can be changed.
    • MenuSeparator -- a nice little line that helps visually separate other items in the menu.

Layout Managers

  • Components can be laid out at specific x,y locations.
    someComponent.reshape(x,y,width,height);
    add(b);
    
  • But, this is inflexible--what happens when the window is resized?
    • Here, component layout is not a function of window shape or size.

Layout Managers (continued)

  • What's a LayoutManager?
    • An object that implements interface LayoutManager.
    • Lays out components according to some scheme.
    • Associated with a particular Container via setLayout:

AWT-Provided Layout Managers

  • FlowLayout -- arranges components left-to-right, line by line.
  • BorderLayout -- arranges components at North, South, East, West and Center, like a compass
  • CardLayout -- arranges components on top of one another -- only one is visible at a time

AWT-Provided Layout Managers (continued)

  • GridLayout -- arranges components in a grid -- each gets the same amount of space
  • GridBagLayout -- arranges components in a grid, but each can take up different amount of space

But None of the Layout Managers Solve my Problem!

  • Write your own!
    • Take a look at BorderLayout and FlowLayout (source code is provided in the JDK)
    • Very simple to write
      • Caution -- make sure you respect the container's insets!

FlowLayout

  • Arranges components left-to-right until no more objects fit on that "line." Rest are wrapped to next line.
  • Default for applets.

BorderLayout

  • Manages "North", "South", "East", "West", "Center" positions.
  • Elements are stretched.

CardLayout

  • Displays one "card" at a time.
  • Each component fills entire container.
  • Cards are accessed via string key.
  • Card brought to front via show("key")

GridLayout

  • Arrange components into rows/columns.
  • Number of rows/columns specified at construction.
    • But, can leave one dimension 0, indicating "any number".

GridBagLayout

  • Most flexible
  • Most complex
  • Will not be used in this class

Interface Layout Design

  • Draw a picture of what you want the interface to look like
  • Two approaches to determining how to layout components:
    • GridBag approach
      • Figure out where everything is and assign constraints to each component
    • Nesting approach
      • Partition the interface into workable parts
      • Lather, rinse, repeat until you're down to simple interfaces.
  • For this class, use the nesting approach.

Layout Manager Design Considerations

  • Each component has a preferred width and height, and a minimum width and height.
  • Layout managers can ignore or honor these parameters.
  • The standard layout managers do the following:
  • BorderLayout
    North/South Components -- preferred height is honored
    East/West Components -- preferred width is honored

Layout Manager Design Considerations (continued)

  • FlowLayout
    Preferred width and height is honored
  • GridLayout and CardLayout
    Preferred size ignored

About CardLayout

  • Use when you want
    • only one of a set of components visible at a time
    • to cycle through a set of components
    • to create a "tabbed panel" or "notebook"

About BorderLayout

  • Use when ou have one central component that should expand or shrink.
  • Use when you have zero or more components that should border it with a fixed width or height.

About BorderLayout (continued)

  • Order of layout:
    • North & South components (expands to full width, uses preferred height)
    • East & West components (expands to available height width, uses preferred width)
    • Center component (expands to fill remaining space
  • Useful for adjoining a label to another component
    • Put the label "West", other component "Center"

About FlowLayout

  • Use when you want:
    • components to keep their preferred size
    • components arranged so as many are visible as possible
    • a horizontally-preferred ordering
  • Be careful not to directly nest FlowLayouts -- they tend to not lay out terribly well due to calculation of a FlowLayout's preferred size.

About GridLayout

  • Use when you want
    • All components to take up the same amount of space
    • Components to be arranged in a two-dimensional grid
    • A fixed number of rows or columns (the other dimension can vary)

About GridBagLayout

  • Use when
    • components need "special" spacing relative to one another
    • a simpler layout manager cannot handle the situation
    • you cannot create the same effect by simple nesting of layouts
  • Requires some study to really understand it

Custom Layout Managers

  • Not difficult to write
  • Good idea if the layout is likely to be needed in many cases
  • Good idea if a correct layout
    • would require deep nesting of other layouts
    • would require a complex GridBagLayout
    • has a simple physical model not handled by another layout manager
  • Look at the source for BorderLayout

Copyright © 1996-1997 MageLang Institute. All Rights Reserved.