0% found this document useful (0 votes)
83 views271 pages

Java 2 Gui Fud Mentals With Swing

livro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views271 pages

Java 2 Gui Fud Mentals With Swing

livro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 271

Java™ 2 GUI Fundamentals with Swing

Instructor: Michael Morrison

www.digitalthink.com
© 1999 DigitalThink, Inc. All rights reserved.
Course Syllabus

Module 1:
Lesson 1 - Course introduction
Lesson 2 - Prerequisites
Lesson 3 - What you need
Lesson 4 - What to expect
Sidebar: Copying and pasting on Unix platforms
Lesson 5 - The course tutors
Lesson 6 - Meet your instructor
Sidebar: Michael Morrison's introduction (Audio transcript)
Lesson 7 - Course project
Sidebar: Features of the Doodle application (FlipBook transcript)
Module 2:
Lesson 1 - Events and user interaction
Lesson 2 - Event fundamentals
Sidebar: The delegation event model is not alone
Lesson 3 - Event sources and listeners
Sidebar: Unicast and multicast event delivery
Sidebar: The event delivery process (FlipBook transcript)
Lesson 4 - Responding to events
Sidebar: Significance of the delegation event model (Audio transcript)
Sidebar: Registering a class as an event listener (MouseOver transcript)
Sidebar: Implementing an event listener (MouseOver transcript)
Quiz: Event basics
Lesson 5 - Handling mouse events
Sidebar: The MouseListener interface (MouseOver transcript)
Sidebar: The MouseMotionListener interface (MouseOver transcript)
Exercise: Starting the Doodle application
Lesson 6 - Using event adapters
Sidebar: Inner classes
Sidebar: The MouseHandler inner class (MouseOver transcript)
Exercise: Using event adapters in Doodle
Lesson 7 - Handling keyboard events
Sidebar: The KeyListener interface (MouseOver transcript)
Exercise: Supporting the keyboard in Doodle
Lesson 8 - Module wrap-up
Quiz: Events and user interaction
Module 3:
Lesson 1 - Introducing Swing
Lesson 2 - GUI basics
Sidebar: GUI design
Sidebar: GUI components (FlipBook transcript)

file:///c|/courses/jv301_r/syllabus.html (1 of 4) [1/11/2000 1:17:58 PM]


Course Syllabus

Exercise: Moving Doodle's drawing functionality into a component


Lesson 3 - Swing and the AWT
Lesson 4 - The Swing application framework
Quiz: Swing basics
Lesson 5 - Framing an application
Sidebar: The getContentPane() method (MouseOver transcript)
Exercise: Porting the Doodle application to Swing
Lesson 6 - Understanding panes
Sidebar: Mixing AWT with Swing (Audio transcript)
Sidebar: Layering components
Lesson 7 - Module wrap-up
Quiz: Module quiz
Module 4:
Lesson 1 - Laying out user interfaces
Lesson 2 - Layout managers
Sidebar: Using layouts (Audio transcript)
Lesson 3 - Working with layouts
Sidebar: Creating a FlowLayout object (MouseOver transcript)
Sidebar: Specifying the spacing between components (MouseOver transcript)
Sidebar: Assigning a container (MouseOver transcript)
Lesson 4 - The flow layout
Sidebar: Adding components using a flow layout (FlipBook transcript)
Quiz: Layout basics
Exercise: Using the flow layout
Lesson 5 - The grid layout
Sidebar: Adding components using the grid layout (FlipBook transcript)
Exercise: Using the grid layout
Lesson 6 - The border layout
Sidebar: Adding components to containers (FlipBook transcript)
Exercise: Selecting tools in the Doodle application
Lesson 7 - The card layout
Sidebar: Adding components to the card panels (FlipBook transcript)
Lesson 8 - The box layout
Sidebar: The box layout versus the grid bag layout
Sidebar: Using the box layout manager (FlipBook transcript)
Lesson 9 - Alignment and invisible fillers in the box layout
Sidebar: Alignment values for a vertical box (FlipBook transcript)
Sidebar: The Box class (FlipBook transcript)
Lesson 10 - Module wrap-up
Quiz: Module quiz
Module 5:

file:///c|/courses/jv301_r/syllabus.html (2 of 4) [1/11/2000 1:17:58 PM]


Course Syllabus

Lesson 1 - Using Swing components


Lesson 2 - Icons
Sidebar: Disabled image icons
Lesson 3 - Working with labels
Sidebar: Alignment constants
Exercise: Using the label component
Lesson 4 - Using buttons
Quiz: Icons, labels, and buttons
Exercise: Tinkering with buttons
Lesson 5 - Jazzing things up with borders
Sidebar: The ramifications of using borders (Audio transcript)
Sidebar: Java's border classes (FlipBook transcript)
Exercise: Improving the look of components with borders
Lesson 6 - Lists
Sidebar: Various features of lists (FlipBook transcript)
Sidebar: Scrolling components
Lesson 7 - Combo boxes
Sidebar: Sample combo boxes (FlipBook transcript)
Exercise: Presenting a list of choices
Lesson 8 - Other Swing components
Sidebar: Swing components in action (FlipBook transcript)
Lesson 9 - Module wrap-up
Quiz: Module quiz
Module 6:
Lesson 1 - Digging deeper into Swing
Lesson 2 - Menus
Sidebar: Drop-down menu types (FlipBook transcript)
Lesson 3 - Menu creation
Sidebar: Creating a menu (FlipBook transcript)
Exercise: Adding a menu to the Doodle application
Lesson 4 - Using keyboard mnemonics and accelerators
Sidebar: Setting the mnemonic and accelerator (MouseOver transcript)
Exercise: Supporting keyboard mnemonics and accelerators in Doodle
Lesson 5 - Working with toolbars
Sidebar: Using the JToolBar class (MouseOver transcript)
Quiz: Menu and toolbar basics
Exercise: Adding a toolbar to the Doodle application
Lesson 6 - Coordinating GUI actions
Sidebar: Clarifying the purpose of actions (Audio transcript)
Sidebar: Creating the paste action (MouseOver transcript)
Exercise: Coordinating GUI actions in Doodle

file:///c|/courses/jv301_r/syllabus.html (3 of 4) [1/11/2000 1:17:58 PM]


Course Syllabus

Lesson 7 - Putting tool tips to use


Sidebar: Sample tool tips (FlipBook transcript)
Exercise: Supporting tool tips in the Doodle application
Lesson 8 - Module wrap-up
Quiz: Digging deeper into Swing
Module 7:
Lesson 1 - Enhancing Swing user interfaces
Lesson 2 - Dialog boxes and option panes
Sidebar: Custom dialog boxes
Lesson 3 - Using option panes
Exercise: Displaying information with an option pane
Lesson 4 - Working with choosers
Sidebar: Using the Swing color chooser (MouseOver transcript)
Sidebar: Creating a file chooser (MouseOver transcript)
Quiz: Dialog boxes, option panes, and choosers
Exercise: Using the color chooser dialog box in Doodle
Lesson 5 - Understanding pluggable look and feel
Lesson 6 - Setting the look and feel
Exercise: Setting the look and feel of Doodle
Lesson 7 - Playing sound
Sidebar: Why you call a static method (Audio transcript)
Exercise: Spicing up Doodle with sound
Lesson 8 - Module wrap-up
Quiz: Module quiz
Module 8:
Lesson 1 - Course conclusion
Sidebar: GUI design and construction with Swing (Audio transcript)
Lesson 2 - We'd love your feedback
Lesson 3 - Help us help you learn!

file:///c|/courses/jv301_r/syllabus.html (4 of 4) [1/11/2000 1:17:58 PM]


JavaTM 2 GUI Fundamentals with Swing

Glossary

Abstract Windowing Toolkit (AWT)

A set of classes and interfaces in the Java API that is devoted to building graphical user
interfaces (GUIs) in Java applets and applications. The AWT forms the basis of the Swing
toolkit, which provides more advanced GUI features.

Accelerator

Provides a means of directly accessing menu items without navigating through a menu
hierarchy. An accelerator key-combination is identified visually to the right of a menu item.

Access modifier

Access modifiers define different levels of access for class members including private,
protected, public, and a default access level.

Applet

Graphical Java programs that execute within a Web page.

AppletViewer

A utility included with the Java Development Kit that allows you to test applets without using a
Web browser.

Application Programming Interface (API)

The set of Java packages and classes included in the Java 2 Software Development Kit (SDK)
that programmers use to create Java programs.

Array

Special constructs in Java that allow you to store a list of items of the same data type.

Border

A special class that determines the appearance of the outer edge of components.

Border layout

Copyright © DigitalThink, Inc. All Rights Reserved.


JavaTM 2 GUI Fundamentals with Swing

The default layout manager for the content pane of frames. The border layout arranges
components in the geographic directions north, south, east, and west, along with another
component in the center.

Box layout

A layout manager that provides a means of arranging components horizontally or vertically


next to each other. What makes the box layout so powerful is its support for nesting and its
ability to fine-tune the spacing between components; you can create very intricate GUIs using
nested box layouts.

Branch statement

Java supports two different kinds of branch statements. The if-else is the most commonly used,
and it is useful in situations where you need to conditionally execute one section of code or
another. Java's other branch statement, the switch statement, is designed to choose between
multiple branches of code.

Break statement

The break statement is used to exit out of a branch or loop.

Buffered I/O

Buffered I/O is a special type of I/O where data is written to a temporary buffer instead of to an
actual stream. When the buffer is full, all of the data in the buffer is written to the stream.
Buffered I/O is more efficient than non-buffered I/O since data is transferred to and from a
physical device in large blocks.

Button

A GUI component that allows the user to invoke a command or set a value. Regular buttons are
used to invoke commands, while radio buttons, toggle buttons, and check boxes provide a
means of setting a value.

Bytecode

The executable form of Java code, which is capable of being executed in the Java runtime
system.

Card layout

Copyright © DigitalThink, Inc. All Rights Reserved.


JavaTM 2 GUI Fundamentals with Swing

A layout manager that arranges multiple panels so that they act as a stack of cards. Only one of
the panels is visible at any given time when you use the card layout. The card layout is handy
for creating wizard-style interfaces, where the user is presented with a sequence of GUIs
implemented as card panels.

Catch clause

The catch clause is the part of the try-catch construct that is used to catch any exceptions that
may be thrown by code in the try clause.

Chooser

A specialized dialog box designed to retrieve information from the user within the context of a
commonly performed task. For example, Swing includes a file chooser that provides a powerful
GUI for browsing for files, along with a color chooser for selecting colors.

Class

A template that defines the implementation of an object, effectively acting as a blueprint for the
object. A class defines data and methods and is a unit of organization in a Java program.

Client

A client is a program that requests and displays information from a special program called a
server. This type of information exchange between a client and server is known as a
client/server architecture.

Combo box

A GUI component that allows the user to select an item from a list. Unlike lists, combo boxes
only display a single item; multiple items are revealed in a drop-down window when you
activate the combo box. Combo boxes also allow users to manually enter new items.

Command-line arguments

Information passed into a command-line application that somehow controls the way the
application runs.

Comments

Comments are used to document code for the purposes of the programmer. Java supports three
different types of comments: //, /* */, and /** */.

Copyright © DigitalThink, Inc. All Rights Reserved.


JavaTM 2 GUI Fundamentals with Swing

Component

A component is an object that performs a function within a graphical user interface (GUI).
Examples of Java components include buttons, combo boxes, menus, and toolbars.

Compound statement

A compound statement is a block of code that is surrounded by curly braces ({ }). Compound
statements allow you to use multiple lines of code in situations where a single statement is
expected. A good example is the code executed in the branch of an if statement.

Conditional operator

The conditional operator acts a like a shorthand if-else statement. It evaluates a test expression
and conditionally evaluates another expression based on the test.

Container

A component that serves as a context for grouping together other components. Examples of
Java containers include panes, toolbars, and dialog boxes.

Data types

Data types dictate the type of information that can be stored in a variable. Java supports a wide
range of simple data types such as int, long, float, boolean, and char. Composite data types in
Java are implemented as objects. An example of a composite data type is String.

Delegation event model

The event model employed by Java 2, which involves registering event listeners with event
sources in order for events to be delivered. This model is very efficient since the only events
that are delivered are those for which a listener has specifically been registered.

Dialog box

A special window that provides a means of displaying a "temporary" user interface that displays
or retrieves information, and then goes away. Constructing a custom dialog box GUI is very
similar to constructing an application GUI through the content pane of a frame window.

Event

Something that occurs within an object that an application or other object might want to know
about and to which it might possibly react.

Copyright © DigitalThink, Inc. All Rights Reserved.


JavaTM 2 GUI Fundamentals with Swing

Event adapter

A helper class that makes the handling of events much cleaner. More specifically, an event
adapter alleviates the need to implement all of the event response methods in an event listener
interface, allowing you to implement only the methods you need.

Event listener

An object capable of responding to an event. You connect an event listener to an event source
in order to receive and respond to events.

Event response method

A special method that is called in response to an event.

Event source

An object capable of generating an event. Most GUI elements are event sources since they
typically generate events in response to user interactions.

Event-driven programming

A programming architecture in which a program responds to events, often generated by the


user, as opposed to following a strictly sequential flow. Event-driven programming in Java
involves the use of event listeners and sources.

Exception

An exceptional situation that a program doesn't know how to handle is known as an exception.
An example of an exception is attempting to divide a number by zero or the system running out
of memory.

Expression

An expression usually involves an equal sign ( = ) and somehow manipulates one or more
variables or values.

Finally clause

The finally clause is used in conjunction with the try-catch construct to place code that is
always executed regardless of whether an exception is thrown.

Flow layout

Copyright © DigitalThink, Inc. All Rights Reserved.


JavaTM 2 GUI Fundamentals with Swing

The default layout manager for panels. The flow layout arranges components across a container
in rows from left to right.

Frame

A container that serves as the main window for an application. Since they serve as main
application windows, frames support familiar UI window features such as a title, border, and
buttons for sizing and closing the frame.

Glue

Used in the box layout to maximize the space between components in one direction (horizontal
or vertical).

Graphical User Interface (GUI)

The program elements involved in communicating with the user through visual cues, typically
utilizing both the keyboard and mouse.

Graphics class

The Graphics class represents an abstract drawing surface called a graphics context, and is
logically equivalent to a piece of paper. Java uses graphics contexts to help abstract the drawing
process, which allows you to use the same drawing code to draw to different graphical devices
such as monitors and printers.

Graphics context

A graphics context is an abstract drawing surface that is logically equivalent to a piece of paper.

Graphics Interchange Format (GIF)

GIF is an image format that is useful for storing non-photographic images such as illustrations
and diagrams; GIF image files typically have a .gif file extension.

Graphics primitives

Graphics primitives are basic geometric shapes such as lines, rectangles, squares, ovals, circles,
polygons, and arcs.

Grid bag layout

Copyright © DigitalThink, Inc. All Rights Reserved.


JavaTM 2 GUI Fundamentals with Swing

A layout manager that provides the most flexibility of all the layouts. The grid bag layout is
similar to a grid layout except that you have a lot more control over the size and position of the
cells in the grid. For example, in a grid bag layout you can specify that some cells span multiple
columns and also control the physical arrangement of components within specific cells in the
grid.

Grid layout

A layout manager that arranges components in a grid of rows and columns. Like the flow
layout, the grid layout arranges components from left to right but they conform to a fixed
arrangement of rows and columns.

Heavyweight component

A component that has an underlying native component, or peer, associated with it. For example,
an AWT button is a heavyweight component because it uses an underlying Windows peer when
running on the Windows platform.

Hypertext Markup Language (HTML)

The formatting language used to create Web pages.

Icon

An image that provides visual cues associated with GUI features. Icons are regularly used with
Swing menu items and buttons, as well as other components.

Identifier

Tokens that represent unique names for variables, methods, classes, packages and any other
named Java construct.

Inheritance

The ability to derive new classes from existing classes in object-oriented programming.

Inner class

A class defined within the scope of another class. Inner classes are often used with event
adapters to make event handling cleaner.

Interface

Copyright © DigitalThink, Inc. All Rights Reserved.


JavaTM 2 GUI Fundamentals with Swing

An interface is a set of methods that Java classes can implement.

Internet Protocol (IP) address

An IP address is a number that is used to uniquely identify computers connected to the Internet.

Invisible filler

Used in the box layout to create space between components.

Java 2 Software Development Kit (SDK)

A standard suite of tools, utilities, and related resources used to build Java applets and
applications.

Java compiler

The tool used to compile Java source code into executable Java programs.

Java Interpreter

A program responsible for translating Java bytecode into native code that can execute on a
given platform.

Joint Photographic Experts Group (JPEG)

JPEG is an image format that provides a highly efficient means of storing photographic images;
JPEG image files typically have a .jpg or .jpeg file extension.

Keywords

Keywords are special identifiers set aside as Java programming constructs. Typical keywords in
Java include int, class, for, while, new, private, and switch to name a few.

Label

An output-only component used to present both text and graphical information to the user. You
often see labels used in conjunction with input components such as text edit fields.

Layout manager

Copyright © DigitalThink, Inc. All Rights Reserved.


JavaTM 2 GUI Fundamentals with Swing

A layout manager determines how components are physically arranged within a container. A
layout manager is often referred to simply as a layout.

Lightweight component

A peerless component. A lightweight component has no underlying native component. The


primary benefit to lightweight components is that they are 100% Java, which alleviates
inconsistencies across different platforms. Most of the components in the Swing toolkit are
lightweight components, as opposed to the heavyweight AWT components.

List

A GUI component that allows the user to select one or more text and/or icon items from a list.

Literals

Literals are constant values such as numbers and strings.

Loop

There are three types of loops used in Java: for loops, while loops, and do loops.

Low-level event

An event fired in response to a low-level input or graphical user interface interaction such as a
key press or mouse click.

main() method

The method within a Java application that is called by the Java interpreter to run the
application.

Menu

A GUI component that provides a hierarchical user interface for selecting commands and
altering settings within an applet or application. A Swing menu consists of a menu bar that is a
container for individual drop-down menus; these menus contain individual menu items that
represent commands or settings.

Message

Copyright © DigitalThink, Inc. All Rights Reserved.


JavaTM 2 GUI Fundamentals with Swing

In object-oriented programming, sending a message to an object to invoke a method is similar


to calling a procedure in structured programming languages.

Method

An isolated section of code in an object that performs a particular function.

Mnemonic

A keyboard user interface used to navigate through the hierarchy of a menu and select a menu
item or invoke a menu command. A mnemonic is often used in conjunction with the Alt key.
An underlined character in the name of a menu item visually identifies a mnemonic.

Multicast event delivery

When an event is broadcast to multiple event listeners simultaneously. Multiple event listeners
can be registered with a multicast event source.

Multithreading

Multithreading is the capability of a program to have multiple paths of execution called threads.

Object

A collection of data and the procedures (methods) that act on that data.

Object class

The class that forms the root of the Java class hierarchy. The Object class serves as the basis for
all Java classes, including the classes in the Java API.

Object-oriented programming (OOP)

The implementation of a program or algorithm using objects.

Operator

A programming construct that performs an evaluation or computation on a data object or


objects.

Option pane

Copyright © DigitalThink, Inc. All Rights Reserved.


JavaTM 2 GUI Fundamentals with Swing

A standard dialog box that makes it easier to accomplish certain tasks. An option pane can often
save you from the task of constructing a custom dialog box GUI.

Overload

To use the same name for several items in the same scope.

Override

To replace an inherited method with a newer version.

Package

A package is a collection of reusable classes and interfaces.

Pane

Generic Swing containers. Multiple panes are used in Swing to create an architecture that can
support rich GUI features.

Parameter

A name and value pair identified by the name and value attributes of the PARAM element used
inside an APPLET element.

Pluggable look and feel

A feature of Swing that allows you to set the look and feel of a GUI dynamically. By utilizing a
pluggable look and feel, a Swing GUI is capable of querying the underlying native GUI and
taking on its look and feel. The look and feel of a GUI comprises such graphical parameters as
color schemes, component styles, and 3D shading effects, to name a few.

Progress bar

Provides a visual representation of the status of an incomplete process. For example, you might
display a progress bar while loading images in an applet.

Protocol

A protocol is a set of rules and standards that govern how network communication works.
Protocols determine how computers communicate with each other over a network.

Copyright © DigitalThink, Inc. All Rights Reserved.


JavaTM 2 GUI Fundamentals with Swing

Rigid area

Used in box layouts to create fixed-size spaces between components in one direction
(horizontal or vertical).

Semantic event

An event fired in response to an action which is based on the semantics of a particular object,
such as an item change in a list.

Separators

Separators are symbols used to inform the Java compiler of how code elements are grouped.

Server

A server is a program that acts as an information source to special programs called clients. This
type of information exchange between a client and server is known as a client/server
architecture.

Slider

A GUI component that allows the user to graphically select a value by sliding a knob within a
bounded range of values.

Socket

A socket is a "fitting" used to connect a stream of network data to a Java program.

Standard I/O

Standard I/O is the most basic form of input and output available to a Java program. Java
standard I/O is text-based, and operates under the premise that the computer has a standard
input device (keyboard) and a standard output device (monitor).

Stream

A stream is a medium through which data is transferred. A stream acts sort of like a pipe in the
real world, except that it shuttles moving data instead of water or gas. Data is transferred
through a stream one byte at a time, and can be directed in different ways.

String

Copyright © DigitalThink, Inc. All Rights Reserved.


JavaTM 2 GUI Fundamentals with Swing

A Java object that represents textual information. Although you can think of a string in Java as
a series of characters, strings are implemented in the String class.

Swing toolkit

A set of classes and interfaces that serve as an extension to the Java AWT. Swing provides
replacements for many of the classes in the AWT. In general, Swing classes and interfaces are
more extensible and powerful than their AWT counterparts.

Table

A GUI component that allows the user to view and manipulate data in a two-dimensional table
format, similar to a spreadsheet.

Text component

A GUI component that allows the user to view and manipulate text.

Thread synchronization

Thread synchronization is the process of structuring threads so that they never interrupt each
other while performing sensitive tasks.

Throwing (an exception)

When an exceptional condition is encountered in a Java program, an exception is thrown, which


means that a special exception object is created and passed out of the current context. The idea
is that a higher context will be more capable of handling the exception.

Token

The smallest code element in a program that is meaningful to the compiler.

Tool tip

A small text window that appears when the user pauses with the mouse cursor over a
component. Tool tip windows are designed to be unobtrusive. The text associated with a tool tip
is typically very concise.

Toolbar

A GUI component that serves as a container for buttons or other components, and is typically
positioned along a bar at the top of an application's main window frame. Toolbars can be

Copyright © DigitalThink, Inc. All Rights Reserved.


JavaTM 2 GUI Fundamentals with Swing

positioned anywhere in an application's frame but the top is the most popular location.

Tree

A GUI component that allows the user to view and manipulate a set of hierarchical data as an
outline.

Try clause

The try clause is the part of the try-catch construct where code is placed that can potentially
throw an exception. You then catch any exceptions that may be thrown in one or more catch
clauses.

Unicast event delivery

When an event is broadcast to a single event listener. Only one event listener can be registered
with a unicast event source.

Uniform Resource Locator (URL)

A URL is a text address used to identify sites on the Internet.

Variables

Variables are locations in memory that are used to store information. You can think of variables
as storage containers designed to hold data of a certain type.

Z-order

The Z-order of a layered pane is a number that indicates the depth of the layer with respect to
the screen. In other words, the screen is the highest layer with a theoretical Z-order of infinity,
while other layers have decreasing Z-orders behind the screen. Z-order is what allows you to
layer panes on top of one another.

Copyright © DigitalThink, Inc. All Rights Reserved.


Lesson 1.1: Course introduction

