SCOUG Java Progamming Class 3

Overview


Class 2 Review


Window Types

The window types are used as containers in which to embed user-interface components. They are implemented by the java runtime using the host operating system window management and event processing facilities. The "window" classes are all extensions of the abstract container class.


Layouts

When creating a user interface, the placement of its components relative to each other and to the embedding container is achieved by using a layout manager. There are five provided layout manager classes each of which is an implementation of the LayoutManager interface. Each of these classes provides a different overall presentation.

The general logic for using a layout manager is:

The layout manager classes are:


GUI Components


Menus

A special type of container, the MenuContainer, is used to construct menus. The menu is implemented as an instance of the MenuBar class and can be attached to a frame object using its setMenuBar() method. The classes are:


Event Handling

Events

Events are used to convey messages to GUI components in response to user actions. The component's event-handling methods can be coded to perform whatever processing is necessary for that action.

When an event occurs, an Event object is created. This object contains all of the pertinent data about the event such as event type, timestamp, target object, etc. (refer to event class below for more details). The component class (of which all GUI objects are descendants) contains a special method for event processing - the handleEvent method. It receives one parameter which is the event object.

After the event object is created, it is made available to one or more components for processing. The order in which it is made available is hierarchical starting with the most embedded object and working out to the least embedded. For example, if a button is embedded in a card layout panel which is embedded in a border layout panel which is embedded in a frame the order of processing will be:

At each level, the handleEvent method of the component can either:

If the component indicates that further processing may be needed then the event will be made available to the next outer component's handleEvent method. (Further processing is indicated by a boolean return value of false.) The handleEvent method may need to allow other default processing of the event to occur (if it is an override). This can be accomplished by returning the value from the defining class; EG return super.handleEvent(…) rather than just return true or return false.

Actions

In some cases, a user interaction comprises several actual events and the component processing is required only when all of the events have completed (eg button click). Another special type of event and method is provided for this situation. If the event.id field is set to Event.ACTION then the action(…) method of the component will be invoked by the handleEvent method during default processing and the action method can be overridden to process these types of interactions. It must also return a boolean value of true or false to indicate that further processing may be needed.

For example, suppose you have a panel with a number of buttons and a card layout with a separate card containing information for each of the buttons. You want to display the appropriate card when a button is pressed and you have named the cards with the button names when they were created. The following code fragment will accomplish this task:

Cardlayout cl = new Cardlayout();
Panel mainPanel = new Panel();
..
mainPanel.setLayout(cl);
..
mainPanel.add(button1.getLabel(), panel1);
mainPanel.add(button2.getLabel(), panel2);
mainPanel.add(button3.getLabel(), panel3);
..
public boolean action(Event e, Object o) {
  if (e.target instanceof Button) {
     cl.show(mainPanel, ( (Button) e.target ).getLabel() );
     return true;
  }
  return false;
}

Other Methods

The component class also includes a set of methods which allow you to process certain types of interactions again without having to test explicitly within the handleEvent method. They are:

Explicit

You can explicitly generate an event by overriding these methods:


Packages

 The AWT package contains the classes and interfaces for all of the user-interface components.

type

name

extends

class

BorderLayout

implements LayoutManager

class

Button

Component

class

Canvas

Component

class

CardLayout

implements LayoutManager

class

Checkbox

Component

class

CheckboxGroup

Component

class

Checkbox

Component

class

CheckboxMenuItem

MenuItem

class

Choice

Component

abstract

Component

implements ImageObserver

abstract

Container

Component

class

Dialog

Window

class

Dimension

 

class

Event

 

class

FileDialog

Dialog

class

FlowLayout

implements LayoutManager

class

Font

 

class

FontMetrics

 

class

Frame

Window implements MenuContainer

abstract

Graphics

 

class

GridBagConstraints

implements Clonable

class

GridBagLayout

implements LayoutManager

class

GridLayout

implements LayoutManager

abstract

Image

 

class

Insets

implements Clonable

class

Label

Component

class

List

Component

class

MediaTracker

 

class

Menu

MenuItem implements MenuContainer

class

MenuBar

MenuComponent implements MenuContainer

abstract

MenuComponent

 