Course introduction
Java 2 GUI Fundamentals with Swing teaches you
how to construct graphical user interfaces (GUIs) in
Java.
You’ll use the Swing toolkit in Java 2, which
provides a rich set of classes and interfaces for
creating very powerful and intuitive GUIs.

Along the way, you'll design and develop an


application as a course project. This project will give
you hands-on experience with the GUI skills you'll
learn.

Course After completing the course, you will have the skills
goals and knowledge necessary to:
● Handle mouse and keyboard events

● Build graphical user interfaces with Swing

● Use Swing components such as buttons,


borders, progress bars, trees, and icons
● Use option panes to display information

● Create and use menus

● Use standard dialog boxes

● Set the pluggable look and feel of a user


interface
● Use keyboard mnemonics

● Add sound support

First-time students: To get the most out of this


course, take the brief Orientation tour.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module1/intro.html [1/11/2000 1:17:59 PM]


Lesson 1.2: Prerequisites

Prerequisites
Make sure you have the necessary
background for this course.

In order to get the most out of this course, you


should have a solid understanding of Java and
experience using fundamental parts of the Java
programming language and API. You should also be
comfortable developing and compiling Java applets
and applications.

For example, you should be able to create


multithreaded programs and handle exceptions, as
well as use OOP constructs such as classes,
packages, and interfaces. You should also know how
to embed applets in Web pages using the HTML
<APPLET> tag.

If you don't have these Java skills, you might consider


taking the first two courses in the Introduction to Java 2
series: Java 2 Fundamentals I and Java 2 Fundamentals II.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module1/prereqs.html [1/11/2000 1:17:59 PM]


DigitalThink : Smart Companies Get It

Copyright © 1996, 1997, 1998, 1999, 2000 DigitalThink,


Inc.
This site contains materials created, developed, or
commissioned by DigitalThink, Inc. and is protected by
international copyright and trademark laws. No material
(including but not limited to the text, images, audio, and/or
video) and no software (including but not limited to any
images or files incorporated in or generated by the software)
may be copied, reproduced, republished, uploaded, posted,
transmitted, or distributed in any way or decompiled, reverse
engineered, or disassembled, except that one copy may be
downloaded for your personal, non-commercial use on a
single computer. In connection with such use, you may not
modify or obscure any copyright or other proprietary notice.
DigitalThink® and the DigitalThink logo are trademarks and
service marks of ours. All company names, trademarks,
service marks and trade names referred to in this course are
the property of their respective owners.
If you have any questions about these terms, or if you would
like information about licensing materials from DigitalThink,
please contact us via email: [email protected].
Legal Info:
● Course License

DigitalThink is a trademark of DigitalThink, Inc.

Log In | Sitemap | Contact


Home | Corporate e-Learning Solutions | Course Catalog | About DigitalThink | Support
Copyright © 1996-2000 DigitalThink, Inc. All Rights Reserved. Privacy Policy

http://www.staging.digitalthink.com/copyright.html [1/11/2000 1:18:00 PM]


Lesson 1.3: What you need

What you need


Find out what you need to take this course.

You will need the following materials to take this


course:

Software In order to work through the example programs


presented throughout the course, you'll need to
download and install the Java 2 Software
Development Kit (SDK) Standard Edition or some
other suitable development environment with a Java
compiler. Any development environment you use
must be Java 2 compliant. For Java 2 SDK download
and installation instructions, please refer to the
Resources page by clicking the Resources button on
the toolbar.

There currently isn't a release of the Java 2 SDK available


for Macintosh computers. For now, if you're using a Mac,
you can work through the course and follow along by
simply studying the examples.

The Java 2 Runtime Environment (JRE) allows users to


install a plug-in that lets their browser run Java 2 applets.
Click the Resources button on the toolbar for more
information.

file:///c|/courses/jv301_r/module1/need.html (1 of 2) [1/11/2000 1:18:00 PM]


Lesson 1.3: What you need

Resources Visit the course Resources page by clicking the


page Resources button on the toolbar.
Course downloads
The Java source code, executables, and graphics for
the course are available for download from the
Resources in a compressed archive file.

This file is provided in forms suitable for Windows,


Mac and Unix platforms, and should be downloaded
at some point so that you can successfully follow
along and perform the exercises.
Course PDF
You can download a compressed file from the
Resources page that contains a PDF file of the entire
course. This PDF file provides the lesson, quiz, and
exercise content so you can read and review the
course material offline at your convenience. The
PDF file is not required to complete this course.
You will need Adobe Acrobat Reader 3.0 or greater
to view a PDF file. If you don't have Acrobat Reader,
you can download it from the Adobe web site.

Other resources
A listing of links to online Java resources are
available on the Resources page. You can reach this
page at any time by clicking the Resources button on
the toolbar.

Course bookstore
Purchase the optional texts for this course online by
visiting the course Bookstore page. These books are
not required to take this course, but do contain
helpful, additional information. You can reach the
Bookstore page at any time by clicking the
Resources button on the toolbar.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module1/need.html (2 of 2) [1/11/2000 1:18:00 PM]


IT Course Catalog : DigitalThink : Smart Companies Get It

Introduction to Java 2 Series


Java 2 Fundamentals I
Price: $325 Course Duration: 12 hours CEUs: 1.5 Course Syllabus

Sampler Courses "Your exercises show the true power of the language and are easy to
C, C++ Programming follow. Great job!!"
Database Concepts -Andy Erickson, Cincinnati, OH
Design & Publishing
Desktop Course Description
IT Management Java 2 Fundamentals I provides moderately experienced programmers
Internet Literacy with a foundation in Java basics and prepares them to learn more
Java Programming advanced aspects of the Java language. Students install and use Sun's
Lotus Java 2 platform, explore stand-alone applications and Web-hosted
MS Certification:MCDBA
applets, learn about Java's object-oriented building blocks, and work
MS Certification:MCSD
MS Certification:MCSE
with Java's operators and control flow statements.
Object Programming
Networking
This course covers fundamentals of Java programming and does not
Oracle
address material that is specific to the Java 2 platform. Therefore,
UNIX Java 2 Fundamentals I is not an upgrade course for those who already
Web Programming understand the fundamentals of Java programming using the JDK
Windows Programming 1.0.2 or 1.1.

Learning Objectives
After completing this course, students will be able to:
● Use many of the tools in the Java 2 platform
● Implement Java classes, objects, and methods
● Use Java data types, expressions, and control flow structures
● Develop stand-alone character-based applications
● Develop and embed Java applets in Web pages using HTML

Prerequisites
In order to get the most from this course, students should have an
understanding of computer programming and experience with a
programming language such as C or C++. Object-oriented
programming experience isn't necessary, but will make learning Java a
little easier.

Note that this is not a beginning course and is not a course designed
for non-programmers. In addition, this course is not an upgrade
course for those who already understand the fundamentals of Java
programming using the JDK 1.0.2 or 1.1.
Required Software
● The Java 2 platform

Platform Support
You can take this course on computers running Windows, Macintosh,
or UNIX. To complete the exercises, however, you must have access
to a computer running Windows or UNIX; Sun does not support a
Macintosh version of Java 2.
Instructor

http://www.staging.digitalthink.com/catalog/jv/jv101/ (1 of 2) [1/11/2000 1:18:01 PM]


IT Course Catalog : DigitalThink : Smart Companies Get It

Michael Morrison, instructor for the Introduction to Java 2 Series,


JavaBeans for Programmers Series and Win32 Programming Series, is
an author and contributor to many Java-related books including
Complete Idiot's Guide to Java (Que), Java 1.1 Unleashed (Sams),
How To Program JavaBeans (Ziff-Davis Press), Presenting JavaBeans
(Sams), and Teach Yourself Internet Game Programming With Java in
21 Days (Sams).
Course Access
Each student has access to the course for a period of 6 months from
the day that a registration is activated. Please read our Courseware
License for more details.

Log In | Sitemap | Contact


Home | Corporate e-Learning Solutions | Course Catalog | About DigitalThink | Support
Copyright © 1996-2000 DigitalThink, Inc. All Rights Reserved Privacy Policy

http://www.staging.digitalthink.com/catalog/jv/jv101/ (2 of 2) [1/11/2000 1:18:01 PM]


IT Course Catalog : DigitalThink : Smart Companies Get It

Introduction to Java 2 Series


Java 2 Fundamentals II
Price: $325 Course Duration: 10 hours CEUs: 1.0 Course Syllabus

Sampler Courses "Teaching a language such as Java lends itself well to this type of
C, C++ Programming self-paced instruction. I was impressed with the level of tutorial
Database Concepts competence and activity in the course."
Design & Publishing -Jim Cant, Ontario, Canada
Desktop
IT Management Course Description
Internet Literacy Java 2 Fundamentals II picks up where Java 2 Fundamentals I leaves
Java Programming off by rounding out the Java knowledge base required for beginning
Lotus Java programmers. Students dig deeper into object-oriented
MS Certification:MCDBA
programming with Java and learn the basics of Java graphics.
MS Certification:MCSD
MS Certification:MCSE
Additionally, the course covers multithreading, exception handling, file
Object Programming
input and output (I/O), and networking basics.
Networking
Oracle
This course covers fundamentals of Java programming and does not
UNIX address material that is specific to the Java 2 platform. Therefore,
Web Programming Java 2 Fundamentals II is not an upgrade course for those who
Windows Programming already understand the fundamentals of Java programming using the
JDK 1.0.2 or 1.1.
Learning Objectives
After completing this course, students will be able to:
● Organize Java classes into packages
● Leverage the use of interfaces in the design of Java classes
● Use the Java AWT to draw primitive graphics, text, and images
● Create and use threads
● Catch program errors using exceptions
● Read and write files
● Use Java's networking features to scan the Web
Prerequisites
Java 2 Fundamentals I, or equivalent knowledge and experience.
Required Software
● The Java 2 platform

Platform Support
You can take this course on computers running Windows, Macintosh,
or UNIX. To complete the exercises, however, you must have access
to a computer running Windows or UNIX; Sun does not support a
Macintosh version of Java 2.
Instructor
Michael Morrison, instructor for the Introduction to Java 2 Series,
JavaBeans for Programmers Series and Win32 Programming Series, is
an author and contributor to many Java-related books including
Complete Idiot's Guide to Java (Que), Java 1.1 Unleashed (Sams),
How To Program JavaBeans (Ziff-Davis Press), Presenting JavaBeans

http://www.staging.digitalthink.com/catalog/jv/jv201/ (1 of 2) [1/11/2000 1:18:02 PM]


IT Course Catalog : DigitalThink : Smart Companies Get It

(Sams), and Teach Yourself Internet Game Programming With Java in


21 Days (Sams).
Course Access
Each student has access to the course for a period of 6 months from
the day that a registration is activated. Please read our Courseware
License for more details.

Log In | Sitemap | Contact


Home | Corporate e-Learning Solutions | Course Catalog | About DigitalThink | Support
Copyright © 1996-2000 DigitalThink, Inc. All Rights Reserved Privacy Policy

http://www.staging.digitalthink.com/catalog/jv/jv201/ (2 of 2) [1/11/2000 1:18:02 PM]


Lesson 1.4: What to expect

What to expect
Explore key course features.

Glossary
Throughout this course, you'll have the opportunity
to review definitions of key terms. Many terms will
link to their definition, which will appear in a small
window.

In addition, glossary links for key terms appear in


each module wrap-up lesson. To get a feel for how
this works, click the word glossary now.

Occasionally in this course, you'll need to examine


screenshots or other images that are larger than our
standard lesson pages. When this is necessary, you'll
see a View Example button in the left margin. Click
this button to open a new Web browser window and
view the image. Try it now by clicking the View
Example button to the left.

Exercise After completing some exercises, you will be


submissions required to cut and paste your source code into a text
submission box. Copying and pasting is generally
easy to accomplish, but sometimes on Unix
platforms, this may require a workaround.

Click the Syllabus button in the toolbar to easily navigate


between lessons and modules. Clicking it will open a
smaller browser window that displays a list with hyperlinks
to this course's lessons, exercises, and quizzes. The red
arrow indicates where you are in the course.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module1/expect.html [1/11/2000 1:18:02 PM]


Resources - Java 2 GUI Fundamentals with Swing

Software | Optional books | Downloads | Internet resources | SDK installation


Course Java 2 GUI Fundamentals with Swing
Introduction to Java 2 Series

Instructor Michael Morrison


Tutors To send a message to one of the course tutors, click
the Tutor button on the toolbar.
Return To return to the course, click the Syllabus button on
to course the toolbar and look for the red arrow. This is the
page you last visited in the course.
Required Java 2 Software Development Kit, Standard Edition
software
The SDK is available as a free download from the
Back to top
Java 2 Platform Standard Edition Web page hosted
by Sun.
For information on how to install the Java 2 Software
Development Kit, Standard Edition please see below.

Recommended The Java 2 Runtime Environment (JRE) Standard


software Edition allows users to install a plug-in that lets their
Back to top browser run Java 2 applets.
Check out the Java 2 Platform Standard Edition Web
page for more information.

Recommended Sun currently supports the Java 2 SDK on Windows


hardware 95/98/NT and on the Solaris platform. If you are a
Mac or non-Solaris UNIX user you may not be able
to compile and test the example Java code in this
course. For now, if you're using a Mac or other
non-Solaris UNIX machines, you can work through
the course and follow along by simply studying the
examples.

http://www.staging.digitalthink.com/resources/courses/jv301/ (1 of 5) [1/11/2000 1:18:03 PM]


Resources - Java 2 GUI Fundamentals with Swing

Optional books Pure JFC Swing


These books are not
by Satyaraj Pantham
required to complete
this course.
Graphic Java 1.2, Mastering the JFC: Swing,
Vol. 2
Back to top by David M. Geary
These optional books are available for purchase
through our online Bookstore.

Downloads Exercise downloads


Please be sure to Macintosh: jv301_exercises.sea.hqx (~49SKB)
download the
appropriate files for Windows: jv301_exercises.zip (~15KB)
your platform. UNIX: jv301_exercises.tar.gz (~9KB)
Back to top
Explanation: These compressed files contain the
files you will need to complete the exercises in this
course.
Course PDF
Macintosh: jv301.pdf.sea.hqx (410KB)
Windows: jv301pdf.zip (807KB)
UNIX: jv301.pdf.gz (280KB)
Explanation: These compressed files contain a PDF
file of the course so you can read and review the
lesson, exercise, and quiz content offline at your
convenience. You will need Adobe Acrobat Reader
version 3.0 or greater to read this file. If you don't
have Acrobat Reader, you can download it from the
Adobe Web site.

Internet resources Swing-specific sites


Note: these links will ● The Swing Connection
launch a new browser
window.
● Sun Java tutorial: Creating a GUI with
JFC/Swing
Back to top

Visual Development Tools


● Symantec's Visual Café

● Metrowerks Code Warrior Gold


● Kawa
● Sun Java Workshop

http://www.staging.digitalthink.com/resources/courses/jv301/ (2 of 5) [1/11/2000 1:18:03 PM]


Resources - Java 2 GUI Fundamentals with Swing

● Microsoft's Visual J++


● JBuilder

Java Technologies
● General Java info

● JavaBeans
● Java Foundation Classes
● Microsoft SDK for Java
● Java Commerce Toolkit
● HotJava Browser
● JavaOS
● Jini

Java 2 SDK Installing the Java 2 SDK (Windows)


installation
Once you've downloaded the Java 2 SDK for
Back to top
Windows from Sun's Java Web site, all you need to
do is execute the downloaded file to install the SDK.
So, if the name of the downloaded file is
SDK12-win32.exe, for example, then you just
double-click the file in Explorer or enter the
following at the MS-DOS command line:
SDK12-win32
The SDK installation will continue, prompting you a
few times to clarify options such as the installation
directory. You can accept all of the default settings
for installing the SDK. After the SDK finishes
installing, you need to set the PATH variable as
mentioned in Configuring the SDK. There are three
ways to do this in Windows:
1. Use the set command from an MS-DOS
command line
2. Use the set command in the autoexec.bat file
3. Edit the environment variables using the
Environment tab in the System control panel
(Windows NT)
I recommend the second approach if you're using
Windows 95/98, or the third approach if you're using
Windows NT. The first approach is temporary and

http://www.staging.digitalthink.com/resources/courses/jv301/ (3 of 5) [1/11/2000 1:18:03 PM]


Resources - Java 2 GUI Fundamentals with Swing

won't serve you too well in the long run. Following


is the line in my autoexec.bat file that sets the PATH
environment variable:
set PATH=%PATH%;c:\SDK1.2\bin
This code actually results in the SDK's bin directory
being appended to my existing PATH variable. Just
be sure to change the SDK directory to reflect the
exact version of the SDK you have installed; my
example PATH setting is based on the initial release
of Java 1.2.
Installing the Java 2 SDK (Solaris)
After downloading the Solaris Java 2 SDK from
Sun's Java Web site, you must execute the
downloaded file to install the SDK. You can
accomplish this by entering the filename at a
command line. The SDK installation will then begin,
prompting you a few times to clarify options such as
the installation directory. You can accept all of the
default settings for installing the SDK.
After the SDK finishes installing, you need to set the
PATH variable as mentioned earlier in the lesson.
There are two ways to do this in Solaris:
1. Use the setenv command from a command
line
2. Use the setenv command in your shell
startup file
I recommend the second approach since the first
approach is temporary and probably won't serve your
purposes in the long run.
Configuring the SDK
Both the Windows and Solaris versions of the SDK
are distributed as self-installing applications.
However, there are two environment variables,
PATH and CLASSPATH, that impact the SDK tools.
● The PATH environment variable is referenced
by your operating system to determine where
to look for executable applications. The bin
directory within the SDK installation should
be added to the PATH variable so that all of
the SDK tools can be executed from any

http://www.staging.digitalthink.com/resources/courses/jv301/ (4 of 5) [1/11/2000 1:18:03 PM]


Resources - Java 2 GUI Fundamentals with Swing

directory.
● The CLASSPATH environment variable is
used to notify SDK tools and some other Java
programs where they can find support classes.
It isn't necessary to set the CLASSPATH
environment variable for this course.

http://www.staging.digitalthink.com/resources/courses/jv301/ (5 of 5) [1/11/2000 1:18:03 PM]


Adobe Acrobat Reader

To view these pages properly, and to shop in our store,


your browser needs to support JavaScript. This may
mean that you need to upgrade to Netscape
Communicator 4.0 or later, or Microsoft Internet Explorer
4.0 or later. Also, in your browser Preferences, enable
JavaScript. Please see our error page for more
information.
Products Acrobat

Adobe® Acrobat® Reader is free, and freely distributable, software


that lets you view and print Adobe Portable Document Format (PDF)
files. With Acrobat Reader, you also can fill in and submit PDF forms
online, as well as download encrypted content from the Web and
unlock it with Web Buy. This Adobe technology is a fully integrated
Important
component of Adobe Acrobat and Acrobat Reader (version 4.05,
Information
English only). If you want to create or annotate Adobe PDF files,
• Product details check out our Acrobat product family.
• How to distribute
Note: You will receive the most recent version of Acrobat Reader that
Acrobat Reader is available for your language and platform. This means you will
• System receive version 3 if Acrobat Reader 4.0 has not yet been released for
requirements your language or platform. Step 3 displays the version and file size of
• Troubleshooting the downloaded file based on the selections you make in Step 1.

Other Options
• What is Web Buy?
• Asian Font Packs
for Acrobat Reader Select an Acrobat Please provide your Now download and
4.0 Reader version. name and e-mail install - it's just that
• Acrobat Reader 4.0 address. easy!
Language:
Middle Eastern Name: The version of Acrobat
English
• Acrobat Reader Reader you are
and Acrobat Platform: downloading:
Reader + Search Win 95 E-mail address: The download file size is:
4.0 CD-ROM
Location nearest you:
• Acrobat Viewer
USA
(Java) ✔ Notify me about
Include option for Adobe software and
searching PDF files offers
✔ Allow others to send
(longer download)
me special offers
Macintosh: .bin or .hqx? Full privacy policy

If you have trouble downloading, or need a 3.x version of the Reader, try this
alternate source.

http://www.adobe.com/products/acrobat/readstep.html (1 of 2) [1/11/2000 1:18:06 PM]


Adobe Acrobat Reader
Copyright ©1999 Adobe Systems Incorporated. All rights reserved.
Terms of Use
Online Privacy Policy

http://www.adobe.com/products/acrobat/readstep.html (2 of 2) [1/11/2000 1:18:06 PM]


Bookstore - Java 2 GUI Fundamentals with Swing

The Bookstore lets you purchase course texts directly over the
Internet. Working with Amazon.com, the most respected
name in online booksellers, we supply you with everything
you need to purchase your books right now. If you prefer, you
can also use this information to find the books at your local
bookstore.

Course Java 2 GUI Fundamentals with Swing


Introduction to Java 2 Series

Optional books Pure JFC Swing


These books by Satyaraj Pantham
provide useful Paperback, 832 pages
information, but Published by Sams
they are not Publication date: October 1998
required to
successfully ISBN: 0672314231
complete the
course. Graphic Java 1.2, Mastering the JFC: Swing, Vol. 2
by David M. Geary
Paperback, 1622 pages
Published by Prentice Hall
Publication date: July 1998
ISBN: 0130796670

http://www.staging.digitalthink.com/resources/bookstore/jv301.html [1/11/2000 1:18:06 PM]


Lesson 1.5: The course tutors

The course tutors


Send email to the course tutors.

The course tutors are the people who will review all
your exercise submissions and send you personal
email in response. You can also email the course
tutors whenever you have a question or get stuck in a
lesson or exercise.

Send email to the To introduce yourself to the tutors, write an


tutors introductory email that briefly describes your
programming experience. You can also explain what
you hope to gain from this course.
Let's walk through the process of sending email to
the course tutors:
1. Click the Tutor button on the toolbar to the
left. The WebMail page appears.
2. Type Student introduction in the
Subject field.
3. Type your email message in the Message field.
4. Click the Send Message button. The WebMail
response page appears.

5. Click the browser Back button twice to return


to this page.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module1/tutors.html [1/11/2000 1:18:06 PM]


file:///c|/courses/jv301_r/module1/images/sampleviewimage.gif

file:///c|/courses/jv301_r/module1/images/sampleviewimage.gif [1/11/2000 1:18:06 PM]


Sidebar: Copying and pasting on Unix platforms

What to expect
Copying and pasting on Unix
platforms
All of the text files you need to get started on course
exercises are included in the download file available
on the Resources page. Open the text file in your text
editor and make any modifications necessary to
complete the exercise. Save the file under a different
name.

From text Once you have the exercise solution in a text file,
editor to you're ready to submit your solution to the course
browser tutors.

Some configurations of Unix operating systems


make it difficult or impossible to copy and paste
between applications. To work around this, you'll
need to open your exercise solution text file in your
Web browser, select and copy the text, then paste it
into the exercise submission text area of the
DigitalThink learning environment.

How to... If you find you cannot cut and paste text from one
application to another, follow the steps listed below.
Please note that DigitalThink supports only Netscape
Navigator 3.x or above on Unix platforms. The
instructions refer to Navigator 4.x, but instructions
for doing this procedure in Navigator 3.x are very
similar:
1. When you are ready to submit an exercise
solution, navigate to the appropriate exercise
page in the course using the course Syllabus.
You'll notice a text box in which you'll want to
paste your exercise solution.
2. From the File menu in Navigator, choose the
Open Page option. The Open Page dialog box
appears.
3. Click the Choose File button. The Choose File
dialog box appears.

file:///c|/courses/jv301_r/module1/unix.sb.html (1 of 2) [1/11/2000 1:18:07 PM]


Sidebar: Copying and pasting on Unix platforms

4. Navigate to the appropriate directory and


select your exercise solution text file. The
name of your text file will now appear in the
Open Page dialog box.
5. Click the Open in Navigator button. The text
file will now appear in the browser window.
6. Select the text of your exercise solution and
choose the Copy option from the Edit menu.
7. Click the browser's Back button to return to
the course Exercise page.

8. Position your mouse cursor in the exercise


submission text box, click the mouse, and then
select Paste from the Edit menu. Your solution
text is now ready to be submitted to the tutors.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module1/unix.sb.html (2 of 2) [1/11/2000 1:18:07 PM]


Lesson 1.6: Meet your instructor

Meet your instructor


Get to know Michael Morrison.

Hello, my name is Michael Morrison and I'll be


your instructor for this course. I am a writer,
software developer, toy inventor, skateboarder, and
cyclist living in Nashville, Tennessee with my wife
Mahsheed.
My entry into the writing profession began with a
book covering the ins and outs of Windows 95 game
programming. Right after that I got heavily involved
Transcript with Java, which resulted in me authoring and
contributing to a wide range of books, most of which
are Java related:
● Complete Idiot's Guide to Java (Que, 1998)

● Teach Yourself More Java in 21 Days


(Sams.net, 1998)
● Java 1.1 Unleashed (Sams.net, 1997)

● Presenting JavaBeans (Sams.net, 1997)

● Teach Yourself Internet Game Programming


with Java in 21 Days (Sams.net, 1996)

Find me on I have a Web site called The Tribe. This site contains
the Web information about my interests and hobbies, as well
as resources associated with my writing projects and
software development efforts.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module1/instructor.html [1/11/2000 1:18:07 PM]


Lesson 1.7: Course project

Course project
Find out about the course project.

The course project is a Java application named


Doodle that relies on a wide range of GUI features.
Doodle is a drawing program that allows you to draw
graphics primitives in different colors.
You'll design and build this application a step at a
time as you gain new skills and learn how to work
with new GUI elements. You will be asked to submit
your project at various stages of development.

Flip through the images below to see some of the


features of the Doodle application:

Transcript

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module1/project.html [1/11/2000 1:18:07 PM]


Sidebar: Michael Morrison's introduction (Audio transcript)

Meet your instructor


Michael Morrison's introduction
(Audio transcript)
Hello, this is Michael Morrison, your instructor.
Before you dive in, I'd like to take a moment to
introduce myself and welcome you to the course. I
also want to introduce you to audio clips and how
they help liven up the course and provide additional
information about course topics.
You'll encounter audio clips such as this one
throughout the course whenever there is
supplemental information that I'd like to share with
you. These audio clips aren't really critical to your
understanding of Java GUIs, but they do sometimes
offer insights or ideas that you'll hopefully find
interesting. I encourage you to take the time to listen
to the audio clips as they do sometimes help clarify
important topics covered in the lessons.

Audio clips also serve as a good change of pace from


the written word, which can make the course easier
to work through. However, please understand that I
don't plan on singing or providing any other forms of
audio entertainment. I'll try hard to limit myself to
discussing GUIs and their relationship to Java. If for
some reason my voice gets on your nerves, you'll be
glad to know that every audio clip in the course has
an associated transcript that you can read if you
choose not to listen to the audio. That's all for now!

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module1/instructor_at.sb.html [1/11/2000 1:18:07 PM]


The Tribe Home Page

This page is under construction - sorry for the inconvenience. This page has been accessed times.

http://www.thetribe.com/ [1/11/2000 1:18:10 PM]


Lesson 2.1: Events and user interaction

Events and user interaction


Module introduction
Java provides a powerful mechanism for detecting
and responding to user interactions, or events. This
module examines events and explains how they are
used to convey information about what has occurred
in a Java program. It will also show you how to
respond to events, especially those involving the
mouse and keyboard.

Module After completing the module, you'll have the skills


learning and knowledge necessary to:
objectives ● Facilitate event delivery with event sources
and listeners
● Handle mouse events

● Create and use event adapters

● Handle keyboard events

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/intro.html [1/11/2000 1:18:10 PM]


Sidebar: Features of the Doodle application (FlipBook transcript)

Course project
Features of the Doodle application
(FlipBook transcript)
Some features of the Doodle application:

file:///c|/courses/jv301_r/module1/project_fb.sb.html (1 of 8) [1/11/2000 1:18:10 PM]


Sidebar: Features of the Doodle application (FlipBook transcript)

file:///c|/courses/jv301_r/module1/project_fb.sb.html (2 of 8) [1/11/2000 1:18:10 PM]


Sidebar: Features of the Doodle application (FlipBook transcript)

file:///c|/courses/jv301_r/module1/project_fb.sb.html (3 of 8) [1/11/2000 1:18:10 PM]


Sidebar: Features of the Doodle application (FlipBook transcript)

file:///c|/courses/jv301_r/module1/project_fb.sb.html (4 of 8) [1/11/2000 1:18:10 PM]


Sidebar: Features of the Doodle application (FlipBook transcript)

file:///c|/courses/jv301_r/module1/project_fb.sb.html (5 of 8) [1/11/2000 1:18:10 PM]


Sidebar: Features of the Doodle application (FlipBook transcript)

file:///c|/courses/jv301_r/module1/project_fb.sb.html (6 of 8) [1/11/2000 1:18:10 PM]


Sidebar: Features of the Doodle application (FlipBook transcript)

file:///c|/courses/jv301_r/module1/project_fb.sb.html (7 of 8) [1/11/2000 1:18:10 PM]


Sidebar: Features of the Doodle application (FlipBook transcript)

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module1/project_fb.sb.html (8 of 8) [1/11/2000 1:18:10 PM]


Lesson 2.2: Event fundamentals

Event fundamentals
Get acquainted with events, Java's
mechanism for handling user interaction.

Moving the mouse or pressing a key on the


keyboard is known as an event. Writing Java code to
handle events is known as event-driven
programming.
Events are used to communicate information about
the changing state of a GUI component such as a
button or checkbox. Forming a fundamental part of
GUIs, events provide the means by which
components communicate with each other.

The event model employed by Java 2 is known as the


delegation event model, which supports two different
types of events: low-level events and semantic
events.

Low-level events Low-level events are events fired in response to a


low-level input or visual user interface interaction.
These events are all-purpose and typically apply to
all components. Mouse drags and key presses are
examples of low-level events. Following are some of
the low-level event classes supported in the Java 2
API:
ComponentEventFired when a component is
resized, moved, hidden or
shown.
WindowEvent Fired when a component is
opened, closed, activated,
deactivated, iconified, or
deiconified.
FocusEvent Fired when a component
receives or loses focus.
KeyEvent Fired when a component
receives a key press or key

file:///c|/courses/jv301_r/module2/fundamentals.html (1 of 2) [1/11/2000 1:18:11 PM]


Lesson 2.2: Event fundamentals

release.
MouseEvent Fired when a component
receives a mouse button click,
mouse button release, mouse
move, or mouse drag.

Semantic Semantic events are events fired when an action


events occurs that is based on the semantics of a particular
component. Semantic events only make sense within
the context of a specific component. An example of a
semantic event is a button press event, which is
related to the specific function (semantics) of a
button. Following are a few of the semantic events
supported in the Java 2 API:
ActionEvent Fired when a generic "action"
occurs such as a button click.
AdjustmentEventFired when a value is
adjusted.
ItemEvent Fired when an item state
changes.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/fundamentals.html (2 of 2) [1/11/2000 1:18:11 PM]


Lesson 2.3: Event sources and listeners

Event sources and listeners


Facilitate event delivery with event sources
and listeners.

Events are generated by event sources, and are


capable of being handled and processed by event
listeners. Event listeners register themselves with
event sources in order to receive event notifications.
The event delivery process can be broken down into
three basic steps:

Transcript

Event Event listeners are objects that are capable of


listeners receiving and processing events. More specifically,
event listeners are interfaces that formally define
how events are received and processed by a
particular application or GUI component. Each event
listener interface corresponds to a different type of
event and defines the methods that must be provided
to handle those events.

Like event sources, event listener interfaces are


organized into low-level and semantic interfaces.
Examples of low-level event listeners include
MouseListener and KeyListener, to name a
couple. Semantic event listeners include
ActionListener and ItemListener, among
others.

file:///c|/courses/jv301_r/module2/sources.html (1 of 2) [1/11/2000 1:18:11 PM]


Lesson 2.3: Event sources and listeners

Event An event source is a GUI component capable of


sources generating events. When an event occurs within an
event source, the source is responsible for examining
registered listeners and sending event notifications.
Event listeners are registered with event sources by
calling event listener registration methods.

Event sources can be organized according to


low-level and semantic events. Some of the standard
Java components that generate low-level events, and
therefore support low-level event listener interfaces,
include Component, Dialog, and Frame. Java
components that generate semantic events include
Button, Choice, and MenuItem, among others.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/sources.html (2 of 2) [1/11/2000 1:18:11 PM]


Sidebar: The delegation event model is not alone

Event fundamentals
The delegation event model is not
alone
The delegation event model is not the only event
model Java has seen. Early versions of Java
employed a very different event model that was
found to be limiting as Java grew and incorporated
more advanced technologies such as JavaBeans. It
took a few iterations before the Java architects
settled on the delegation event model, which was
introduced in Java 1.1.

Although older approaches to event handling are still


supported in Java 2 for backward compatibility, you
should stick with the delegation event model. This
module, and the course in general, focus solely on
the delegation event model.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/fundamentals.sb.html [1/11/2000 1:18:11 PM]


Lesson 2.4: Responding to events

Responding to events
Respond to events.

To respond to events, you must register a class as an


Transcript event listener. This class must implement the
appropriate event response methods defined in the event
listener interface.

Registering For an application to receive events, it must register


an event itself as an event listener by calling an event listener
listener registration method on the event source. As an example,
the following code shows how an application registers
itself by calling an event listener registration method:

Transcript

Event registration methods always consist of the prefix


add or remove followed by the name of the event
listener interface.

Implementing After registering an application as an event listener, you


an event have to actually implement the appropriate event
listener listener interface. The addActionListener()
method requires that the object passed in implement the
ActionListener interface. This is accomplished in
the application class definition like this:

class MyApp extends Frame implements ActionListener {


}

file:///c|/courses/jv301_r/module2/responding.html (1 of 2) [1/11/2000 1:18:11 PM]


Lesson 2.4: Responding to events

The application then must implement all of the event


response methods defined in the ActionListener
interface. In the case of ActionListener, there is
only a single method defined: actionPerformed().
Following is an example of how the application might
implement this method:

Transcript

Most event listener interfaces define multiple event response


methods. If you implement such an interface to create an event
listener, you must provide implementations for all of the
methods. A little later in the module you will learn how to use
an alternate approach that eliminates this problem.

Quiz
Click the Quiz button to test your knowledge of events.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/responding.html (2 of 2) [1/11/2000 1:18:11 PM]


Sidebar: Unicast and multicast event delivery

Event sources and listeners


Unicast and multicast event
delivery
Depending on the event source, events can be
delivered in one of two ways: unicast or multicast.
Multicast event delivery involves an event being
broadcast to multiple event listeners simultaneously,
while unicast event delivery involves an event being
broadcast only to a single event listener.
Multiple event listeners can be registered with a
multicast event source using event registration
methods, while only one event listener can be
registered with a unicast event source. If you attempt
to register more than one event listener with a unicast
event source, the event registration method will
disallow the registration and throw an exception.
If you think of your voice being transmitted over a
telephone wire as an "event" in the real world, then
unicast event delivery is akin to a normal telephone
call. In this case, there is only one party listening to
you. On the other hand, multicast event delivery is
akin to a conference call. In this case, there are
multiple listeners.

It's important to understand that unicast event


delivery is a more limited form of event delivery and
should be avoided whenever possible. In other
words, GUI components that act as event sources
should support multicast event delivery if at all
possible. All of the standard Java GUI components
support multicast event delivery.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/sources.sb.html [1/11/2000 1:18:11 PM]


Sidebar: The event delivery process (FlipBook transcript)

Event sources and listeners


The event delivery process
(FlipBook transcript)
The event delivery process can be broken down into
three basic steps:

file:///c|/courses/jv301_r/module2/sources_fb.sb.html (1 of 2) [1/11/2000 1:18:12 PM]


Sidebar: The event delivery process (FlipBook transcript)

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/sources_fb.sb.html (2 of 2) [1/11/2000 1:18:12 PM]


Lesson 2.5: Handling mouse events

Handling mouse events


Handle mouse events.

Java provides two event listener interfaces for


responding to mouse events: MouseListener and
MouseMotionListener.

Mouse events The MouseListener interface defines event


response methods that deal with the mouse button
being clicked and the mouse pointer entering and
exiting a GUI component:

Transcript

Mouse motion The MouseMotionListener interface defines


events event response methods that deal with events related
to the mouse moving:

Transcript

The MouseEvent All of the mouse event response methods accept a


object MouseEvent object that is passed as the only
parameter. This object contains information about
the mouse event such as the X and Y position of the
mouse pointer; the getX() and getY() methods
return the X and Y position of the mouse pointer
when the mouse event occurred.

Exercise
Click the Exercise button to begin creating the
Doodle application.

file:///c|/courses/jv301_r/module2/handling.html (1 of 2) [1/11/2000 1:18:12 PM]


Lesson 2.5: Handling mouse events

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/handling.html (2 of 2) [1/11/2000 1:18:12 PM]


Sidebar: Significance of the delegation event model (Audio transcript)

Responding to events
Significance of the delegation
event model (Audio transcript)
Just in case you're thinking the event
source/listener architecture of the delegation event
model seems like an unnecessary complication,
allow me to explain its significance. One of the key
design goals of the delegation event model is
efficiency. If you consider how many event
notifications are generated for some events such as
mouse drags, it becomes apparent that an event
model that isn't highly efficient could easily create
performance bottlenecks.
The delegation event model is designed so events in
which you aren't interested are never delivered. The
process of registering an event listener is how you
notify an event source that it should deliver events to
the listener. Any event that isn't explicitly registered
via an event listener will never be delivered.

So, if you build an application that doesn't register a


mouse event listener, the application won't receive
any mouse events. This scenario is certainly logical
but earlier versions of Java didn't work this way. You
can thank the delegation event model for making
event handling efficient.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/responding_at.sb.html [1/11/2000 1:18:12 PM]


Sidebar: Registering a class as an event listener (MouseOver transcript)

Responding to events
Registering a class as an event
listener (MouseOver transcript)

This code, which would appear in the application's


constructor, registers the application as a mouse
event listener for a button named button.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/responding_mo.sb.html [1/11/2000 1:18:12 PM]


Sidebar: Implementing an event listener (MouseOver transcript)

Responding to events
Implementing an event listener
(MouseOver transcript)

This method prints a message to standard output if


the user clicks the button event source with which
the application is registered.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/responding2_mo.sb.html [1/11/2000 1:18:12 PM]


Quiz: Event basics

Quiz
Event basics

Each question is worth one point. Some questions ask you to select the
best answer, others ask you to select all the correct answers. To receive
credit for questions asking for all the correct answers, you must select all
the correct answers and only the correct answers.

1.The event model employed by Java 2 is known as:


Please select the best answer.
A. The Java 2 event model
B. The Swing event model
C. The delegation event model
2.Events fired in response to a low-level input or visual user interface
interaction are known as:
Please select the best answer.
A. Transient events
B. Low-level events
C. Semantic events
3.A GUI component capable of generating events is known as:
Please select the best answer.
A. An event manager
B. An event source
C. An event listener
4.Which one of the following methods would you call to register a class as
a MouseListener event listener?
Please select the best answer.
A. addListener()
B. registerMouseListener()
C. addMouseListener()

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/responding.qz.html [1/11/2000 1:18:12 PM]


Lesson 2.6: Using event adapters

Using event adapters


Use event adapters to handle events more
cleanly.

Implementing an event listener interface to handle


events is often a hassle because it requires that you
implement all of the event response methods in the
interface, regardless of which ones you need.
You can sidestep this problem with event adapters,
which are "convenience classes" that make the handling
of events much cleaner. Event adapters enable you to
implement only the event response methods you need,
freeing you from the hassle of writing empty methods
you don't need just to conform to an interface.
Event adapters are classes, not interfaces, which is why
you can override only the methods you need in them.
There is a unique event adapter class for each event
listener interface that defines more than one method.
Examples of event adapters include
ComponentAdapter, KeyAdapter, and
MouseAdapter.

Since event adapters are classes, you can't just


implement them in an application class. To use an
adapter class, you must create an inner class, within the
application class, that derives from the adapter class.
You then implement only the event response methods
you need in the inner class. Following is an example of
such an inner class called MouseHandler:

Transcript

file:///c|/courses/jv301_r/module2/adapters.html (1 of 2) [1/11/2000 1:18:13 PM]


Lesson 2.6: Using event adapters

You still must register the application as an event


listener, but this time the listener is specified as an
instance of the adapter-derived inner class instead of the
application object itself:
addMouseListener(new MouseHandler());

Exercise
In this exercise, you'll use an event adapter to handle
mouse events.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/adapters.html (2 of 2) [1/11/2000 1:18:13 PM]


Sidebar: The MouseListener interface (MouseOver transcript)

Handling mouse events


The MouseListener interface
(MouseOver transcript)

The mouseClicked() method is called whenever


the user clicks a mouse button. For more specific
mouse button handling, you can break a mouse click
down into a button press and release.
The mousePressed() and mouseReleased()
methods are called if a button is pressed and
released.

The mouseEntered() and mouseExited()


methods are called whenever the mouse pointer
enters and exits a GUI component.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/handling_mo.sb.html [1/11/2000 1:18:13 PM]


Sidebar: The MouseMotionListener interface (MouseOver transcript)

Handling mouse events


The MouseMotionListener
interface (MouseOver transcript)

The mouseMoved() method is called whenever the


mouse is moved across the applet window.

The mouseDragged() method is similar to


mouseMoved() but it is only called if the mouse is
being moved while a mouse button is held down.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/handling2_mo.sb.html [1/11/2000 1:18:13 PM]


Exercise: Starting the Doodle application

Exercise

Handling mouse events


Starting the Doodle application
Objective
Add mouse support to the Doodle application.
Exercise scoring
Full credit for this exercise is 10 points. You'll submit your source code to the course tutors.
Overview
As you learned in the previous module, you'll build a course project application named “Doodle”
throughout the course. The Doodle application is a simple graphics application that allows the user to
draw graphic shapes in different colors. In this exercise, you begin the Doodle application by adding
support for the mouse.
Source files
We've supplied the starter code for this exercise in the 02-05 folder of the course download available
from the Resources page.

Instructions
The Doodle application relies heavily on a support class named DoodleShape that represents a simple
graphic shape. The DoodleShape class provides constants identifying each shape that it can represent:
LINE, RECTANGLE, TRIANGLE, and OVAL. Since your focus is on the Doodle application itself, I've
provided the code for the DoodleShape class (also available in the file DoodleShape.java):

import java.awt.*;

public class DoodleShape {

public static final int LINE = 1;


public static final int RECTANGLE = 2;
public static final int TRIANGLE = 3;
public static final int OVAL = 4;

private int type;


private Point start;
private Point end;

file:///c|/courses/jv301_r/module2/handling.ex.html (1 of 5) [1/11/2000 1:18:13 PM]


Exercise: Starting the Doodle application

private Color color;

public DoodleShape(int type, Point start, Point end, Color color) {


this.type = type;
this.start = start;
this.end = end;
this.color = color;
}

public void setEnd(Point end) {


// Set the end position
this.end = end;
}

public void draw(Graphics g) {


// Set the graphics color and save the old color
Color c = g.getColor();
g.setColor(color);

// Draw the appropriate shape


switch (type) {
case LINE:
g.drawLine(start.x, start.y, end.x, end.y);
break;

case RECTANGLE:
g.drawRect(Math.min(start.x, end.x),
Math.min(start.y, end.y),
Math.abs(end.x - start.x),
Math.abs(end.y - start.y));
break;

case TRIANGLE:
g.drawLine(start.x, end.y,
start.x + (end.x - start.x) / 2, start.y);
g.drawLine(start.x + (end.x - start.x) / 2,
start.y, end.x, end.y);
g.drawLine(end.x, end.y, start.x, end.y);
break;

case OVAL:
g.drawOval(Math.min(start.x, end.x),
Math.min(start.y, end.y),
Math.abs(end.x - start.x),
Math.abs(end.y - start.y));
break;

file:///c|/courses/jv301_r/module2/handling.ex.html (2 of 5) [1/11/2000 1:18:13 PM]


Exercise: Starting the Doodle application

default:
System.err.println("ERROR : Unknown shape type.");
}

// Restore the old graphics color


g.setColor(c);
}
}

Your task in this exercise is to add mouse support to the Doodle application. This involves implementing
both Java mouse event listener interfaces in the Doodle application class and adding code to track the
mouse and draw a graphic shape.
Following is the starter code for the Doodle application (available in the file Doodle.txt). This starter
code contains everything you need except the code to deal with mouse events:

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Doodle extends Frame implements {


ArrayList shapeList = new ArrayList();
int shapeType;
Color shapeColor;
DoodleShape curShape;

public static void main(String[] args) {


Doodle app = new Doodle();
app.setSize(400, 400);
app.show();
}

public Doodle() {
super("Doodle");

// Initialize drawing member variables


shapeType = DoodleShape.LINE;
shapeColor = Color.black;

// Register the event handlers


addWindowListener(new WindowHandler());
// *** REGISTER THE MOUSE LISTENERS HERE ***
}

file:///c|/courses/jv301_r/module2/handling.ex.html (3 of 5) [1/11/2000 1:18:13 PM]


Exercise: Starting the Doodle application

public void paint(Graphics g) {


// Draw the list of graphic objects
for (int i = 0; i < shapeList.size(); i++) {
DoodleShape shape = (DoodleShape)shapeList.get(i);
shape.draw(g);
}
}

class WindowHandler extends WindowAdapter {


public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
}

Notice that there is a comment in the constructor that indicates where you should register the mouse
event listeners. You also need to clarify in the Doodle class declaration that Doodle implements the
two mouse listener interfaces: MouseListener and MouseMotionListener. Finally, you need to
implement all of the event response methods defined in the listener interfaces.
You may have noticed that the Doodle application already responds to window events, as is evident by
the call to addWindowListener(). This is necessary so that the application is exited properly when
the frame window is closed. The WindowHandler class is an event adapter class that makes handling
window events cleaner. You'll learn how to create and use event adapters in the next lesson.
I've provided the code that goes in the important mouse event response methods. Following is the code
that should be executed when the mouse button is pressed (available in the file MousePressed.txt):

public void mousePressed(MouseEvent e) {


// Create a new shape
curShape = new DoodleShape(shapeType, e.getPoint(),
e.getPoint(), shapeColor);
}

Following is the code that should be executed when the mouse button is released (available in the file
MouseReleased.txt):

public void mouseReleased(MouseEvent e) {


// Set the end position of the shape
curShape.setEnd(e.getPoint());

// Add the shape to the list


shapeList.add(curShape);

// Repaint to clean up the "rubber band" shape

file:///c|/courses/jv301_r/module2/handling.ex.html (4 of 5) [1/11/2000 1:18:13 PM]


Exercise: Starting the Doodle application

repaint();
}

Following is the code that should be executed when the mouse is dragged (available in the file
MouseDragged.txt):

public void mouseDragged(MouseEvent e) {


// Prepare the graphics context
Graphics g = getGraphics();
g.setXORMode(Color.white);

// Erase the previous "rubber band" shape


curShape.draw(g);

// Update the shape position


curShape.setEnd(e.getPoint());

// Draw the updated "rubber band" shape


curShape.draw(g);
}

What to submit to the course tutors

In the text box below, cut and paste the complete code for the Doodle class. Click OK, I'm Done to
submit your code to the course tutors.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/handling.ex.html (5 of 5) [1/11/2000 1:18:13 PM]


Lesson 2.7: Handling keyboard events

Handling keyboard events


Handle keyboard events.

Keyboard events are handled much like mouse


events, except that there is a different event listener
interface for keyboard events: KeyListener. The
KeyListener interface defines event response
methods related to the keyboard:

Transcript

Keyboard events are only delivered to a component if the


component has the input focus, which is usually evident by
a highlighted rectangle that illuminates the component. If
you create a custom component, you must make sure it
receives the input focus by calling the requestFocus()
method in response to a mouse press event. You'll learn how to do this in
the next module.

The KeyEvent In all three of the key event handler methods, a


object KeyEvent object is passed along containing
information about the key event. The KeyEvent
class provides two methods that can be used to
determine the key identity: getKeyChar() and
getKeyCode(). The getKeyChar() method
returns the key identity as a character, while the
getKeyCode() method returns an integer code
representing the key. In general, it is safer to use the
getKeyCode() method since it doesn't rely on
language-specific characters.

file:///c|/courses/jv301_r/module2/keyboard.html (1 of 2) [1/11/2000 1:18:13 PM]


Lesson 2.7: Handling keyboard events

The reason for representing key identities as integer codes


instead of characters is to avoid language-specific character
dependencies. For example, you might want to develop an
applet that supports both English and Japanese, languages
which have very different character sets. Using the
getKeyCode() method to check for the identity of a key allows the
applet to support both languages.

Integer key codes are defined as constants in the


KeyEvent class and begin with VK_. Examples of
integer key codes include VK_A, VK_B, VK_1,
VK_2, VK_ENTER, VK_PAGE_UP, VK_LEFT, and
VK_RIGHT, to name a few.

The following code shows how to use the


getKeyCode() method to see if the user pressed
an arrow key:

public void keyPressed(KeyEvent e) {


switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
System.out.println("Up!");
break;
case KeyEvent.VK_DOWN:
System.out.println("Down!");
break;
case KeyEvent.VK_LEFT:
System.out.println("Left!");
break;
case KeyEvent.VK_RIGHT:
System.out.println("Right!");
break;
}
}