class

MenuItem

MenuComponent

class

Panel

Container

class

Polygon

 

class

Rectangle

 

class

Scrollbar

Component

class

TextArea

TextComponent

class

TextComponent

Component

class

TextField

TextComponent

class

Toolkit

 

class

Window

Container

interface

LayoutManager

 

interface

MenuContainer

 

 


Reference Notes:

GridbagConstraint values

The following table lists the gridbagconstraint variables and values.

NOTE: values declared in uppercase must be preceded with the class name ( eg - GridBagConstraints.RELATIVE )

NOTE: in a list of values, the italicized value is the default

Name

Description

anchor

orientation of component within its cell. Values are: CENTER, EAST, NORTH, NORTHEAST, NORTHWEST, SOUTH, SOUTHEAST, SOUTHWEST, WEST

fill

 how to "enlarge" the component to fill its cell. Values are: NONE, HORIZONTAL, VERTICAL, BOTH

gridheight

height of this cell. Value can be an integer or REMAINDER

gridwidth

width of this cell. Value can be an integer or REMAINDER

gridx

x-coordiante of the cell. Value can be an integer or RELATIVE

gridy

y-coordinate of the cell. Value can be an integer or RELATIVE

insets

defines padding between cell boundary and component

ipadx

defines horizontal padding between cells

ipady

defines vertical padding between cells

weightx

defines horizontal weight of component during layout

weighty

defines vertical weight of component during layout

Event Class

The details of the event class implementation are reproduced here for reference purposes. Values given in upper case must be preceded by Event. when used (EG Event.MOUSE_UP)

Variables

 

Object arg

argument provided with event, content depends on event type

int clickCount

for MOUSE_DOWN, the number of mouse button clicks

Event evt

for linked event lists, the next event

int id

the event identification. Values incude: ACTION_EVENT, GOT_FOCUS, KEY_ACTION, KEY_ACTION_RELEASE, KEY_PRESS, KEY_RELEASE, LIST_DESELECT, LIST_SELECT, LOAD_FILE, LOST_FOCUS, MOUSE_DOWN, MOUSE_DRAG, MOUSE_ENTER, MOUSE_EXIT, MOUSE_MOVE, MOUSE_UP, SAVE_FILE, SCROLL_ABSOLUTE, SCROLL_LINE_DOWN, SCROLL_LINE_UP, SCROLL_PAGE_DOWN, SCROLL_PAGE_UP, WINDOW_DEICONIFY, WINDOW_DESTROY, WINDOW_EXPOSE, WINDOW_ICONIFY, WINDOW_MOVED

int key

which key was used (KEY_ACTION or KEY_ACTION_RELEASE), values are: DOWN, END, F1 - F12, HOME, LEFT, PGDN, PGUP, RIGHT, UP

int modifiers

which modifiers are active; values are: ALT_MASK, CTRL_MASK, META_MASK, SHIFT_MASK (these have slightly different meanings for key and mouse events)

Object target

the Object on which the event occurred

long when

timestamp when event occurred

int x

x-coordinate of event

int y

y-coordinate of event

Constructors

public Event(Object target, int id, Object arg)

 

public Event(Object target, long when, int id, int x, int y, int key, int modifiers, Object arg)

 

public Event(Object target, long when, int id, int x, int y, int key, int modifiers)

Methods

public boolean controlDown()

 

public boolean metaDown()

 

protected String paramString()

 

public boolean shiftDown()

 

public String toString()

 

public void translate(int dx, int dy)

 


Copyright Notices

Java and JDK as referenced herein are protected by the following copyright notices:

(c) Copyright Sun Microsystems Inc., 1992-1997. All rights reserved.

(c) Copyright IBM Corporation, 1996-1997. All rights reserved.

The JDK is based in part on the work of the Independent JPEG group.

The Java (tm) technology is owned and exclusively licensed by Sun Microsystems Inc. Java (tm) is a trademark of Sun Microsystems Inc. in the United States and other countries.

The JDK implementation used in this class is OS/2 version 1.0.2 and is used with permission of IBM Corporation. This use is governed by the terms and conditions specified in the beta license agreement provided by IBM Corporation.