Exercise
Add keyboard support to the Doodle application in
this exercise.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/keyboard.html (2 of 2) [1/11/2000 1:18:13 PM]


Sidebar: Inner classes

Using event adapters


Inner classes
Inner classes represent an interesting feature that was
added to Java in version 1.1. Put simply, an inner
class is a class defined within another class. Inner
classes allow you to embed a class definition within
another class, effectively creating a class with a
scope limited to the inner workings of the class in
which it is defined.

One very handy usage of inner classes is in event


handling, where you can create event adapter inner
classes within an applet or application class. In this
situation, inner classes give you the freedom to
respond to individual events without having to fully
implement an event listener interface.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/adapters.sb.html [1/11/2000 1:18:13 PM]


Sidebar: The MouseHandler inner class (MouseOver transcript)

Using event adapters


The MouseHandler inner class
(MouseOver transcript)

The MouseHandler inner class extends the


MouseAdapter class and overrides the
mouseClicked() method.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/adapters_mo.sb.html [1/11/2000 1:18:13 PM]


Exercise: Using event adapters in Doodle

Exercise

Using event adapters


Using event adapters in Doodle
Objective
Use an event adapter to handle mouse events in the Doodle application.
Exercise scoring
Full credit for this exercise is 10 points. You'll submit your source code to the course tutors.
Overview
In the previous lesson, you created the Doodle application and added mouse support to it. However, you
were forced to implement some event response methods that you didn't need because you were
implementing mouse listener interfaces instead of using event adapters. In this exercise, you are going to
modify the Doodle application so that it uses an event adapter to handle mouse events.
Instructions
You should create an inner class named MouseHandler that is derived from the MouseAdapter
class. You can then move the mousePressed() and mouseReleased() methods into the
MouseHandler inner class and get rid of the unnecessary methods defined in the MouseListener
interface.
You should also create an inner class named MouseMotionHandler that is derived from the
MouseMotionAdapter class. You can then move the MouseDragged() method into this class.
What to submit to the course tutors
In the text box below, cut and paste the code for the new Doodle class. Make sure that you modify the
appropriate listener registration methods to reference instances of these new inner classes. Click OK, I'm
Done to submit the code to the course tutors.

file:///c|/courses/jv301_r/module2/adapters.ex.html (1 of 2) [1/11/2000 1:18:14 PM]


Exercise: Using event adapters in Doodle

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/adapters.ex.html (2 of 2) [1/11/2000 1:18:14 PM]


Lesson 2.8: Module wrap-up

Events and user interaction


Module wrap-up

This module introduced you to events. You began


the module by learning some event basics, including
the relationship between event sources and listeners.
You then responded to events by registering event
listeners and implementing event listener interfaces.
From there, you explored mouse events and how to
respond to them. You used event adapters, which
provide a much cleaner approach to handling events.
Finally, you finished up the module by responding to
keyboard events.
This module discussed how the following terms and
concepts relate to Java:
event
event-driven programming
delegation event model
low-level event
semantic event
event listener
event source
unicast event delivery
multicast event delivery
event adapter
Inner class

Discussion
Do you have any questions or comments about the
material covered so far in this course? If so, click the

file:///c|/courses/jv301_r/module2/wrap.html (1 of 2) [1/11/2000 1:18:14 PM]


Lesson 2.8: Module wrap-up

Discuss button on the toolbar, locate the module


topic folder that pertains to your question or
comment, and read through the discussions to see if
someone has already asked your question. If not,
post it yourself.

Quiz
Click the Quiz button to test what you've learned in
this module.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/wrap.html (2 of 2) [1/11/2000 1:18:14 PM]


Sidebar: The KeyListener interface (MouseOver transcript)

Handling keyboard events


The KeyListener interface
(MouseOver transcript)

The keyTyped() method is called whenever a key


is typed on the keyboard.

For more control over a typed key, you can use the
keyPressed() and keyReleased() methods,
which are called in response to a key press and a key
release.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/keyboard_mo.sb.html [1/11/2000 1:18:14 PM]


Exercise: Supporting the keyboard in Doodle

Exercise

Handling keyboard events


Supporting the keyboard in Doodle
Objective
Add keyboard support to the Doodle application.
Exercise scoring
Full credit for this exercise is 10 points. You'll submit your source code to the course tutors.
Overview
At this stage the Doodle application allows the user to draw lines by holding down the mouse button and
dragging the mouse around. You may have noticed that the DoodleShape class supports other shape
types, as well as color. In this exercise, you'll add keyboard support to Doodle so that the user can press
keys to change the drawing color and the shape type.
Instructions
You should handle key events using the keyPressed() event response method. Make sure to use an
event adapter when handling this event so that you don't have to implement any unnecessary methods in
the KeyListener interface. Also, keep in mind that you should detect which key was pressed using
the getKeyCode() method defined in the KeyEvent class, which returns a key constant.
Following is the code that should be executed in response to the user pressing the space key, which is
identified by the VK_SPACE constant:

if (++shapeType > DoodleShape.OVAL)


shapeType = DoodleShape.LINE;

The above code cycles through the different shape types, allowing the user to change the shape by
pressing the space key. Following is the code for the R key, which is identified by the VK_R constant:
shapeColor = Color.red;
This code uses the Color.red color constant to set the current shape color to red, which means that
any shapes drawn after pressing the R key will be red. Feel free to add code to support as many colors as
you choose. For information about the different color constants supported in Java, refer to the Color
class in the Java documentation.
What to submit to the course tutors

file:///c|/courses/jv301_r/module2/keyboard.ex.html (1 of 2) [1/11/2000 1:18:14 PM]


Exercise: Supporting the keyboard in Doodle

In the text box below, cut and paste the code for the new Doodle class. Click OK, I'm Done to submit
your code to the course tutors.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/keyboard.ex.html (2 of 2) [1/11/2000 1:18:14 PM]


Lesson 3.1: Introducing Swing

Introducing Swing
Module introduction
The Java programming language has evolved to
provide an extremely rich set of graphical user
interface classes that you can use to build powerful
and effective user interfaces.

This module explores Java's support for user


interface construction with a particular emphasis on
the new Swing toolkit. The Swing toolkit in Java 2
provides an incredibly wide range of GUI features
that can be interesting and easy to use.

Module After completing the module, you'll have the skills


learning and knowledge necessary to:
objectives ● Clarify Swing's relationship to the AWT

● Assess the GUI features offered by Swing

● Use Swing frames to create main application


windows
● Port a simple AWT application to Swing

● Explain the function and significance of Swing


panes

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module3/intro.html [1/11/2000 1:18:14 PM]


Quiz: Events and user interaction

Quiz
Events and user interaction

Each question is worth one point. Some questions ask you to select the
best answer, others ask you to select all the correct answers. To receive
credit for questions asking for all the correct answers, you must select all
the correct answers and only the correct answers.

1.Which one of the following GUI component classes is capable of


generating semantic events?
Please select the best answer.
A. Component
B. Frame
C. Button
2.Which one of the following event listener interfaces would you
implement in order to handle mouse drag events?
Please select the best answer.
A. MouseEventListener
B. MouseMotionListener
C. MouseListener
3.Convenience classes that make the handling of events much cleaner are
known as:
Please select the best answer.
A. Event listeners
B. Event adapters
C. Event responders
4.Which one of the following methods in the KeyEvent class should you
use to handle key events in order to avoid language dependencies?
Please select the best answer.
A. getKey()
B. getKeyChar()
C. getKeyCode()

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module2/wrap.qz.html [1/11/2000 1:18:15 PM]


Lesson 3.2: GUI basics

GUI basics
Evaluate the significance of GUI
components.

A GUI component is a graphical user interface


element that is used as a building block for user
interfaces. The Java 2 API provides a large set of
components in the Abstract Windowing Toolkit
(AWT), which is located in the java.awt package.
Also, the Swing toolkit provides an even larger set of
components in the various javax.swing.*
packages.

The x in javax.swing is a carry-over from when Swing


was a separate extension to the Java API. As of Java 2,
Swing is a part of the core Java API and therefore is no
longer considered an extension.

Following are examples of some GUI components:

Transcript

Java's AWT is structured as a nested hierarchy of


components that begin with the Component class.
The Component class defines general properties
common to all components such as width, height,
background color, etc. This component hierarchy not
only defines the relationship between component
classes, but also the way components are arranged on
the screen and the order in which they are painted. If
you create custom components of your own, you will
typically derive them from the Component class.

file:///c|/courses/jv301_r/module3/basics.html (1 of 2) [1/11/2000 1:18:15 PM]


Lesson 3.2: GUI basics

Some components are capable of containing other components.


These components are called containers. The main frame
Note: window in an application is a good example of a container
because it contains all of the components that make up the
application's user interface.

Exercise
In this exercise, you'll modify the Doodle application
so that its drawing functionality is encapsulated in a
component.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module3/basics.html (2 of 2) [1/11/2000 1:18:15 PM]


Lesson 3.3: Swing and the AWT

Swing and the AWT


Assess the relationship between Swing and
the AWT.

Prior to Java 2, the AWT formed the entirety of


Java's support for GUI construction. However, Java 2
introduces the Swing toolkit, which works
hand-in-hand with the old AWT.

The Swing toolkit provides many classes that replace


classes in the AWT. These Swing classes are often
derived from AWT classes, so essentially you can
think of Swing as an extension of the AWT. As an
example, the AWT Component class has a Swing
counterpart named JComponent.

In general, Swing classes that derive from and/or replace


existing AWT classes are named starting with the letter J.
For example, the JButton Swing class replaces the
Button class in the AWT.

Why Swing? The main reason for the addition of Swing has to do
with a limitation in the AWT. Components in the
AWT are known as heavyweight components
because they are associated with native components,
or peers, on a given operating system. For example,
an AWT button uses an underlying Windows peer
when running on the Windows platform. This
approach results in platform dependencies that make
it difficult to build truly cross-platform GUIs.
Swing is built around the concept of lightweight
components, which are peerless components.
Lightweight components have no underlying native
component, meaning that Swing components are
100% Java. As a result, Swing components don't

file:///c|/courses/jv301_r/module3/swing.html (1 of 2) [1/11/2000 1:18:15 PM]


Lesson 3.3: Swing and the AWT

have any variance across different platforms.


To accommodate the desire among users to have
Java GUIs resemble native GUIs, Swing employs a
pluggable look and feel architecture, which allows
you to set the look and feel of a GUI dynamically.

In addition to a lightweight component architecture, Swing


also provides many new features that you'll learn about
throughout the course.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module3/swing.html (2 of 2) [1/11/2000 1:18:15 PM]


Sidebar: GUI design

GUI basics
GUI design
Since a GUI is a graphical application's only means
of communicating with the user, you should
carefully consider its design. Major software
publishers spend lots of money researching GUI
design to find out how users can get the most out of
their software. The good news is that there is no right
or wrong in GUI design. It is an art form that
involves merging the aesthetic value of a design with
practical function.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module3/basics.sb.html [1/11/2000 1:18:15 PM]


Sidebar: GUI components (FlipBook transcript)

GUI basics
GUI components (FlipBook
transcript)
Following are examples of some GUI components:

file:///c|/courses/jv301_r/module3/basics_fb.sb.html (1 of 2) [1/11/2000 1:18:15 PM]


Sidebar: GUI components (FlipBook transcript)

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module3/basics_fb.sb.html (2 of 2) [1/11/2000 1:18:15 PM]


Exercise: Moving Doodle's drawing functionality into a component

Exercise

Gui basics
Moving Doodle's drawing functionality into a component
Objective
Encapsulate the drawing functionality of Doodle in a component.
Exercise scoring
The full credit for this exercise is 5 points. You'll submit your source code to the course tutors.
Overview
It is helpful to have the drawing functionality of the Doodle application encapsulated in a component. In
this exercise, you create a component class named DoodlePad that is derived from the Component
class.
Source files
We've supplied the starter code for this exercise in a file called Doodle.java. You'll find this file in
the 03-02 folder of the course download available from the Resources page.

Instructions
Create a component class named DoodlePad that is derived from the Component class. The
DoodlePad class should handle the mouse and keyboard events that were handled in the application,
and completely take on the responsibility of drawing the graphic objects. To help get you started, I've
provided the code for the new Doodle class, which creates a DoodlePad component and adds it to the
application's user interface:

import java.awt.*;
import java.awt.event.*;

public class Doodle extends Frame {


DoodlePad pad;

public static void main(String[] args) {


Doodle app = new Doodle();
app.setSize(400, 400);
app.show();
}

file:///c|/courses/jv301_r/module3/basics.ex.html (1 of 2) [1/11/2000 1:18:15 PM]


Exercise: Moving Doodle's drawing functionality into a component

public Doodle() {
super("Doodle");

// Create the Doodle pad


pad = new DoodlePad();

// Add the Doodle pad to the frame


add(pad);

// Register the event handlers


addWindowListener(new WindowHandler());
}

class WindowHandler extends WindowAdapter {


public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
}

The only trick to creating the DoodlePad class is to make sure that you call the requestFocus()
method in response to the mouse button being pressed. This step makes sure that the input focus is set to
the component so that it receives keyboard events.
What to submit to the course tutors
In the text box below, cut and paste the code for new DoodlePad component class. Click OK, I'm Done
to submit your code to the course tutors.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module3/basics.ex.html (2 of 2) [1/11/2000 1:18:15 PM]


Lesson 3.4: The Swing application framework

The Swing application framework


Examine the Swing application framework
and evaluate what it can offer user interface
construction.

Following are the major features offered by the


Swing toolkit:
Lightweight component framework
Pluggable look and feel architecture
Enhanced layout managers
Powerful set of reusable GUI components
Standard dialog boxes
Tool tips
Keyboard mnemonics
Support for assistive technologies

The AWT provides a set of layout managers that are


used to determine how components are physically
arranged within a container. Swing introduces four
additional layout managers, including one that
significantly simplifies the layout of components in
complex user interfaces.
Swing also adds a huge set of components to help
make user interfaces richer and more powerful. In
addition, Swing's standard dialog boxes provide a
means of performing common UI tasks such as
allowing the user to browse and select a file or a
color.

Tool tips, keyboard mnemonics, and support for


assistive technologies all fall within the realm of user
interface enhancements. Tool tips are small pop-up
windows that provide help information within the
context of a GUI element. Keyboard mnemonics
provide a means of assigning hot-keys to common
tasks. Finally, support for assistive technologies
allows you to build user interfaces that are accessible

file:///c|/courses/jv301_r/module3/application.html (1 of 2) [1/11/2000 1:18:16 PM]


Lesson 3.4: The Swing application framework

to users with physical disabilities.

Technically speaking, Java's support for assistive


technologies isn't part of Swing but instead belongs to the
Accessibility API in the javax.accessibility
package. However, for the purposes of this course we'll be
discussing it in the same context as Swing.

Quiz
Click the Quiz button to test your knowledge of
Swing basics

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module3/application.html (2 of 2) [1/11/2000 1:18:16 PM]


Lesson 3.5: Framing an application

Framing an application
Use Swing's JFrame class

All Swing user interfaces begin with a frame, which


is a container that serves as the main window for an
application. Since they serve as main application
windows, frames support familiar UI window
features such as a title, border, and buttons for sizing
and closing the frame.
Frames are represented by the JFrame class, which
replaces the AWT's Frame class. JFrame is one of
the few heavyweight Swing components because it
represents a physically distinct window. Following is
an example of a Swing frame:

The JFrame class is actually very similar to the


AWT's Frame class. In fact, you can convert an
AWT application to Swing by simply changing the
application's frame from Frame to JFrame.

However, the JFrame class introduces the concept

file:///c|/courses/jv301_r/module3/framing.html (1 of 2) [1/11/2000 1:18:16 PM]


Lesson 3.5: Framing an application

of a content pane, which is a special component that


takes on the job of managing the frame's interior.
Any UI modifications made to a Swing frame such
as adding components must actually be made to the
frame's content pane. This task is accomplished by
first retrieving the content pane with a call to the
getContentPane() method:

Transcript

Exercise
In this exercise, you'll modify Doodle’s GUI so that
it uses the Swing toolkit.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module3/framing.html (2 of 2) [1/11/2000 1:18:16 PM]


Quiz: Swing basics

Quiz
Swing basics

Each question is worth one point. Some questions ask you to select the
best answer, others ask you to select all the correct answers. To receive
credit for questions asking for all the correct answers, you must select all
the correct answers and only the correct answers.

1.Components that are capable of containing other components are known


as:
Please select the best answer.
A. Compartments
B. Containers
C. Frames
2.Which one of the following Swing components replaces a similar
component in the AWT?
Please select the best answer.
A. JLabel
B. AbstractButton
C. ImageIcon
3.A Java component with no underlying native component associated with
it is known as a:
Please select the best answer.
A. Peer component
B. Lightweight component
C. Heavyweight component

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module3/application.qz.html [1/11/2000 1:18:16 PM]


Lesson 3.6: Understanding panes

Understanding panes
Define how panes are used within Swing
user interfaces.

A pane is a generic Swing container. Multiple


Transcript panes are used in Swing to create an architecture that
can support rich GUI features. There are five
fundamental types of Swing panes:
Root pane
Layered pane
Content pane
Menu bar
Glass pane

The root pane is a virtual pane that serves as a


container for the other four pane types. The
following figure reveals the structure of the root
pane:

file:///c|/courses/jv301_r/module3/panes.html (1 of 2) [1/11/2000 1:18:16 PM]


Lesson 3.6: Understanding panes

The purpose of this pane architecture is to provide a


means of supporting both lightweight and
heavyweight components within a single GUI.
The layered pane is not a pane per se. Unlike the
content pane, one does not draw in it directly.
Instead, it is a means of organizing the drawing order
of other panes. Since lightweight components don't
have peers, the layered pane takes on the chore of
determining how they overlap each other.

The content pane is the central pane to which you


add components to construct a user interface. The
menu bar is on the same layer as the content pane
and provides a place for an application's menu.
Finally, the glass pane resides on a layer above all
others and allows you to draw graphics on top of
components; the glass pane is invisible by default.

The good news about the root pane is that you don't have to
worry about it too much when constructing a Swing GUI.
However, occasionally this knowledge will come in handy
so it's important that you at least understand the basic panes
housed in the root pane.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module3/panes.html (2 of 2) [1/11/2000 1:18:16 PM]


Sidebar: The getContentPane() method (MouseOver transcript)

Framing an application
The getContentPane() method
(MouseOver transcript)

In this example, a button is indirectly added to the


Swing frame by adding it to the frame's content pane.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module3/framing_mo.sb.html [1/11/2000 1:18:16 PM]


Exercise: Porting the Doodle application to Swing

Exercise

Framing an application
Porting the Doodle application to Swing
Objective
Modify the Doodle application's GUI so that it uses the Swing toolkit.
Exercise scoring
This exercise is worth a total of 1 point. You don't have to submit your code to the course tutors to
receive credit for this exercise.
Overview
In this exercise you'll modify the Doodle application to use the Swing toolkit.
Instructions
Modifying the Doodle application so that it uses the Swing toolkit basically requires changing the parent
classes for Doodle and DoodlePad to Swing classes instead of AWT classes. More specifically, you
should use JFrame instead of Frame, and JComponent instead of Component.
Keep in mind that the JFrame class requires you to add components to its content pane instead of
adding them directly to the frame itself. So, you'll need to make a small change to the code that adds the
DoodlePad component to the frame.
Don't forget that the requestFocus() method should be called in response to the mouse button being
pressed. This step makes sure that the input focus is set to the component so that it receives keyboard
events.
One more thing - the JFrame and JComponent classes are defined in the javax.swing package, so
you need to import this package in the Doodle and DoodlePad source code files.

After completing the exercise, click the OK, I'm Done button and the system will award you full credit.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module3/framing.ex.html [1/11/2000 1:18:16 PM]


Lesson 3.7: Module wrap-up

Introducing Swing
Module wrap-up

This module introduced you to GUIs and the Swing


toolkit. You began the module by learning some GUI
basics, like the role of GUI components. You then
examined the Swing toolkit and its relationship to the
AWT.
From there, you dug deeper into the Swing toolkit by
learning about some of its most important features.
You used Swing's JFrame class to modify the
Doodle application. Finally, you wrapped up the
module by studying the pane architecture employed
by Swing to manage components.
This module discussed how the following terms and
concepts relate to Java:
component
Abstract Windowing Toolkit (AWT)
container
Swing toolkit
lightweight component
heavyweight component
frame
pane

Discussion
Do you have any questions or comments about the
material covered so far in this course? If so, click the
Discuss button on the toolbar, locate the module
topic folder that pertains to your question or
comment, and read through the discussions to see if
someone has already asked your question. If not,

file:///c|/courses/jv301_r/module3/wrap.html (1 of 2) [1/11/2000 1:18:17 PM]


Lesson 3.7: Module wrap-up

post it yourself.

Quiz
Click the Quiz button to test what you've learned in
this module.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module3/wrap.html (2 of 2) [1/11/2000 1:18:17 PM]


Sidebar: Mixing AWT with Swing (Audio transcript)

Understanding panes
Mixing AWT with Swing (Audio
transcript)
Even though Swing co-exists with the Java
AWT, in many ways Swing represents a departure
from the way in which GUIs were constructed using
the AWT in prior versions of Java. However, keep in
mind that Java 2 is still backward compatible with
earlier Java versions. This means that you have the
freedom to use the traditional AWT approach to
building GUIs or the new Swing approach.
One interesting problem associated with Java's
support for both approaches to GUI construction is
the mixing of lightweight and heavyweight
components. Consider the example of creating two
buttons, a Button and a JButton. The first button
is a heavyweight component with a peer while the
second button is a peerless lightweight component.
Java has to be able to allow these two components to
peacefully co-exist within a common GUI
framework, which is why it employs the root pane
architecture.

Even though Java is equipped to handle a mixing of


component types, it isn't a good idea to mix older
AWT components with new Swing components if
you are building an application with a Swing user
interface. The Swing components are designed
specifically to fit into the Swing architecture, and
therefore are the components of choice for Swing
GUIs.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module3/panes_at.sb.html [1/11/2000 1:18:17 PM]


Sidebar: Layering components

Understanding panes
Layering components
The layered pane determines how components
overlap each other. It does this by establishing a
Z-order for each layer, which you can think of as a
number indicating the depth of a layer with respect to
the screen. In other words, the screen is the highest
layer with a theoretical Z-order of infinity, while
other layers have decreasing Z-orders behind the
screen.

The JLayeredPane class defines a set of constant


layer identifiers that specify the Z-order of common
layers. Following are these constants in order of
increasing Z-order:

FRAME_CONTENT_LAYER the content pane and menu bar reside in this


layer
DEFAULT_LAYER most components reside in this layer
PALETTE_LAYER palettes and floating toolbars reside in this
layer
MODAL_LAYER dialog boxes reside in this layer
POPUP_LAYER pop-up menus and tool tips reside in this
layer
DRAG_LAYER components that are being dragged
temporarily reside in this layer

By default, components are added to the default layer


identified by the DEFAULT_LAYER constant. You
can also specify an explicit integer layer when
adding components to a container.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module3/panes.sb.html [1/11/2000 1:18:17 PM]


Lesson 4.1: Laying out user interfaces

Laying out user interfaces


Module introduction
Although specific GUI features are often
encapsulated within a given component, an
application's GUI typically involves the orchestration
of multiple components. Components must be
carefully arranged in a GUI to achieve maximum
impact and ease of use.

This module explores the manner in which


components are physically arranged in a Java GUI. It
will show you how layout managers make it possible
to carefully control the positioning of components in
a GUI.

Module After completing the module, you'll have the skills


learning and knowledge necessary to:
objectives ● Explain the role of layout managers in GUI
design
● Set the layout for a container

● Use the standard AWT layouts, including


flow, grid, border, and card
● Use the Swing box layout

● Nest panels and layouts to build rich GUIs

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/intro.html [1/11/2000 1:18:17 PM]


Quiz: Module quiz

Quiz
Module quiz

Each question is worth one point. Some questions ask you to select the
best answer, others ask you to select all the correct answers. To receive
credit for questions asking for all the correct answers, you must select all
the correct answers and only the correct answers.

1.Java's AWT is structured as a nested hierarchy of components that begin


with:
Please select the best answer.
A. The AWT class
B. The Frame class
C. The Component class
2.A special component that manages the interior of a JFrame object is
called a:
Please select the best answer.
A. Content pane
B. Layout manager
C. Container
3.The virtual pane that serves as a container for other types of panes is
known as the:
Please select the best answer.
A. Root pane
B. Frame pane
C. Glass pane
4.What two panes are located on the same layer within the root pane?
Please select the best answer.
A. Glass pane and layered pane
B. Content pane and menu bar
C. Layered pane and content pane
5.In what central package are most Swing components located?
Please select the best answer.
A. java.swing
B. java.swing.component
C. javax.swing

file:///c|/courses/jv301_r/module3/wrap.qz.html (1 of 2) [1/11/2000 1:18:17 PM]


Quiz: Module quiz

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module3/wrap.qz.html (2 of 2) [1/11/2000 1:18:17 PM]


Lesson 4.2: Layout managers

Layout managers
Explain the role of layout managers in
containers.

You learned that components are added to a


Transcript container, which is often the content pane of an
application's frame window. Every container in Java
has an associated layout manager that determines
how components are physically arranged within the
container. Since every panel has a layout manager
associated with it, you can create nested panels
within other panels to facilitate a more elaborate
organization of components.

Layout managers are often referred to simply as layouts.

The Java Layout managers play a very important role in


layout designing the GUI for an applet or application
managers because they greatly simplify the physical
arrangement of components. The basic layout
managers supported by the Java AWT follow:
● Flow layout

● Grid layout

● Border layout

● Card layout

● Grid bag layout

In addition to these layouts, the Swing toolkit


introduces a box layout that can be used to construct
complex GUIs. The box layout serves as a good
alternative to the grid bag layout, which can be
complicated to use.

file:///c|/courses/jv301_r/module4/layout.html (1 of 2) [1/11/2000 1:18:18 PM]


Lesson 4.2: Layout managers

The Swing toolkit actually introduces four new layouts,


including the box layout. However, the remaining three
Swing layouts are not intended for application
programming because they are used internally by Swing
components.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/layout.html (2 of 2) [1/11/2000 1:18:18 PM]


Lesson 4.3: Working with layouts

Working with layouts


Create layouts and assign them to
containers.

Layouts are implemented in Java as classes that you


can instantiate. However, layouts themselves are not
GUI components. Layouts are invisible objects used
to control how GUI components are arranged within
a container.
All layout classes are required to implement the
LayoutManager interface. Creating a layout is
much like creating any other Java object:

Transcript

Most layouts also allow you to specify the spacing


between components:

Transcript

Perhaps more important than creating a layout is


assigning it to a container. The Container class
defines a method named setLayout() that sets
the layout for a container:

Transcript

You can also set the layout for a new container in the
container's constructor when you first create it:

file:///c|/courses/jv301_r/module4/working.html (1 of 2) [1/11/2000 1:18:18 PM]


Lesson 4.3: Working with layouts

JPanel panel =
new JPanel(new FlowLayout());

Once the layout has been set for a container, any


components added to the container will be physically
arranged by the layout.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/working.html (2 of 2) [1/11/2000 1:18:18 PM]


Sidebar: Using layouts (Audio transcript)

Layout managers
Using layouts (Audio transcript)
Layouts provide a means of arranging
components in a container using relative positioning,
which means that the components are positioned
relative to each other. Relative positioning is
important because it helps to alleviate problems
associated with variations across different computing
platforms. For example, resolutions and window
sizes can vary greatly from platform to platform, in
which case components that are positioned at
absolute locations may not look like you would
expect.

You can still position components at absolute


locations in Java but it's a good idea to try and avoid
it if possible. Fortunately, layouts go a long way
toward alleviating the need to use absolute
positioning. For example, the box layout has some
really slick features that allow you to fine-tune the
relative positioning of components.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/layout_at.sb.html [1/11/2000 1:18:18 PM]


Lesson 4.4: The flow layout

The flow layout


Use the flow layout to arrange components
in rows.

The flow layout is the default layout manager for


panels, and is represented by the FlowLayout
class. The flow layout arranges components across a
container in rows from left to right.
When a row fills up with components, the
components wrap around and a new row is started.
You can arrange the rows in a flow layout to be
aligned left, right, or centered. By default,
components are aligned centered in a flow layout.

This FlipBook shows how components are added


using a flow layout. It also shows how the
FlowLayout class might be used:

Transcript

Since the flow layout is the default layout manager for


panels, you don't have to explicitly set it in a panel if you
want to use it. However, content panes don't use the flow
layout as the default layout manager, so you must explicitly
set the flow layout if you're working with a content pane.

Quiz
Click the Quiz button to test what you've learned
about layouts.

Exercise
In this exercise, you'll use a flow layout.

file:///c|/courses/jv301_r/module4/flow.html (1 of 2) [1/11/2000 1:18:18 PM]


Lesson 4.4: The flow layout

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/flow.html (2 of 2) [1/11/2000 1:18:18 PM]


Sidebar: Creating a FlowLayout object (MouseOver transcript)

Working with layouts


Creating a FlowLayout object
(MouseOver transcript)

This code creates a FlowLayout object.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/working_mo.sb.html [1/11/2000 1:18:18 PM]


Sidebar: Specifying the spacing between components (MouseOver transcript)

Working with layouts


Specifying the spacing between
components (MouseOver
transcript)

The above code shows how to create a flow layout


with centered components that have 8 pixels between
them in the X direction and 12 pixels in the Y
direction.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/working2_mo.sb.html [1/11/2000 1:18:18 PM]


Sidebar: Assigning a container (MouseOver transcript)

Working with layouts


Assigning a container (MouseOver
transcript)

In this example, the layout for the content pane of a


frame window is set to a flow layout by calling the
setLayout() method.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/working3_mo.sb.html [1/11/2000 1:18:18 PM]


Lesson 4.5: The grid layout

The grid layout


Arrange components in a grid using the grid
layout.

The grid layout, which is represented by the


GridLayout class, is a layout manager that
arranges components in a grid of rows and columns.
Like the flow layout, the grid layout arranges
components from left to right but they conform to a
fixed arrangement of rows and columns.
For example, you may create a grid layout as a 3x2
grid that is capable of holding six components. The
first two components added will be placed in the first
row, the second two in the second row, etc.

A major difference between the grid layout and the


flow layout is that components in a grid layout are
sized the same to fit into each grid cell. You set the
number of cells by passing the number of rows and
columns into the GridLayout constructor:

GridLayout grid =
new GridLayout(3, 2);

This FlipBook shows how components are added


using the grid layout. It also offers some sample
code:

Transcript

Exercise
Click the Exercise button to use a grid layout
manager.

file:///c|/courses/jv301_r/module4/grid.html (1 of 2) [1/11/2000 1:18:18 PM]


Lesson 4.5: The grid layout

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/grid.html (2 of 2) [1/11/2000 1:18:18 PM]


Sidebar: Adding components using a flow layout (FlipBook transcript)

The flow layout


Adding components using a flow
layout (FlipBook transcript)
How the FlowLayout class might be used:

file:///c|/courses/jv301_r/module4/flow_fb.sb.html (1 of 3) [1/11/2000 1:18:19 PM]


Sidebar: Adding components using a flow layout (FlipBook transcript)

file:///c|/courses/jv301_r/module4/flow_fb.sb.html (2 of 3) [1/11/2000 1:18:19 PM]


Sidebar: Adding components using a flow layout (FlipBook transcript)

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/flow_fb.sb.html (3 of 3) [1/11/2000 1:18:19 PM]


Quiz: Layout basics

Quiz
Layout basics

Each question is worth one point. Some questions ask you to select the
best answer, others ask you to select all the correct answers. To receive
credit for questions asking for all the correct answers, you must select all
the correct answers and only the correct answers.

1.Which one of the following layout managers was introduced to Java 2 as


part of the Swing toolkit?
Please select the best answer.
A. Border layout
B. Box layout
C. Grid bag layout
2.What interface are all layout manager classes required to implement?
Please select the best answer.
A. LayoutManager
B. Layout
C. Container
3.Which one of the following methods do you call on a container to set its
layout manager?
Please select the best answer.
A. setLayout()
B. setLayoutManager()
C. setContainerLayout()
4.What is the default layout manager for panels?
Please select the best answer.
A. Box layout
B. Border layout
C. Flow layout

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/flow.qz.html [1/11/2000 1:18:19 PM]


Exercise: Using the flow layout

Exercise

The flow layout


Using the flow layout
Objective
Arrange components using a flow layout.
Exercise scoring
This exercise is worth a total of 1 point. You don't have to submit your code to the course tutors to
receive credit for this exercise.
Overview
Create a class FlowTest that extends JFrame. In the FlowTest constructor perform the following:
1. Set the layout of the frame's content pane to a flow layout.
2. Create five JButton objects with the text "One", "Two", "Three", "Four", and "Five" and add
them to the content pane.
3. Add a window listener to the frame that will allow the user to exit the application by closing the
frame window.
To test your FlowTest class, provide a main() method in which you create an instance of
FlowTest, set its initial size, and show it.
When you run this application, notice how the flow layout arranges the buttons across the content pane
from left to right, using as many rows as necessary. Also notice that the each button is displayed using its
preferred size. As you resize the frame window, you can see that the number of rows used will depend on
the size of the frame window and the size of the buttons.

After completing the exercise, click the OK, I'm Done button and the system will award you full credit.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/flow.ex.html [1/11/2000 1:18:19 PM]


Lesson 4.6: The border layout

The border layout


Arrange components along the borders of a
container using the border layout.

The border layout is the default layout manager for


the content pane of frames, and is represented by the
BorderLayout class. The border layout is very
different from the flow layout and grid layout in that
it arranges components in the geographic directions
north, south, east, and west, along with another
component in the center.

You specify the direction location of a component


when you add it to a container by calling the add()
method and passing one of the following
BorderLayout constants as the second parameter:
NORTH, SOUTH, EAST, WEST, or CENTER.
Following is an example:

add(new JButton("Press me!"),


BorderLayout.NORTH);

If you don't add a component to a certain position, no physical


Note:
space is set aside for the position.

This FlipBook shows how components are added to


containers using a border layout. Some code is also
included as an example:

Transcript

file:///c|/courses/jv301_r/module4/border.html (1 of 2) [1/11/2000 1:18:19 PM]


Lesson 4.6: The border layout

Exercise
Modify the Doodle application so that it uses a
border layout manager.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/border.html (2 of 2) [1/11/2000 1:18:19 PM]


Sidebar: Adding components using the grid layout (FlipBook transcript)

The grid layout


Adding components using the grid
layout (FlipBook transcript)

file:///c|/courses/jv301_r/module4/grid_fb.sb.html (1 of 3) [1/11/2000 1:18:20 PM]


Sidebar: Adding components using the grid layout (FlipBook transcript)

file:///c|/courses/jv301_r/module4/grid_fb.sb.html (2 of 3) [1/11/2000 1:18:20 PM]


Sidebar: Adding components using the grid layout (FlipBook transcript)

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/grid_fb.sb.html (3 of 3) [1/11/2000 1:18:20 PM]


Exercise: Using the grid layout

Exercise

The grid layout


Using the grid layout
Objective
Arrange components using a grid layout.
Exercise scoring
This exercise is worth a total of 1 point. You don't have to submit your code to the course tutors to
receive credit for this exercise.
Instructions
Create a class GridTest that extends JFrame. In the GridTest constructor perform the following:
1. Set the layout of the frame's content pane to a grid layout with 3 rows and 2 columns.
2. Create five JButton objects with the text "One", "Two", "Three", "Four", and "Five" and add
them to the content pane.
3. Add a window listener to the frame that will allow the user to exit the application by closing the
frame window.
To test your GridTest class, provide a main() method in which you create an instance of
GridTest, set its initial size, and show it.
When you have this application working correctly, the buttons will be displayed in a grid of 3 rows and 2
columns with each button being displayed at the same size.

After completing the exercise, click the OK, I'm Done button and the system will award you full credit.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/grid.ex.html [1/11/2000 1:18:20 PM]


Lesson 4.7: The card layout

The card layout


Use the card layout to arrange components
like a deck of cards.

The card layout, which is represented by the


CardLayout class, is a layout manager that
arranges multiple panels so that they act like a stack
of cards. A card layout consists of a group of panels
but only one of the panels is visible at any given
time.
You display different cards by calling the first(),
last(), next() and previous() methods on a
CardLayout object and passing in the parent
container. Each panel card can use its own layout
manager to arrange the components it contains.

This FlipBook shows how components are added to


the card panels of a card layout. It also offers some
sample code which uses the CardLayout class:

Transcript

The card layout is handy for creating wizard-style


interfaces, in which the user is presented with a sequence
of GUIs implemented as card panels.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/card.html [1/11/2000 1:18:20 PM]


Sidebar: Adding components to containers (FlipBook transcript)

The border layout


Adding components to containers
(FlipBook transcript)
Adding components to containers using a border
layout:

file:///c|/courses/jv301_r/module4/border_fb.sb.html (1 of 3) [1/11/2000 1:18:20 PM]


Sidebar: Adding components to containers (FlipBook transcript)

file:///c|/courses/jv301_r/module4/border_fb.sb.html (2 of 3) [1/11/2000 1:18:20 PM]


Sidebar: Adding components to containers (FlipBook transcript)

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/border_fb.sb.html (3 of 3) [1/11/2000 1:18:20 PM]


Exercise: Selecting tools in the Doodle application

Exercise

The border layout


Selecting tools in the Doodle application
Objective
Use a border layout and buttons to create a GUI for selecting drawing tools.
Exercise scoring
This exercise is worth a total of 1 point. You don't have to submit your code to the course tutors to
receive credit for this exercise.
Overview
Although the Doodle application supports a relatively primitive keyboard interface for selecting drawing
tools using the Space key, it makes much more sense to provide GUI components for selecting the
drawing tool. More specifically, a series of buttons representing each drawing tool would provide a nice
user interface for selecting a tool.
In this exercise, you modify the Doodle application to support a series of radio buttons that are used to
select the drawing tool. The code for the buttons will be supplied to you. The focus of the exercise is on
the border layout used to arrange the Doodle application's GUI.
Source files
We've supplied the starter code for this exercise in files called ButtonPanel.txt and
ActionPerformed.txt. You'll find these files in the 04-06 folder of the course download available
from the Resources page.
Instructions
Before getting into the specifics of adding radio buttons to the Doodle GUI, you must first add a method
named setShapeType() to the DoodlePad class so that you can set the shape type. This method
should accept one parameter and simply set the shape type for the pad to the parameter value.
Since the drawing tool buttons should be positioned across the top of the Doodle frame window, you're
going to create a new panel to contain them. Following is the code (ButtonPanel.txt) to create and
add the buttons to the panel.

JPanel buttonPanel = new JPanel();


ButtonGroup group = new ButtonGroup();
JRadioButton button = new JRadioButton("Line", true);
button.addActionListener(this);

file:///c|/courses/jv301_r/module4/border.ex.html (1 of 2) [1/11/2000 1:18:21 PM]


Exercise: Selecting tools in the Doodle application

group.add(button);
buttonPanel.add(button);
button = new JRadioButton("Rectangle", false);
button.addActionListener(this);
group.add(button);
buttonPanel.add(button);
button = new JRadioButton("Triangle", false);
button.addActionListener(this);
group.add(button);
buttonPanel.add(button);
button = new JRadioButton("Oval", false);
button.addActionListener(this);
group.add(button);
buttonPanel.add(button);

Since the border layout is the default layout for the frame's content pane, you don't need to call
setLayout(). However you will need to specify the border positions of the button panel and
DoodlePad component when adding them to the content pane.
You may notice that the Doodle class (this) is registered as an action event listener for each of the
buttons. You need modify the Doodle class declaration to indicate that Doodle implements the
ActionListener interface. Following is the implementation of the actionPerformed() event
response method, which sets the graphic tool based on the button pressed (ActionPerformed.txt):

public void actionPerformed(ActionEvent e) {


String cmd = e.getActionCommand();
if (cmd.equals("Line"))
pad.setShapeType(DoodleShape.LINE);
else if (cmd.equals("Rectangle"))
pad.setShapeType(DoodleShape.RECTANGLE);
else if (cmd.equals("Triangle"))
pad.setShapeType(DoodleShape.TRIANGLE);
else if (cmd.equals("Oval"))
pad.setShapeType(DoodleShape.OVAL);
}

After completing the exercise, click the OK, I'm Done button and the system will award you full credit.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/border.ex.html (2 of 2) [1/11/2000 1:18:21 PM]


Lesson 4.8: The box layout

The box layout


Use the box layout to build intricate user
interfaces.

The box layout is arguably the most versatile of the


layout managers discussed in this course. It is
represented by the BoxLayout class, and provides
a means of arranging components horizontally or
vertically next to each other.
What makes the box layout so powerful is its support
for nesting and its ability to fine-tune the spacing
between components. If specified, the box layout
respects a component's minimum, maximum, and
preferred size. Unlike other layout managers you've
learned about, the box layout has a special container
class named Box that utilizes it.

This FlipBook demonstrates how components are


added using the box layout manager:

Transcript

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/box.html [1/11/2000 1:18:21 PM]


Sidebar: Adding components to the card panels (FlipBook transcript)

The card layout


Adding components to the card
panels (FlipBook transcript)
Adding components to the card panels of a card
layout:

file:///c|/courses/jv301_r/module4/card_fb.sb.html (1 of 3) [1/11/2000 1:18:21 PM]


Sidebar: Adding components to the card panels (FlipBook transcript)

file:///c|/courses/jv301_r/module4/card_fb.sb.html (2 of 3) [1/11/2000 1:18:21 PM]


Sidebar: Adding components to the card panels (FlipBook transcript)

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/card_fb.sb.html (3 of 3) [1/11/2000 1:18:21 PM]


Lesson 4.9: Alignment and invisible fillers in the box layout

Alignment and invisible fillers in


the box layout
Align and create space between
components.

Alignment
The box layout takes an interesting approach to
aligning components: you provide a floating point
number between 0.0 and 1.0 that determines how a
component is aligned with other components in the
box. Following are examples of what different
alignment values mean for a vertical box:

Transcript

Alignments in between these values produces varied


results.

Invisible You can use invisible fillers to create space between


fillers components using the box layout. The Box class
provides the following types of invisible fillers:

Transcript

You use the createRigidArea() method on a


Box object to create a rigid area. The
createHorizontalGlue() and
createVerticalGlue() methods are used to
create glue between components. Finally, to use a
filler you must create an instance of the
Box.Filler inner class.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/alignment.html [1/11/2000 1:18:21 PM]


Sidebar: The box layout versus the grid bag layout

The box layout


The box layout versus the grid bag
layout
Although the box layout is extremely versatile, the
AWT provides a layout manager called grid bag that
provides more intricate control than the box layout.
Of course, increased flexibility often comes at a
price. In the case of the grid bag layout, the price is
the complexity of coding required to create an
intricate GUI.
The grid bag layout is similar to a grid layout except
that you have a lot more control over the size and
position of the cells in the grid. For example, in a
grid bag layout you can specify that some cells span
multiple columns. You can also control the physical
arrangement of components within specific cells in
the grid.

In most cases, you'll be able to use nested box


layouts instead of the grid bag layout. I encourage
this because the box layout is much easier to use than
the grid bag layout. If you want to find out how the
grid bag layout works, please refer to the
GridBagLayout and GridBagConstraints
classes in the Java 2 API documentation.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/box.sb.html [1/11/2000 1:18:21 PM]


Sidebar: Using the box layout manager (FlipBook transcript)

The box layout


Using the box layout manager
(FlipBook transcript)
Adding components using the box layout manager:

file:///c|/courses/jv301_r/module4/box_fb.sb.html (1 of 2) [1/11/2000 1:18:21 PM]


Sidebar: Using the box layout manager (FlipBook transcript)

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/box_fb.sb.html (2 of 2) [1/11/2000 1:18:21 PM]


Lesson 4.10: Module wrap-up

Laying out user interfaces


Module wrap-up

This module introduced you to layout managers,


which are used in Java to control the physical
arrangement of components within a container. You
began the module by learning the basics of layout
managers and why they are important to GUI
construction.
You then created layouts and set them in containers.
From there, you explored each of the different layout
managers available in both the Java AWT and
Swing. You even applied some of the layouts to the
Doodle course project application.
This module discussed how the following terms and
concepts relate to Java:
layout manager
flow layout
grid layout
border layout
card layout
grid bag layout
box layout
invisible filler
glue
rigid area

Discussion
Do you have any questions or comments about the
material covered so far in this course? If so, click the
Discuss button on the toolbar, locate the module

file:///c|/courses/jv301_r/module4/wrap.html (1 of 2) [1/11/2000 1:18:22 PM]


Lesson 4.10: Module wrap-up

topic folder that pertains to your question or


comment, and read through the discussions to see if
someone has already asked your question. If not,
post it yourself.

Quiz
Click the Quiz button to test what you've learned in
this module.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/wrap.html (2 of 2) [1/11/2000 1:18:22 PM]


Sidebar: Alignment values for a vertical box (FlipBook transcript)

Alignment and invisible fillers in the box layout


Alignment values for a vertical box
(FlipBook transcript)

file:///c|/courses/jv301_r/module4/alignment_fb.sb.html (1 of 2) [1/11/2000 1:18:22 PM]


Sidebar: Alignment values for a vertical box (FlipBook transcript)

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/alignment_fb.sb.html (2 of 2) [1/11/2000 1:18:22 PM]


Sidebar: The Box class (FlipBook transcript)

Alignment and invisible fillers in the box layout


The Box class (FlipBook
transcript)
The Box class provides the following types of
invisible fillers:

file:///c|/courses/jv301_r/module4/alignment2_fb.sb.html (1 of 2) [1/11/2000 1:18:22 PM]


Sidebar: The Box class (FlipBook transcript)

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/alignment2_fb.sb.html (2 of 2) [1/11/2000 1:18:22 PM]


Lesson 5.1: Using Swing components

Using Swing components


Module introduction
Although Swing offers many useful classes and
interfaces, Swing components are perhaps the most
interesting and useful of Swing's features. In this
module, you'll use some of the more popular Swing
components to build compelling GUIs.

Module After completing the module, you'll have the skills


learning and knowledge necessary to:
objectives ● Create icons from GIF and JPEG images

● Create and use labels which display text and


icons
● Create and use buttons containing text and
icons
● Improve the appearance of components with
borders
● Use lists and combo boxes to manage the
selection of items

● Explore other components provided in the


Swing toolkit

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module5/intro.html [1/11/2000 1:18:22 PM]


Quiz: Module quiz

Quiz
Module quiz

Each question is worth one point. Some questions ask you to select the
best answer, others ask you to select all the correct answers. To receive
credit for questions asking for all the correct answers, you must select all
the correct answers and only the correct answers.

1.What is the default layout manager for the content pane of an


application's frame window?
Please select the best answer.
A. Flow layout
B. Grid layout
C. Border layout
2.Which one of the following layout managers always forces components
to be the same size?
Please select the best answer.
A. Border layout
B. Box layout
C. Grid layout
3.Which one of the following constants represents a valid position for the
border layout?
Please select the best answer.
A. BorderLayout.TOP
B. BorderLayout.LEFT
C. BorderLayout.CENTER
4.To maximize the space between components in a box layout, you would
use:
Please select the best answer.
A. Glue
B. Filler
C. A rigid area

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module4/wrap.qz.html [1/11/2000 1:18:23 PM]


Lesson 5.2: Icons

Icons
Create and use icon components.

Icons are used in Swing GUIs to provide visual cues


associated with GUI features. The Icon interface
defines the general characteristics of a Swing icon,
including methods for working with the icon.

Java provides a class named ImageIcon that


implements the Icon interface and provides a
simple way to create icons based on GIF or JPEG
images. To create an icon based on an image, simply
provide the name of the image file as the parameter
to the ImageIcon constructor:

ImageIcon icon = new ImageIcon("Fish.gif");

Optionally, you can provide a second parameter that


identifies a text description of the icon:

ImageIcon icon = new ImageIcon("Fish.gif",


"Orange fish");

The text description for an icon is not displayed, but it is


available with a call to the getDescription() method.

file:///c|/courses/jv301_r/module5/icons.html (1 of 2) [1/11/2000 1:18:23 PM]


Lesson 5.2: Icons

Using icons The ImageIcon class isn't actually a Java


component, which means that you can't use it by
itself within a GUI. You will always use the
ImageIcon class in conjunction with components
such as the JLabel component, which you learn
about in the lesson on labels. Following is an
example of using an image icon with a label
component:

Container contentPane = getContentPane();


ImageIcon icon = new ImageIcon("Fish.gif");
contentPane.add(new JLabel(icon));

Here's the results of the code:

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module5/icons.html (2 of 2) [1/11/2000 1:18:23 PM]


Lesson 5.3: Working with labels

Working with labels


Create and use label components.

Labels are output-only components that are used to


present both text and graphical information to the
user. You often see labels used in conjunction with
input components such as text edit fields. Labels are
represented in Swing by the JLabel class.
Following is an example of creating a simple text
label:

JLabel label = new JLabel("Happy birthday!");

You saw in the previous lesson how to create an icon


label by passing an icon as the only parameter to the
JLabel constructor. You can also combine text and
an icon to create a more compelling and informative
label:

ImageIcon icon = new ImageIcon("Cake.gif");


JLabel label = new JLabel("Happy birthday!", icon,
JLabel.CENTER);

Here's the results of the code:

file:///c|/courses/jv301_r/module5/labels.html (1 of 2) [1/11/2000 1:18:23 PM]


Lesson 5.3: Working with labels

Aligning The JLabel class supports a set of alignment


labels constants that are used to specify the horizontal and
vertical alignment of the label's contents. For
example, the TOP, LEFT, BOTTOM, RIGHT, and
CENTER constants are used to determine the
horizontal and vertical alignment of text within a text
label. The JLabel class also supports geographic
alignment constants such as NORTH and
NORTH_EAST.
You set the horizontal and vertical alignment of a
label using the setHorizontalAlignment()
and setVerticalAlignment() methods. You
can also use the same constants to set a label's text
position with respect to its icon by calling
setHorizontalTextPosition() and
setVerticalTextPosition(). Following is
an example of setting the vertical and horizontal text
position of a label:

label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.BOTTOM);

Exercise
Click the Exercise button to build an application that
uses labels.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module5/labels.html (2 of 2) [1/11/2000 1:18:23 PM]


Sidebar: Disabled image icons

Icons
Disabled image icons
You are probably familiar with images used on
toolbar buttons in many popular applications such as
a Web browser. And you've probably noticed that
when these buttons become disabled, the images on
them take on a gray, colorless appearance. This
grayed image provides a visual cue to let you know
that a particular button is disabled.
In Java, image icons are used to represent images in
most components, including buttons, and are
therefore responsible for offering a grayed look in
response to a component becoming disabled.

Swing uses a gray filter to carry out a default graying


feature for the ImageIcon class. Swing
automatically generates a grayed version of any
images that you use with the ImageIcon class,
which keeps you from having to provide an
additional grayed image. Of course, if you want to
create a custom icon class of your own you must
provide a suitable graying feature, but that's a
relatively advanced procedure.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module5/icons.sb.html [1/11/2000 1:18:23 PM]


Lesson 5.4: Using buttons

Using buttons
Create and use button components.

One of the most commonly used GUI components


is the button. Swing supports buttons via the
JButton class, which provides support for a basic
push button with text and/or an icon. Following are
examples of creating JButton components:

JButton b1 = new JButton("Click me!");


JButton b2 = new JButton(icon);
JButton b3 = new JButton("Click me!", icon);

Click the View Example button to see a simple text


button, an icon button, and a combination text/icon
button.

Swing also includes the JToggleButton,


JCheckBox, and JRadioButton classes, which
are types of buttons.

A toggle button is a two-state button


that remains pushed-in after you
click it; when you click it again it
returns to the un-pushed state.

A check box is a type of toggle


button that displays a check mark
next to a line of text.

Radio buttons are used in groups to


provide a mutually exclusive series
of choices; only one radio button in
a group can be selected at any given
time.

file:///c|/courses/jv301_r/module5/buttons.html (1 of 2) [1/11/2000 1:18:23 PM]


Lesson 5.4: Using buttons

Button events When a button is pushed, it generates an action


event. You register an event listener to handle button
action events by calling the
addActionListener() method on a button.
You must then implement the ActionListener
interface in the event listener class by providing a
suitable actionPerformed() method. You can
determine which button was pushed by calling the
getSource() method on the ActionEvent
object passed into the actionPerformed()
method, and then casting it to a JButton.

Quiz
Test your knowledge of icons, labels, and buttons in
this quiz.

Exercise
Click the Exercise button to create an application
that uses buttons.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module5/buttons.html (2 of 2) [1/11/2000 1:18:23 PM]


Sidebar: Alignment constants

Working with labels


Alignment constants
Some Swing components rely on alignment
constants to determine how their contents are
positioned. For example, the text and icon within a
label are aligned based on the alignment constant
passed into the JLabel constructor, or by passing
constants into a pair of methods on a JLabel object
after it has been created.
These alignment constants are all defined in the
SwingConstants interface, which is
implemented by any Swing components that require
alignment. Following are the alignment constants
defined in the SwingConstants interface:
LEFT RIGHT
TOP BOTTOM
CENTER EAST
SOUTH_EAST SOUTH
SOUTH_WEST WEST
NORTH_WEST NORTH
NORTH_EAST HORIZONTAL
VERTICAL LEADING
TRAILING

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module5/labels.sb.html [1/11/2000 1:18:23 PM]


Exercise: Using the label component

Exercise

Working with labels


Using the label component
Objective
Create a label component that utilizes text and an icon.
Exercise scoring
The full credit for this exercise is 10 points. You'll submit your source code to the course tutors.
Instructions
Create an application named LabelTest that creates three different labels. These labels should
demonstrate the following:
● A simple text label

● An icon label

● A combination text/icon label with the text centered below the icon

You can use a GIF or JPEG image of your choice for the icon and any text that you choose. Also, set the
layout for the application's content pane to a 1x3 grid layout.
Source files
We've provided some sample images for you to use in the sample images folder of the course download
available from the Resources page.

What to submit to the course tutors


In the text box below, cut and paste the source code for LabelTest. Click OK, I'm Done to submit the
code to the course tutors.

file:///c|/courses/jv301_r/module5/labels.ex.html (1 of 2) [1/11/2000 1:18:23 PM]


Exercise: Using the label component

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module5/labels.ex.html (2 of 2) [1/11/2000 1:18:23 PM]


Lesson 5.5: Jazzing things up with borders

Jazzing things up with borders


Improve the look of components with
borders.

The overall look of a GUI can be improved with


Transcript borders, which are special classes that determine the
appearance of the outer edge of components. Like
icons, borders aren't actually components.
Java's border classes are located in the
javax.swing.border package, and include
support for the following types of borders:

Transcript

You set the border for a component by calling the


setBorder() method and passing in a border
object. For example, the following code sets the
border for a label to a line border that is magenta in
color and 3 pixels wide:

label.setBorder(
new LineBorder(Color.magenta, 3));

The EtchedBorder, BevelBorder, and


SoftBevelBorder classes all accept a parameter
indicating whether the border is to be raised or
lowered in appearance. Following are examples of
setting each type of appearance:

label1.setBorder(
new EtchedBorder(EtchedBorder.RAISED));

file:///c|/courses/jv301_r/module5/borders.html (1 of 2) [1/11/2000 1:18:24 PM]


Lesson 5.5: Jazzing things up with borders

label2.setBorder(
new BevelBorder(BevelBorder.LOWERED));

Finally, the MatteBorder class accepts a tiled


icon that is used to draw the border around a
component:

label.setBorder(
new MatteBorder(new ImageIcon("Heart.gif")));

Compound Java supports two other types of borders that are


and titled built around the types you just learned about:
borders compound borders and titled borders. Compound
borders are represented by the CompoundBorder
class, and allow you to nest a border within the insets
of another border. Titled borders are represented by
the TitledBorder class, and allow you to place
title text along the edge of a border.

Click the View Example button to see sample


compound and titled borders.

Exercise
In this exercise, you’ll modify the ButtonTest
application so that it uses borders.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module5/borders.html (2 of 2) [1/11/2000 1:18:24 PM]


file:///c|/courses/jv301_r/module5/images/buttons1.gif

file:///c|/courses/jv301_r/module5/images/buttons1.gif [1/11/2000 1:18:24 PM]


Quiz: Icons, labels, and buttons

Quiz
Icons, labels, and buttons

Each question is worth one point. Some questions ask you to select the
best answer, others ask you to select all the correct answers. To receive
credit for questions asking for all the correct answers, you must select all
the correct answers and only the correct answers.

1.The general characteristics of a Swing icon are defined by:


Please select the best answer.
A. The Icon interface
B. The Icon class
C. The ImageIcon class
2.A two-state button that remains pushed-in after you click it is known as:
Please select the best answer.
A. A radio button
B. A toggle button
C. A sticky button
3.What type of event does a button generate when it is pushed?
Please select the best answer.
A. Item event
B. Action event
C. Push event

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module5/buttons.qz.html [1/11/2000 1:18:24 PM]


Exercise: Tinkering with buttons

Exercise

Using buttons
Tinkering with buttons
Objective
Create several button components and respond to their action events.
Exercise scoring
The full credit for this exercise is 5 points. You'll submit your source code to the course tutors.
Instructions
Write an application named ButtonTest that creates three different buttons using the JButton class.
The buttons should demonstrate the following:
● A simple text button

● An icon button

● A combination text/icon button

You can use a GIF or JPEG image of your choice for the icon and any text that you choose. Also, set the
layout for the application's content pane to a 1x3 grid layout.
Respond to the button events by registering the application as an action event listener and implementing
the actionPerformed() method. Check to see which button was pressed by calling getSource()
on the event object and comparing the result to each of the button objects. Print a string to standard
output indicating the button that was pressed.
Source files
We've provided some sample images for you to use in the sample images folder of the course download
available from the Resources page.

What to submit to the course tutors


In the text box below, cut and paste the source code for ButtonTest. Click OK, I'm Done to submit
your code to the course tutors.

file:///c|/courses/jv301_r/module5/buttons.ex.html (1 of 2) [1/11/2000 1:18:24 PM]


Exercise: Tinkering with buttons

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module5/buttons.ex.html (2 of 2) [1/11/2000 1:18:24 PM]


Lesson 5.6: Lists

Lists
Use lists to manage the selection of items.

Lists are GUI components that manage lists of items.


The user selects items from a list by clicking them
with the mouse or navigating through the list with the
arrow keys on the keyboard. Swing supports lists with
the JList class.

Using lists Lists are used to list a series of text and/or icon items
from which the user can select one or more items.
You can create a list from an array by passing the
array into the constructor for the JList object:

String[] months = { "January", "February", "March",


"April", "May", "June", "July", "August",
"September", "October", "November", "December" };
JList list = new JList(months);
contentPane.add(list);

Flip through the following figures to see various


features of lists:

Transcript

If you simply create a list and add it to a pane, the list


won't support scrolling. To support scrolling, you
must create a JScrollPane object and pass the list
into its constructor:

JScrollPane scrollPane = new JScrollPane(list);


contentPane.add(scrollPane);

file:///c|/courses/jv301_r/module5/lists.html (1 of 2) [1/11/2000 1:18:24 PM]


Lesson 5.6: Lists

To select an item in a list, call the


setSelectedIndex() method and pass in the
zero-based index of the list item:
list.setSelectedIndex(2);
You can also select multiple items in a list by passing
the starting and ending indices of the items to the
setSelectionInterval() method:

list.setSelectionInterval(2, 4);

List events Lists generate list selection events when you select an
item in a list. To handle a list event, you simply
implement the ListSelectionListener
interface, which defines a single method:
valueChanged(). Following is an example of
printing the list item that was selected using the
getSelectedValue() method in the JList
class:

public void valueChanged(ListSelectionEvent e) {


System.out.println(list.getSelectedValue());
}

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module5/lists.html (2 of 2) [1/11/2000 1:18:24 PM]


Sidebar: The ramifications of using borders (Audio transcript)

Jazzing things up with borders


The ramifications of using borders
(Audio transcript)
Borders might not seem like that big of a deal to
you at this point because they really only determine
how the outer edge of a component is drawn.
However, you have to consider the ramifications of
using borders throughout an entire user interface. By
carefully utilizing bevel and etched borders, for
example, you can create intuitive and visually
appealing user interfaces with a 3D look.
More specifically, you could set the bevel border for
some components to have a raised look, while others
could be lowered. Although you and I both know
that the components aren't really raised or lowered,
the visual effect can significantly help the
organization of a user interface.

Users have come to expect slick user interfaces in


modern graphical applications. Borders help make
this job a little easier, so you should aspire to give
them what they want.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module5/borders_at.sb.html [1/11/2000 1:18:25 PM]


Sidebar: Java's border classes (FlipBook transcript)

Jazzing things up with borders


Java's border classes (FlipBook
transcript)
Java's border classes include support for the
following types of borders:

file:///c|/courses/jv301_r/module5/borders_fb.sb.html (1 of 3) [1/11/2000 1:18:25 PM]


Sidebar: Java's border classes (FlipBook transcript)

file:///c|/courses/jv301_r/module5/borders_fb.sb.html (2 of 3) [1/11/2000 1:18:25 PM]


Sidebar: Java's border classes (FlipBook transcript)

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module5/borders_fb.sb.html (3 of 3) [1/11/2000 1:18:25 PM]


file:///c|/courses/jv301_r/module5/images/images1.gif

file:///c|/courses/jv301_r/module5/images/images1.gif [1/11/2000 1:18:25 PM]


Exercise: Improving the look of components with borders

Exercise

Jazzing things up with borders


Improving the look of components with borders
Objective
Improve the look of the buttons by setting their borders.
Exercise scoring
This exercise is worth a total of 1 point. You don't have to submit your code to the course tutors to
receive credit for this exercise.
Instructions
Modify the ButtonTest application from the previous lesson so that the buttons use the following
borders:
● Line border - red, 10 pixels wide

● Etched border - lowered

● Soft bevel border - raised

Don't forget to import the javax.swing.border package.

After completing the exercise, click the OK, I'm Done button and the system will award you full credit.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module5/borders.ex.html [1/11/2000 1:18:25 PM]


Lesson 5.7: Combo boxes

Combo boxes
Use combo boxes to manage the selection
of items.

Combo boxes are GUI components that also


manage lists of items. As with lists, the user selects
items from a combo box using the mouse or arrow
keys. Swing supports combo boxes with
JComboBox class.

Using combo Combo boxes are similar to lists in that they provide
boxes a GUI for selecting items from a list. Unlike lists,
combo boxes only display a single item. Multiple
items are revealed in a drop-down window when you
activate the combo box. Combo boxes are limited in
that they only allow one item to be selected at a time.
On the other hand, combo boxes are flexible in that
they can allow users to manually enter new items.
The JComboBox class is similar in many ways to
JList, which means that working with combo
boxes in Java code is similar to using lists.
Flip through the following frames to view some
sample combo boxes:

Transcript

Exercise
Click the Exercise button to create a list.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module5/comboboxes.html [1/11/2000 1:18:25 PM]


Sidebar: Various features of lists (FlipBook transcript)

Lists
Various features of lists (FlipBook
transcript)

file:///c|/courses/jv301_r/module5/lists_fb.sb.html (1 of 2) [1/11/2000 1:18:25 PM]


Sidebar: Various features of lists (FlipBook transcript)

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module5/lists_fb.sb.html (2 of 2) [1/11/2000 1:18:25 PM]


Sidebar: Scrolling components

Lists
Scrolling components
Swing makes it very easy to support scrolling in a
component by using the JScrollPane class. The
JScrollPane class implements a scrolling pane
that serves as a container for components that require
scrolling functionality.

The JScrollPane class assesses the scrolling


needs of a component and determines whether it
should display scroll bars for altering the
component's view space. In most cases, you can rely
on the default behavior of the JScrollPane class
and avoid writing custom scrolling code.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module5/lists.sb.html [1/11/2000 1:18:25 PM]


Lesson 5.8: Other Swing components

Other Swing components


Review other Swing components.

In addition to the components you've learned about


throughout this module, Swing also supports the
following components:
table JTable
JTextField,
text components JPasswordField,
JTextArea, JTextPane
scroller JScrollBar, JScrollPane
slider JSlider
JProgressBar,
progress bar
JProgressMonitor
tree JTree

Flip through the following figures to view these


Swing components in action:

Transcript

The details of using these components could get a


little long. However, I think you'll find them fun to
explore and easy to use on your own, especially
considering that you're already accustomed to using
basic components such as labels, buttons, borders,
and lists.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module5/otherswing.html [1/11/2000 1:18:26 PM]


Sidebar: Sample combo boxes (FlipBook transcript)

Combo boxes
Sample combo boxes (FlipBook
transcript)

file:///c|/courses/jv301_r/module5/comboboxes_fb.sb.html (1 of 2) [1/11/2000 1:18:26 PM]


Sidebar: Sample combo boxes (FlipBook transcript)

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module5/comboboxes_fb.sb.html (2 of 2) [1/11/2000 1:18:26 PM]


Exercise: Presenting a list of choices

Exercise

Combo boxes
Presenting a list of choices
Objective
Create a list that presents the user with a set of choices.
Exercise scoring
The full credit for this exercise is 5 points. You'll submit your source code to the course tutors.
Instructions
1. Write an application named ListTest that creates a list and fills it with items that you find
interesting.
2. Write a list selection event response method that prints the name of the item selected from the list
to standard output. Be sure to use a scroll pane so that the list supports scrolling.
What to submit to the course tutors
In the text box below, cut and paste the source code for ListTest. Click OK, I'm Done to submit the
code to the course tutors.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module5/comboboxes.ex.html [1/11/2000 1:18:26 PM]


Lesson 5.9: Module wrap-up

Using Swing components


Module wrap-up

This module tackled the exciting topic of Swing


components. The module began by introducing you
to icons, which aren't components themselves, but
nonetheless play a vital role in constructing some
components. You then learned about two
fundamental components: labels and buttons.
From there, you explored borders, which are used to
improve the look of other components. You then
examined lists and combo boxes, which provide a
means of selecting items from a list. Finally, you
finished up the module by taking a look at some of
the other Swing components available for jazzing up
your GUIs.
Please feel free to explore Swing's full component
set now that you've gained experience using some of
the more popular components.
This module discussed how the following terms and
concepts relate to Java:
icon
label
button
border
list
combo box
table
text component
slider

file:///c|/courses/jv301_r/module5/wrap.html (1 of 2) [1/11/2000 1:18:26 PM]


Lesson 5.9: Module wrap-up

progress bar
tree

Discussion
Do you have any questions or comments about the
material covered so far in this course? If so, click the
Discuss button on the toolbar, locate the module
topic folder that pertains to your question or
comment, and read through the discussions to see if
someone has already asked your question. If not,
post it yourself.

Quiz
Click the Quiz button to test what you’ve learned in
this module.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module5/wrap.html (2 of 2) [1/11/2000 1:18:26 PM]


Sidebar: Swing components in action (FlipBook transcript)

Other Swing components


Swing components in action
(FlipBook transcript)

file:///c|/courses/jv301_r/module5/otherswing_fb.sb.html (1 of 3) [1/11/2000 1:18:26 PM]


Sidebar: Swing components in action (FlipBook transcript)

file:///c|/courses/jv301_r/module5/otherswing_fb.sb.html (2 of 3) [1/11/2000 1:18:26 PM]


Sidebar: Swing components in action (FlipBook transcript)

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module5/otherswing_fb.sb.html (3 of 3) [1/11/2000 1:18:26 PM]


Lesson 6.1: Digging deeper into Swing

Digging deeper into Swing


Module introduction
This module further explores how to construct some
of the modern GUI features. You'll learn to create
and use menus, toolbars, and tool tips. You'll also
coordinate common actions between menus and
toolbars.

Module After completing the module, you'll have the skills


learning and knowledge necessary to:
objectives ● Create menus with different types of menu
items
● Set keyboard mnemonics and accelerators for
menu commands
● Create toolbars with different types of buttons

● Coordinate menu and toolbar commands

● Use tool tips

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module6/intro.html [1/11/2000 1:18:26 PM]


Quiz: Module quiz

Quiz
Module quiz

Each question is worth one point. Some questions ask you to select the
best answer, others ask you to select all the correct answers. To receive
credit for questions asking for all the correct answers, you must select all
the correct answers and only the correct answers.

1.Which one of the following classes represents a Swing component?


Please select the best answer.
A. Icon
B. LineBorder
C. JLabel
2.Which one of the following classes allows you to combine two borders
together?
Please select the best answer.
A. CompoundBorder
B. TitledBorder
C. DualBorder
3.What method in the ListSelectionListener interface is called in
response to the user selecting an item in a list?
Please select the best answer.
A. valueChanged()
B. itemSelected()
C. listSelection()
4.To add scrolling support to a component, you must create the component
and pass it into the constructor of:
Please select the best answer.
A. A JScrollBar object.
B. A JScrollPane object.
C. A JScroller object.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module5/wrap.qz.html [1/11/2000 1:18:27 PM]


Lesson 6.2: Menus

Menus
Examine menus and their role in Swing
GUIs.

Swing provides a suite of classes for creating


almost any menu you could possibly need. The
JMenuBar class forms the basis for Swing menus
and serves as the main menu bar for an application.
The JMenu class represents individual drop-down
menus in menu bars. Items within a drop-down menu
can be one of the following types:

Transcript

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module6/menus.html [1/11/2000 1:18:27 PM]


Lesson 6.3: Menu creation

Menu creation
Create a menu.

Flip through the following frames to learn how to


create a menu:

Transcript

The JCheckBoxMenuItem and


JRadioButtonMenuItem classes work very much like
JMenuItem in that they can be created as text-only or as
text with an icon.

Exercise
Click the Exercise button to add a menu to the
Doodle application.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module6/creation.html [1/11/2000 1:18:27 PM]


Sidebar: Drop-down menu types (FlipBook transcript)

Menus
Drop-down menu types (FlipBook
transcript)
Items within a drop-down menu can be one of the
following types:

file:///c|/courses/jv301_r/module6/menus_fb.sb.html (1 of 2) [1/11/2000 1:18:27 PM]


Sidebar: Drop-down menu types (FlipBook transcript)

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module6/menus_fb.sb.html (2 of 2) [1/11/2000 1:18:27 PM]


Lesson 6.4: Using keyboard mnemonics and accelerators

Using keyboard mnemonics and


accelerators
Use keyboard mnemonics and accelerators
to provide a quick way to access GUI
components.

It is very important to provide a keyboard interface


for interacting with menus. This support is
accomplished through keyboard mnemonics and
accelerators.

A mnemonic allows you to navigate through the


hierarchy of a menu and select a menu item or
invoke a menu command. A mnemonic is often used
in conjunction with the Alt key. An underlined
character in the name of a menu item identifies a
mnemonic:

An accelerator provides a means of directly


accessing a specific menu item without navigating
through the menu hierarchy. An accelerator
key-combination is identified to the right of a menu
item:

file:///c|/courses/jv301_r/module6/mnemonics.html (1 of 2) [1/11/2000 1:18:28 PM]


Lesson 6.4: Using keyboard mnemonics and accelerators

Setting To set the mnemonic and accelerator for a given


mnemonics and menu item, you simply call the setMnemonic()
accelerators and setAccelerator() methods on the item:

Transcript

You can also use other key masks such as


SHIFT_MASK and ALT_MASK when setting the
accelerator.

Exercise
In this exercise, you’ll add support for keyboard
mnemonics and accelerators to the menu items in
Doodle.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module6/mnemonics.html (2 of 2) [1/11/2000 1:18:28 PM]


Sidebar: Creating a menu (FlipBook transcript)

Menu creation
Creating a menu (FlipBook
transcript)

file:///c|/courses/jv301_r/module6/creation_fb.sb.html (1 of 2) [1/11/2000 1:18:28 PM]


Sidebar: Creating a menu (FlipBook transcript)

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module6/creation_fb.sb.html (2 of 2) [1/11/2000 1:18:28 PM]


Exercise: Adding a menu to the Doodle application

Exercise

Menu creation
Adding a menu to the Doodle application
Objective
Add a menu to the Doodle application to support drawing commands.
Exercise scoring
Full credit for this exercise is 10 points. You'll submit your source code to the course tutors.
Instructions
Add code to the constructor of the Doodle class to create a menu. This menu should provide commands
for setting the current shape type.
In addition, create a menu command for selecting the drawing color. The actionPerformed() event
response method can simply print a string to standard output when the Select Color command is invoked,
since you won't be adding color selection functionality to Doodle until later in the course.
Following are the specific menu items you'll be creating:
● A Draw drop-down menu

● Line, Rectangle, Triangle, and Oval radio button menu items

● A separator between the shape type and color selection menu items

● A Select Color menu item

Hints
To create a separator (space) between menu items, call the addSeparator() method on the JMenu
object, like this:
drawMenu.addSeparator();
Keep in mind that the radio button menu items on the menu work very much like normal radio buttons.
You'll need to add them to a ButtonGroup object for them to be mutually exclusive. Also, you’ll need
to call addActionListener() on each menu item to register the application. This way, the
application will receive action events from the items. Note: After adding the menu to Doodle there are
now two different ways to set the current shape type. You may notice that the current shape type
indicated by the menu items is not synchronized with the current shape type indicated by the buttons
along the top of the content pane. That's ok for now. You will have an opportunity to correct this a little
later on in the course.

file:///c|/courses/jv301_r/module6/creation.ex.html (1 of 2) [1/11/2000 1:18:28 PM]


Exercise: Adding a menu to the Doodle application

What to submit to the course tutors


In the text box below, cut and paste the new source code for the Doodle class. Click OK, I'm Done to
submit the code to the course tutors.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module6/creation.ex.html (2 of 2) [1/11/2000 1:18:28 PM]


Lesson 6.5: Working with toolbars

Working with toolbars


Create and use toolbars that contain
buttons.

Toolbars have become about as standard as menus


in modern graphical applications. Toolbars provide a
means of accessing commonly-used application
features from an array of buttons or other
components. Toolbars can be positioned anywhere in
an application's frame but the top is the most popular
location.

Creating toolbars The JToolBar class represents a Swing toolbar


that acts as a container. Creating a toolbar using the
JToolBar class is as simple as constructing an
object and adding components to it:

Transcript

file:///c|/courses/jv301_r/module6/toolbars.html (1 of 2) [1/11/2000 1:18:28 PM]


Lesson 6.5: Working with toolbars

Since toolbar buttons typically don't contain text,


they don't have an action command associated with
them by default. You must explicitly set the action
command for icon toolbar buttons so that they have
the appropriate command string when they generate
action events. This step is accomplished by calling
the setActionCommand() method:

button.setActionCommand("Edit");

Quiz
In this quiz, you’ll test your knowledge of menu and
toolbar basics.

Exercise
Click the Exercise button to add a toolbar to the
Doodle application.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module6/toolbars.html (2 of 2) [1/11/2000 1:18:28 PM]


Sidebar: Setting the mnemonic and accelerator (MouseOver transcript)

Using keyboard mnemonics and accelerators


Setting the mnemonic and
accelerator (MouseOver transcript)

This code sets the mnemonic character for the Copy


menu item to C, and then sets the accelerator to the
key-combination Ctrl+C.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module6/mnemonics_mo.sb.html [1/11/2000 1:18:28 PM]


Exercise: Supporting keyboard mnemonics and accelerators in Doodle

Exercise

Using keyboard mnemonics and accelerators


Supporting keyboard mnemonics and accelerators in Doodle
Objective
Add support for keyboard mnemonics and accelerators to Doodle.
Exercise scoring
Full credit for this exercise is 5 points. You'll submit your source code to the course tutors.
Instructions
Add support for keyboard mnemonics and accelerators to the menu items in the Doodle application. You
can use your own judgment to determine suitable mnemonic characters and accelerator key
combinations. Keep in mind, however, that they should be intuitive for the user.
What to submit to the course tutors
In the text box below, cut and paste the new source code for the new Doodle class. Click OK, I'm Done
to submit the code to the course tutors.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module6/mnemonics.ex.html [1/11/2000 1:18:28 PM]


Lesson 6.6: Coordinating GUI actions

Coordinating GUI actions


Coordinate GUI actions among multiple
components.

A common problem in GUI applications is


Transcript coordinating GUI actions that are common to
multiple components. For example, you may have a
menu item for the Paste command as well as a
toolbar button. It's important for these two GUI
elements to generate the same events and share the
same state information (enabled, disabled, etc.).
Swing provides a special interface called Action
that solves this problem. This interface allows you to
create general action commands and then add them
to a toolbar or menu. The appropriate GUI element is
automatically created and a common event listener is
registered.

This code shows how a single paste action is created


and added to a menu and a toolbar:

Transcript

Sharing action If you don't care about matching GUI elements, there
commands is a simpler approach to coordinating actions among
multiple components. All you have to do is set the
action command strings to match. In fact, you've
probably already done this in the Doodle application
when you set the action commands for the toolbar
buttons. That is why the single
actionPerformed() method responds to both
menu item and toolbar button events.

file:///c|/courses/jv301_r/module6/gui.html (1 of 2) [1/11/2000 1:18:28 PM]


Lesson 6.6: Coordinating GUI actions

Exercise
In this exercise, you’ll coordinate GUI actions in
Doodle.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module6/gui.html (2 of 2) [1/11/2000 1:18:28 PM]


Sidebar: Using the JToolBar class (MouseOver transcript)

Working with toolbars


Using the JToolBar class
(MouseOver transcript)

In this code, a toolbar is created and three icon


buttons are added to it. Notice that unlike menus,
toolbars are added to the content pane of a frame
window as opposed to calling a special method to set
them.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module6/toolbars_mo.sb.html [1/11/2000 1:18:29 PM]


Quiz: Menu and toolbar basics

Quiz
Menu and toolbar basics

Each question is worth one point. Some questions ask you to select the
best answer, others ask you to select all the correct answers. To receive
credit for questions asking for all the correct answers, you must select all
the correct answers and only the correct answers.

1.You set the menu bar for an application by:


Please select the best answer.
A. Adding the menu bar object to the content pane
B. Calling the setJMenuBar() method
C. Calling the addJMenuBar() method
2.A keyboard mnemonic is identified by:
Please select the best answer.
A. An icon displayed to the right of a menu item
B. Text displayed to the right of a menu item
C. An underlined character in the name of a menu item
3.How do you set the action command for an icon toolbar button?
Please select the best answer.
A. You call the setActionCommand() method.
B. You pass the action command to the button's constructor.
C. The action command is automatically set to the name of the
icon image.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module6/toolbars.qz.html [1/11/2000 1:18:29 PM]


Exercise: Adding a toolbar to the Doodle application

Exercise

Working with toolbars


Adding a toolbar to the Doodle application
Objective
Add a toolbar to Doodle.
Exercise scoring
Full credit for this exercise is 10 points. You'll submit your source code to the course tutors.
Instructions
1. Replace the button panel code in the constructor of the Doodle class with new code that creates a
toolbar. You need to create toggle buttons in the toolbar that set the current shape type.
2. Create a toolbar button for selecting the drawing color.
Following are the specific toolbar buttons you'll be creating:
❍ Line, Rectangle, Triangle, and Oval toggle buttons

❍ A separator between the shape type and color selection buttons

❍ A Select Color button

Source files
The GIF images for these buttons are available for download from the Resources page. The specific
images for this exercise can be found in the toolbar images folder. You’re also welcome to create your
own.
Hints
To create a separator (space) between toolbar buttons, call the addSeparator() method on the
JToolBar object, like this: toolbar.addSeparator();
Use the JToggleButton class for the toggle buttons that set the shape type. Keep in mind that the
toggle buttons on the toolbar should work like radio buttons. You’ll need to add them to a
ButtonGroup object for them to be mutually exclusive.
Use the JButton class for the select color button.
You will need to call addActionListener() on each toolbar button to register the application so
that it receives action events from the items.
Make sure to set the action command for the buttons by calling the setActionCommand() method on each

file:///c|/courses/jv301_r/module6/toolbars.ex.html (1 of 2) [1/11/2000 1:18:29 PM]


Exercise: Adding a toolbar to the Doodle application

of them.
What to submit to the course tutors
In the text box below, cut and paste the new code for the Doodle class. Click OK, I'm Done to submit
your code to the course tutors.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module6/toolbars.ex.html (2 of 2) [1/11/2000 1:18:29 PM]


Lesson 6.7: Putting tool tips to use

Putting tool tips to use


Use tool tips to provide help information for
components.

It is often helpful to provide tool tips so users can


quickly determine the usage of a button. A tool tip is
a small text window that appears when the user
pauses with the mouse cursor over a component.
Since tool tip windows are designed to be
unobtrusive, the text associated with a tool tip should
be very concise—one or two words is ideal.

Flip through the following frames to see some


sample tool tips displayed:

Transcript

Setting Tool tips are very easy to set for a component. A


tool tips simple call to the setToolTipText() method is
all that is required:
button.setToolTipText("Copy");

This code sets the tool tip text for a button to the
string "Copy", which is displayed whenever the
user pauses with the mouse cursor over the button.

Although tool tips are primarily used with icon buttons,


you can assign tool tips with any Swing component to help
make its usage clearer.

file:///c|/courses/jv301_r/module6/putting.html (1 of 2) [1/11/2000 1:18:29 PM]


Lesson 6.7: Putting tool tips to use

Exercise
In this exercise, you’ll add tool tips to Doodle.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module6/putting.html (2 of 2) [1/11/2000 1:18:29 PM]


Sidebar: Clarifying the purpose of actions (Audio transcript)

Coordinating GUI actions


Clarifying the purpose of actions
(Audio transcript)
I want to clarify the purpose of actions just in
case you're still confused by them. Actions come into
play when more than one GUI element invokes the
same command within an application. A good
example is the File Save command that is found in
most applications. You would typically have a menu
item and a toolbar button for this command. You
already know that you can set the two GUI elements
to the same action command for them to generate the
same action event. However, what if you want to
disable them because no changes have been made?
This is where the real power of actions is revealed.

By using actions, the two GUI elements are


effectively handled as one. You can enable or disable
the action itself instead of the GUI elements. These
settings are then automatically reflected in all of the
GUI elements that were created from the action. The
primary limitation of actions is that they can only be
used to create JMenuItem and JButton objects.
In other words, you can't use them with radio buttons
or check boxes, which would be handy in Doodle.
Even so, actions provide a clean way to synchronize
the appearance and functionality of menu items and
toolbar buttons.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module6/gui_at.sb.html [1/11/2000 1:18:29 PM]


Sidebar: Creating the paste action (MouseOver transcript)

Coordinating GUI actions


Creating the paste action
(MouseOver transcript)

The PasteAction class is a class you must create


that implements the Action interface.

The call to setEnabled() results in both the


Paste menu item and the Paste toolbar button being
disabled.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module6/gui_mo.sb.html [1/11/2000 1:18:29 PM]


Exercise: Coordinating GUI actions in Doodle

Exercise

Coordinating GUI actions


Coordinating GUI actions in Doodle
Objective
Coordinate GUI actions in Doodle so that the menu and toolbar are consistent.
Exercise scoring
This exercise is worth a total of 1 point. You don't have to submit your code to the course tutors to
receive credit for this exercise.
Overview
Although the menu items and toolbar buttons in the Doodle application have matching action events, you
may have noticed that they aren't coordinated in terms of maintaining a consistent state. For example,
when you select Rectangle as the shape type from the menu, the Rectangle toolbar button doesn't become
selected to reflect the change.
Fortunately, this inconsistency is easy to fix. All you have to do is call the setSelected() method on
the appropriate menu items and toolbar buttons in response to commands in the actionPerformed()
method.
Instructions
Following is an example of setting the menu item and toolbar button for the Line command:

if (cmd.equals("Line")) {
pad.setshapeType(DoodleShape.LINE);
lineItem.setSelected(true);
lineButton.setSelected(true);
}

Modify the actionPerformed() method in Doodle so that all of the graphic type commands are
properly coordinated.

After completing the exercise, click the OK, I'm Done button and the system will award you full credit.

file:///c|/courses/jv301_r/module6/gui.ex.html (1 of 2) [1/11/2000 1:18:30 PM]


Exercise: Coordinating GUI actions in Doodle
Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module6/gui.ex.html (2 of 2) [1/11/2000 1:18:30 PM]


Lesson 6.8: Module wrap-up

Digging deeper into Swing


Module wrap-up

This module described some interesting Swing


features that are found in most modern GUI
applications. You began the module by learning how
to create menus. You then learned how important it
is to provide a keyboard interface for accessing menu
items, which is made possible by mnemonics and
accelerators.
From there, you learned how to create toolbars and
coordinate GUI actions between toolbars and menus.
You finished up the module by learning how to
assign tool tips to components.
This module discussed how the following terms and
concepts relate to Java:
menu
mnemonic
accelerator
toolbar
tool tip

Discussion
Do you have any questions or comments about the
material covered so far in this course? If so, click the
Discuss button on the toolbar, locate the module
topic folder that pertains to your question or
comment, and read through the discussions to see if
someone has already asked your question. If not,
post it yourself.

file:///c|/courses/jv301_r/module6/wrap.html (1 of 2) [1/11/2000 1:18:30 PM]


Lesson 6.8: Module wrap-up

Quiz
Click the Quiz button to test what you've learned in
this module.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module6/wrap.html (2 of 2) [1/11/2000 1:18:30 PM]


Sidebar: Sample tool tips (FlipBook transcript)

Putting tool tips to use


Sample tool tips (FlipBook
transcript)

file:///c|/courses/jv301_r/module6/putting_fb.sb.html (1 of 2) [1/11/2000 1:18:30 PM]


Sidebar: Sample tool tips (FlipBook transcript)

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module6/putting_fb.sb.html (2 of 2) [1/11/2000 1:18:30 PM]


Exercise: Supporting tool tips in the Doodle application

Exercise

Putting tool tips to use


Supporting tool tips in the Doodle application
Objective
Add support for tool tips to Doodle.
Exercise scoring
This exercise is worth a total of 1 point. You don't have to submit your code to the course tutors to
receive credit for this exercise.
Instructions
Modify the constructor for the Doodle application so that each toolbar button has a suitable tool tip to
help clarify its usage. Keep in mind that tool tip text should be very concise, preferably just a word or
two.

After completing the exercise, click the OK, I'm Done button and the system will award you full credit.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module6/putting.ex.html [1/11/2000 1:18:30 PM]


Lesson 7.1: Enhancing Swing user interfaces

Enhancing Swing user interfaces


Module introduction
This module tackles some interesting Swing
features that can be used to significantly improve
your applet and application GUIs. You’ll also learn
some techniques that give users more flexibility in
terms of how they interact with your GUIs.

Module After completing the module, you'll have the skills


learning and knowledge necessary to:
objectives ● Explain the relationship between dialog boxes
and option panes
● Create and use option panes

● Put choosers to work gathering information

● Assess the importance of a GUI's look and feel

● Set the look and feel for a GUI

● Play sounds

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module7/intro.html [1/11/2000 1:18:30 PM]


Quiz: Digging deeper into Swing

Quiz
Digging deeper into Swing

Each question is worth one point. Some questions ask you to select the
best answer, others ask you to select all the correct answers. To receive
credit for questions asking for all the correct answers, you must select all
the correct answers and only the correct answers.

1.What do you use to navigate through the hierarchy of a menu and select
a menu item or invoke a menu command?
Please select the best answer.
A. A keyboard mnemonic
B. A keyboard accelerator
C. A hot-key
2.What special construct do you use to coordinate GUI actions that are
common to multiple components?
Please select the best answer.
A. The Action interface
B. The GUIAction class
C. The ActionEvent class
3.How do you add a space between buttons on a toolbar?
Please select the best answer.
A. Create and add a button with no label or icon to the toolbar.
B. Call the addSeparator() method on the toolbar object.
C. Create a JSeparator object and add it to the toolbar.
4.Which one of the following strings would best serve as tool tip text for a
toolbar button that allows the user to browse for and open a text file?
Please select the best answer.
A. Open a text file
B. Browse for a file and open it
C. Open file

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module6/wrap.qz.html [1/11/2000 1:18:31 PM]


Lesson 7.2: Dialog boxes and option panes

Dialog boxes and option panes


Use dialog boxes and option panes to share
information with the user.

You can display and retrieve information by placing


components directly in the content pane of an applet
or application window. However, there are situations
where it is helpful to place these components within
a dialog box.

Dialog boxes provide a means of displaying a


"temporary" user interface that displays or retrieves
information. Option panes are standard dialog boxes
that make it easier to accomplish certain tasks. These
panes save you the task of constructing a custom
dialog box GUI.

Swing's support Swing supports dialog boxes through the JDialog


for dialog boxes class, which contains a content pane to which you
and option panes add dialog box components. Constructing a custom
dialog box GUI is very similar to constructing an
application GUI through the content pane of a frame
window.

Swing supports option panes through the


JOptionPane class, which provides a variety of
different static methods that can be used to create
standard dialog boxes such as message dialog boxes,
confirmation dialog boxes, and input dialog boxes.

Click the View Example button to see some standard


dialog boxes.

file:///c|/courses/jv301_r/module7/dialog.html (1 of 2) [1/11/2000 1:18:31 PM]


Lesson 7.2: Dialog boxes and option panes

Swing also supports choosers, which are standard dialog


boxes tailored to very specific tasks such as browsing for a
file or selecting a color. You'll learn more about choosers
later in the module.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module7/dialog.html (2 of 2) [1/11/2000 1:18:31 PM]


Lesson 7.3: Using option panes

Using option panes


Present information to the user with option
panes.

The most straightforward way to use option panes is


to call one of the static methods defined in the
JOptionPane class:
showMessageDialog()
showConfirmDialog()
showInputDialog()
showOptionDialog()

These methods all take similar parameters used to


customize the appearance and functionality of the
dialog boxes. For example, each method accepts a
"message type" parameter that determines the look
and feel of the dialog, along with the default icon
displayed. This parameter can be one of the following
constants: ERROR_MESSAGE,
INFORMATION_MESSAGE, WARNING_MESSAGE,
QUESTION_MESSAGE, or PLAIN_MESSAGE.

Following is an example of creating a message dialog


box:

JOptionPane.showMessageDialog(this,
"You are running low on memory.",
"Memory warning", JOptionPane.WARNING_MESSAGE);

file:///c|/courses/jv301_r/module7/option.html (1 of 2) [1/11/2000 1:18:31 PM]


Lesson 7.3: Using option panes

Confirmation dialog boxes are different from message


dialog boxes in that they present multiple buttons. The
following constants are used to identify the buttons in
a confirmation dialog: YES_NO_OPTION,
YES_NO_CANCEL_OPTION,
OK_CANCEL_OPTION.

Following is an example of creating a confirmation


dialog box:

JOptionPane.showConfirmDialog(this,
"Are you sure you want to exit the application?",
"Exit application", JOptionPane.YES_NO_OPTION);

The return value of the showConfirmDialog()


method identifies the button that was pressed by the
user: YES_OPTION, NO_OPTION, OK_OPTION,
CANCEL_OPTION, or CLOSED_OPTION.

Creating input and option dialog boxes is similar to


creating message and confirmation dialog boxes. The
main difference is that you provide additional
parameters to the latter two dialogs to specify a set of
options from which the user can select.

Exercise
Add an option pane to Doodle in this exercise.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module7/option.html (2 of 2) [1/11/2000 1:18:31 PM]


Sidebar: Custom dialog boxes

Dialog boxes and option panes


Custom dialog boxes
The JDialog class serves as the container for
creating custom dialog boxes. To create a custom
dialog box, you simply derive a class from the
JDialog class and create an instance of it. You
then create and add components to the dialog object
just as if you were constructing a GUI within the
content pane of a frame window.

In fact, dialogs have content panes that you use just


like you use content panes in frame windows. The
border layout is the default layout used for dialog
content panes, which is also similar to content panes
in frame windows.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module7/dialog.sb.html [1/11/2000 1:18:31 PM]


file:///c|/courses/jv301_r/module7/images/dialogbox1.gif

file:///c|/courses/jv301_r/module7/images/dialogbox1.gif [1/11/2000 1:18:31 PM]


Lesson 7.4: Working with choosers

Working with choosers


Use choosers to provide a consistent GUI
for performing common input tasks.

Choosers are specialized dialog boxes designed to


retrieve information from the user within the context
of a commonly-performed task. For example,
browsing a disk drive or network for a file is a
common task users have to perform. Swing includes
a file chooser that provides a powerful GUI for
performing this task.

There is also a color chooser, which is a very


intuitive GUI used to select colors. The
JFileChooser and JColorChooser classes
implement file and color choosers in Java.

Click the View Example button to see a sample file


chooser and color chooser dialog box.

Using The easiest way to use the Swing color chooser is to


choosers call the static showDialog() method in the
JColorChooser class:

Transcript

To create a file chooser, you must first create an


instance of the JFileChooser class, and then call
the showOpenDialog() or
showSaveDialog() method:

Transcript

file:///c|/courses/jv301_r/module7/choosers.html (1 of 2) [1/11/2000 1:18:31 PM]


Lesson 7.4: Working with choosers

Quiz
Click the Quiz button to test your knowledge of
dialog boxes, option panes, and choosers.

Exercise
In this exercise, you’ll add a color chooser dialog
box to the Doodle application.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module7/choosers.html (2 of 2) [1/11/2000 1:18:31 PM]


Exercise: Displaying information with an option pane

Exercise

Using option panes


Displaying information with an option pane
Objective
Provide information to the user by adding an option pane to Doodle.
Exercise scoring
This exercise is worth a total of 1 point. You don't have to submit your code to the course tutors to
receive credit for this exercise.
Overview
If you recall, the Select Color command in the Doodle application is still unimplemented. It would be
helpful to inform the user of this fact so that they aren't left wondering why nothing happens when they
select the menu item or click the toolbar button.
Instructions
Modify Doodle so that it displays a message dialog box (option pane) to let the user know that the Select
Color command isn't implemented when they select the menu item or click the toolbar button. Use the
appropriate message-type constant to convey that this is an informational dialog box.

After completing the exercise, click the OK, I'm Done button and the system will award you full credit.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module7/option.ex.html [1/11/2000 1:18:31 PM]


Lesson 7.5: Understanding pluggable look and feel

Understanding pluggable look and


feel
Assess Swing's pluggable look and feel
feature.

One of the problems with a cross-platform GUI


such as Swing is that it typically takes the least
common denominator of generic GUI features,
because it has to be capable of working on a variety
of different platforms. Another drawback is that
applications designed with a "generic" GUI all tend
to have a generic look that doesn't mesh well with
the underlying native GUI. Java addresses both of
these problems in Swing with a pluggable look and
feel feature.
By utilizing a pluggable look and feel, a Swing GUI
is capable of querying the underlying native GUI and
taking on its look and feel. This capability means
that a single Java application will look and feel like a
native Windows application when run on Windows,
and a native Macintosh application when run on a
Mac.
Currently, Swing supports four different look and
feels:
Java (Metal)
Windows
Motif
Macintosh

If you don't explicitly set the look and feel of an


applet or application, Swing will automatically use
the Java look and feel. For example, the Doodle
application you’re working on uses the Java look and
feel. The next lesson shows you how to set the look
and feel of an application.

You can set the look and feel of an application to

file:///c|/courses/jv301_r/module7/pluggable.html (1 of 2) [1/11/2000 1:18:32 PM]


Lesson 7.5: Understanding pluggable look and feel

Windows or Motif even if it is run on a different


platform. However, the Macintosh look and feel is
only available on the Macintosh platform.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module7/pluggable.html (2 of 2) [1/11/2000 1:18:32 PM]


file:///c|/courses/jv301_r/module7/images/choosers1.gif

file:///c|/courses/jv301_r/module7/images/choosers1.gif [1/11/2000 1:18:32 PM]


Sidebar: Using the Swing color chooser (MouseOver transcript)

Working with choosers


Using the Swing color chooser
(MouseOver transcript)

The first parameter is the parent container.


The second parameter is the caption to be displayed
in the chooser dialog box.

The third parameter is the color to be initially


selected. The showDialog() method returns the
color that the user selected.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module7/choosers_mo.sb.html [1/11/2000 1:18:32 PM]


Sidebar: Creating a file chooser (MouseOver transcript)

Working with choosers


Creating a file chooser
(MouseOver transcript)

The parameter to showOpenDialog() is the


parent container for the dialog box.

The return value of this method indicates the button


that was pressed by the user to exit the dialog:
APPROVE_OPTION or CANCEL_OPTION. You
can then determine the name of the file selected by
calling the getName() method.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module7/choosers2_mo.sb.html [1/11/2000 1:18:32 PM]


Quiz: Dialog boxes, option panes, and choosers

Quiz
Dialog boxes, option panes, and choosers

Each question is worth one point. Some questions ask you to select the
best answer, others ask you to select all the correct answers. To receive
credit for questions asking for all the correct answers, you must select all
the correct answers and only the correct answers.

1.If you want to present information to the user and require no input from
her, which one of the following JOptionPane methods would you
use?
Please select the best answer.
A. showConfirmDialog()
B. showMessageDialog()
C. showInputDialog()
2.If the user presses the Yes button in a confirmation dialog box, the
showConfirmDialog() method returns the constant:
Please select the best answer.
A. YES_OPTION
B. YES_BUTTON
C. APPROVE_OPTION
3.Which one of the following static methods do you call in
JColorChooser class to invoke the color chooser dialog box?
Please select the best answer.
A. showColorDialog()
B. showOpenDialog()
C. showDialog()

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module7/choosers.qz.html [1/11/2000 1:18:32 PM]


Exercise: Using the color chooser dialog box in Doodle

Exercise

Working with choosers


Using the color chooser dialog box in Doodle
Objective
Use the color chooser dialog box in Doodle to provide the user with an intuitive means of selecting
colors.
Exercise scoring
Full credit for this exercise is 10 points. You'll submit your source code to the course tutors.
Instructions
The color chooser dialog box provides a perfect GUI for implementing the Select Color feature in the
Doodle application. Modify the Doodle application so that it uses the color chooser dialog to allow the
user to select a new shape color.
In addition to adding the color chooser creation code, you must also add some code to the DoodlePad
class that provides access to the shapeColor member variable. To accomplish this task, you need to
create the getShapeColor() and setshapeColor() methods, which you must then call in the
application's actionPerformed() method to get and set the shape color.
What to submit to the course tutors
In the text box below, cut and paste the new source code for both the DoodlePad class and the
Doodle class. Click OK, I'm Done to submit your code to the course tutors.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module7/choosers.ex.html [1/11/2000 1:18:32 PM]


Lesson 7.6: Setting the look and feel

Setting the look and feel


Set the look and feel of a user interface.

The Java API provides a class named UIManager


that includes static methods for setting the look and
feel with relative ease. Following is an example of
using the setLookAndFeel() method to set the
look and feel of an application to the Metal look and
feel.

try {
UIManager.setLookAndFeel(
"javax.swing.plaf.metal.MetalLookAndFeel");
}
catch (Exception e) {
}

In this example, the class name of the Metal look and


feel class is passed into the setLookAndFeel()
method.

The following look and feel classes are supported in


the Java 2 API:

javax.swing.plaf.metal.MetalLookAndFeel
com.sun.java.swing.plaf.windows.WindowsLookAndFeel
com.sun.java.swing.plaf.motif.MotifLookAndFeel
com.sun.java.swing.plaf.mac.MacLookAndFeel

file:///c|/courses/jv301_r/module7/setting.html (1 of 2) [1/11/2000 1:18:33 PM]


Lesson 7.6: Setting the look and feel

If you want your applet or application to assume the


native look and feel of the platform on which it’s
running, you can call the
getSystemLookAndFeelClassName()
method. Following is an example of setting an
application to the native look and feel:

try {
String laf =
UIManager.getSystemLookAndFeelClassName();
UIManager.setLookAndFeel(laf);
}
catch (Exception e) {
}

Although the look and feel of an application can be set


dynamically at any time, you should initially set it in the
main() method for the application before doing anything
else.

Exercise
In this exercise, you’ll modify the Doodle application
so that it assumes the look and feel of the native
platform on which it is running.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module7/setting.html (2 of 2) [1/11/2000 1:18:33 PM]


Lesson 7.7: Playing sound

Playing sound
Use sound to liven up user interfaces.

Although sound has little to do with Swing, I


Transcript thought you might enjoy finishing up the Doodle
application by injecting a little fun into it. Java 2
represents a significant improvement to Java's
support for sound.

The AudioClip The Java 2 API provides an interface named


class AudioClip that represents a digital audio clip in
one of the following formats: AIFF, AU, WAV,
MIDI, or RMF. The Applet class implements the
AudioClip interface.

You create an AudioClip object by calling the


static newAudioClip() method in the Applet
class and passing a URL identifying an audio clip
file. You can then call the play() method to play
the clip. Following is an example of playing the
audio clip stored in a file named Applause.au:

try {
AudioClip clip = Applet.newAudioClip(
new URL("file:" + "Applause.au"));
clip.play();
}
catch (MalformedURLException exception) {
}

You can also loop an audio clip by calling the


loop() method instead of play(). This results in
a clip playing repeatedly until you delete the
AudioClip object or call the stop() method.

file:///c|/courses/jv301_r/module7/sound.html (1 of 2) [1/11/2000 1:18:33 PM]


Lesson 7.7: Playing sound

Exercise
In this exercise, you’ll add sound support to Doodle.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module7/sound.html (2 of 2) [1/11/2000 1:18:33 PM]


Exercise: Setting the look and feel of Doodle

Exercise

Setting the look and feel


Setting the look and feel of Doodle
Objective
Match look and feel of Doodle to the look and feel of the native GUI.

Exercise scoring
This exercise is worth a total of 1 point. You don't have to submit your code to the course tutors to
receive credit for this exercise.

Instructions
Modify the Doodle application so that it assumes the look and feel of the native platform on which it is
running.

After completing the exercise, click the OK, I'm Done button and the system will award you full credit.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module7/setting.ex.html [1/11/2000 1:18:33 PM]


Lesson 7.8: Module wrap-up

Enhancing Swing user interfaces


Module wrap-up

This module explained how to enhance Swing user


interfaces. You began the module by learning about
dialog boxes and option panes, and how they are
used to retrieve information from and present it to
the user. You then learned how to create and use
option panes.
From there, you explored choosers, which provide a
means of performing common GUI information
retrieval tasks such as allowing the user to browse
for a file or select a color. The module then shifted
gears and explained Java's pluggable look and feel
architecture. You learned how to set the look and
feel of an application so that it blends with the native
GUI. You then wrapped up the module with a little
fun by learning how to play audio clips.
This module discussed how the following terms and
concepts relate to Java:
dialog box
option pane
chooser
pluggable look and feel

Discussion
Do you have any questions or comments about the
material covered so far in this course? If so, click the
Discuss button on the toolbar, locate the module
topic folder that pertains to your question or
comment, and read through the discussions to see if
someone has already asked your question. If not,
post it yourself.

file:///c|/courses/jv301_r/module7/wrap.html (1 of 2) [1/11/2000 1:18:33 PM]


Lesson 7.8: Module wrap-up

Quiz
Click the Quiz button to test what you’ve learned in
this module.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module7/wrap.html (2 of 2) [1/11/2000 1:18:33 PM]


Sidebar: Why you call a static method (Audio transcript)

Playing sound
Why you call a static method
(Audio transcript)
It might seem strange that you must call a static
method in the Applet class to create an audio clip
for an application. This arrangement is due to the
fact that versions of Java prior to Java 2 only
supported the playback of audio clips within applets.
Since the Applet class already had support for
loading audio clips, the Java architects apparently
decided not to confuse things by moving it around.

I think it would have made more sense if


AudioClip was a class, not an interface, and then
it could contain the static newAudioClip()
method. Fortunately, loading and playing audio clips
is still very straightforward, so I shouldn't complain
too much. It's inevitable for inconsistencies to
develop in isolated places when an API evolves as
rapidly as Java's.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module7/sound_at.sb.html [1/11/2000 1:18:33 PM]


Exercise: Spicing up Doodle with sound

Exercise

Playing sound
Spicing up Doodle with sound
Objective
Add sound support to Doodle.
Exercise scoring
Full credit for this exercise is 5 points. You'll submit your source code to the course tutors.
Source files
We've supplied two audio clips, Drip.au and Ding.au, in the download file available from the
Resources page. The clips for this exercise can be found in the folder called audio. You're also free to use
audio clips of your own.
Instructions
Modify the Doodle application so that it plays one sound when the user selects a new drawing tool and
another when the user creares a new shape.
Playing sounds for the two actions I mentioned involves adding code to the following places in the
Doodle code:
● the mouseReleased() event response method in the DoodlePad class

● the actionPerformed() event response method in the Doodle class

It doesn't really matter which sound you play in each of these methods, although I found that the Drip
audio clip sounded better when played in response to the user drawing a new graphic (releasing the
mouse button in DoodlePad). Also, keep in mind that there is a slight delay when loading and playing
an audio clip, so it's a good idea to play the clip before doing anything else in the methods.
What to submit to the course tutors
In the text box below, cut and paste the new source code for both the DoodlePad class and the
Doodle class. Click OK, I'm Done to submit your code to the course tutors.

file:///c|/courses/jv301_r/module7/sound.ex.html (1 of 2) [1/11/2000 1:18:33 PM]


Exercise: Spicing up Doodle with sound

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module7/sound.ex.html (2 of 2) [1/11/2000 1:18:33 PM]


Lesson 8.1: Course conclusion

Course conclusion
You’ve reached the end of your journey through
Swing. You now have a solid understanding of the
Swing toolkit and how it is used to build GUIs with
the Java 2 API. You've covered a lot of territory and
amassed a great deal of knowledge that will
undoubtedly prove helpful when you start building
GUIs of your own.

This course is comprehensive in terms of presenting


Transcript
the basics of GUI construction with Swing.
However, it would be impossible to touch on every
detail of the Swing technology in a single course.
Therefore, I encourage you to experiment with
Swing on your own. You'll find that the skills you
developed throughout the course will serve you well
even as you explore more advanced aspects of
Swing.

Course I hope you've enjoyed the course and found it to be


evaluation informative and helpful. Before you leave, please
continue on to the next two lessons and take a few
moments to answer a brief course survey. Your
responses will enable us to improve this course on a
continual basis and deliver the content that you want
to see in a fashion that is best for you.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module8/conclusion.html [1/11/2000 1:18:33 PM]


Quiz: Module quiz

Quiz
Module quiz

Each question is worth one point. Some questions ask you to select the
best answer, others ask you to select all the correct answers. To receive
credit for questions asking for all the correct answers, you must select all
the correct answers and only the correct answers.

1.If you don't explicitly set the look and feel of an applet or application,
Swing will:
Please select the best answer.
A. Default to using a text-based user interface
B. Automatically use the Java look and feel
C. Automatically use the Windows look and feel
2.Which of the following look and feels could you use on a Windows
platform?
Please select the best answer.
A. Windows, Motif, and Mac
B. Windows, Mac, and Java
C. Windows, Java, and Motif
3.What class contains the static methods used to get and set the look and
feel for an application?
Please select the best answer.
A. UIManager
B. JFrame
C. LAFSelector
4.What static method do you call to create and load an audio clip?
Please select the best answer.
A. AudioClip.loadAudioClip()
B. AudioClip.newAudioClip()
C. Applet.newAudioClip()

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module7/wrap.qz.html [1/11/2000 1:18:34 PM]


Lesson 8.2: We'd love your feedback

We'd love your feedback

Please take a couple of minutes to complete the


simple course evaluation on the next page. This is
your chance to let us know how you think we can
improve our courses--we'll really use your input!
Additionally, completing the survey signifies you
have finished the course and may be a requirement
for students with a company account. Finally,
submitting the end of course survey is a critical step
in the CEU process.

Your If you have any comments about your Web-based


comments training experience in general, you can also send us
appreciated your feedback via email.

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module8/feedback.html [1/11/2000 1:18:34 PM]


Sidebar: GUI design and construction with Swing (Audio transcript)

Course conclusion
GUI design and construction with
Swing (Audio transcript)
Before you go, I want to leave you with a few
thoughts regarding GUI design and construction with
Swing. The first thing to understand is that there is
no such thing as a right or wrong GUI design. Your
ultimate goal in designing GUIs is to make them as
intuitive and easy to use as possible. One way to
achieve this goal is to keep things simple. Many of
the most elegant GUIs are relatively simplistic.
Software can be complicated enough without
overburdening users with complex GUIs.

The other related point I want to make is to


experiment with different GUI designs before
settling on a final design. I've designed application
GUIs three or four times over before finally settling
on a design with which I was happy. This process
reveals the true nature of GUI design. It involves an
eye for graphic design as well as programming skills.
Fortunately, Swing's powerful layout features give
you plenty of flexibility to tweak your GUIs until
they are just right. Best of luck in your Swing
designs!

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module8/conclusion_at.sb.html [1/11/2000 1:18:34 PM]


Lesson 8.3: Help us help you learn!

Help us help you learn!

We hope that you found Java 2 GUI Fundamentals


with Swing a valuable and enjoyable learning
experience.
Please take a couple of minutes to answer several
questions regarding the overall organization,
presentation, and quality of the course material. Your
input will enable us to improve this course on a
continual basis and deliver the content that you want
to see in a fashion that is best for you.
On a scale of 1 to 10 (10 being the highest), overall, how do you rate the
course you’ve just taken: Java 2 GUI Fundamentals with Swing?
10

Please read each of the following statements and then select the sentiment
that most closely matches your reaction. Following the statements is the
most important part of this short survey--a space for you to provide
feedback in your own words.
The DigitalThink Web site
1. The pages on the DigitalThink Web site were quick to load.
strongly agree
agree
neutral
disagree
strongly disagree
2. The DigitalThink site is easy to navigate through.
strongly agree
agree
neutral
disagree
strongly disagree
3. I was able to learn all I needed to know about DigitalThink courses
from the information on the site.

file:///c|/courses/jv301_r/module8/evaluation.html (1 of 5) [1/11/2000 1:18:37 PM]


Lesson 8.3: Help us help you learn!

strongly agree
agree
neutral
disagree
strongly disagree
4. The design of the DigitalThink site struck me as confusing and unclear.
strongly agree
agree
neutral
disagree
strongly disagree
5. I have a clear understanding of what DigitalThink does and how
Web-based training at DigitalThink works.
strongly agree
agree
neutral
disagree
strongly disagree
Your course experience
6. The course covered the materials that I expected it would.
strongly agree
agree
neutral
disagree
strongly disagree
7. I found it easy to navigate through the course.
strongly agree
agree
neutral
disagree
strongly disagree
8. The course Syllabus window was a useful tool for moving through the
course.
strongly agree
agree
neutral
disagree
strongly disagree
9. There was too much text to read in most of the lessons.

file:///c|/courses/jv301_r/module8/evaluation.html (2 of 5) [1/11/2000 1:18:37 PM]


Lesson 8.3: Help us help you learn!

strongly agree
agree
neutral
disagree
strongly disagree
10. The quizzes were an important part of my overall course experience.
strongly agree
agree
neutral
disagree
strongly disagree
11. The course audio worked well and I could easily understand the
instructor's spoken comments.
strongly agree
agree
neutral
disagree
strongly disagree
12. The interactive Chat and Discussion areas were an important and fun
part of my overall course experience.
strongly agree
agree
neutral
disagree
strongly disagree
Learning via the Web
13. Web-based learning at DigitalThink enabled me to learn the course
materials quickly and conveniently.
strongly agree
agree
neutral
disagree
strongly disagree
14. My DigitalThink course was a more effective learning method than I
thought it would be.
strongly agree
agree
neutral
disagree

file:///c|/courses/jv301_r/module8/evaluation.html (3 of 5) [1/11/2000 1:18:37 PM]


Lesson 8.3: Help us help you learn!

strongly disagree
15. I would rather learn from a book instead of taking another
DigitalThink course.
strongly agree
agree
neutral
disagree
strongly disagree
16. I would rather take a CD-ROM-based training course than learn from
DigitalThink.
strongly agree
agree
neutral
disagree
strongly disagree
17. I would rather take a classroom-based training course with a live
instructor than learn via the Web using DigitalThink.
strongly agree
agree
neutral
disagree
strongly disagree
18. I would be interested in taking another Web-based course from
DigitalThink.
strongly agree
agree
neutral
disagree
strongly disagree
19. If I needed to learn a new skill for my job, I would check with
DigitalThink to see if they offered a Web-based course on the topic.
strongly agree
agree
neutral
disagree
strongly disagree
20. The best feedback we get comes in the form of your own words. In
two sentences or more, how would you describe your DigitalThink course
experience to a friend?

file:///c|/courses/jv301_r/module8/evaluation.html (4 of 5) [1/11/2000 1:18:37 PM]


Lesson 8.3: Help us help you learn!

Thank you for taking the time to help us make the DigitalThink experience
the greatest way to learn!

submit form reset all answers

Copyright © DigitalThink, Inc. All Rights Reserved

file:///c|/courses/jv301_r/module8/evaluation.html (5 of 5) [1/11/2000 1:18:37 PM]


IT Course Catalog : DigitalThink : Smart Companies Get It

Academic Credit
You can receive academic credit upon successful completion of most
DigitalThink courses.
Students who successfully complete a course receive Continuing
Sampler Courses Education Units (CEUs), which are then applicable (for most courses)
C, C++ Programming towards transcripted college credit at the University of Phoenix. The
Database Concepts number of credits awarded for each course varies with the length of
Design & Publishing the course.
Desktop
IT Management The University of Phoenix is the largest accredited private university in
Internet Literacy the US, and offers on-site classes at locations in twelve states. The
Java Programming UOP Online degree program is available to students worldwide.
Lotus
MS Certification:MCDBA How to earn CEUs from DigitalThink
MS Certification:MCSD
MS Certification:MCSE
Object Programming
How to obtain college credit from the University of Phoenix
Networking
Oracle Frequently Asked Questions
UNIX
Web Programming
Windows Programming

Log In | Sitemap | Contact


Home | Corporate e-Learning Solutions | Course Catalog | About DigitalThink | Support
Copyright © 1996-2000 DigitalThink, Inc. All Rights Reserved Privacy Policy

http://www.staging.digitalthink.com/catalog/credit.html [1/11/2000 1:18:37 PM]


Support : DigitalThink : The Internet Learning Solution

Contact
Use these email addresses to direct your questions or comments to the appropriate person at
DigitalThink.

[email protected] Our Sales Team can set up a Corporate Account to provide Web-based
training to your entire company. We can also produce and deliver custom courses to help your
organization train employees, sales people, or customers.

request support If you're having trouble using your credit card to buy a course, or if you can't
log into a course you're registered for, please contact us.

[email protected] If you'd like to suggest a course topic or an improvement to our


site, send an email to this address.

[email protected] Use this address to get more general information about our company.

[email protected] How do we look? Comments and questions about our site design or
graphics should be sent here.

[email protected] If you've discovered a bug (for example, a broken link or an image that
won't load), let us know. The more details you can give us about the problem, the better.

Copyright © DigitalThink, Inc. All Rights Reserved

http://www.staging.digitalthink.com/company/contact_frame.html [1/11/2000 1:18:38 PM]

You might also like