Skip to content Skip to navigation Skip to collection information

OpenStax-CNX

You are here: Home » Content » Object-Oriented Programming (OOP) with Java » Java OOP: The AWT and Swing, A Preview

Navigation

Table of Contents

Recently Viewed

This feature requires Javascript to be enabled.
 

Java OOP: The AWT and Swing, A Preview

Module by: R.G. (Dick) Baldwin. E-mail the author

Summary: This module provides a very brief preview of some of what you can expect to find in subsequent modules regarding the Abstract Windows Toolkit (AWT) and the Swing component set.

Preface

This module is part of a collection of modules designed to teach you about Object-Oriented Programming (OOP) using Java.

(Editor's note: As you will see when you read this module, the original version was written about fourteen years ago. However, despite numerous improvements that have occurred in Java since then, most of what was true on this topic in 1998 is still true in 2012. A wise man once said, "If it ain't broke, don't fix it."

One of the changes that you will see in subsequent modules is that the name of the package that contains the Swing classes has been changed to javax.swing .)

Discussion

This module provides a very brief preview of some of what you can expect to find in subsequent modules regarding the Abstract Windows Toolkit ( AWT ) and the Swing component set.

The user interface of a modern computer program often involves techniques to activate many of the human senses. We use icons, text boxes, images, sound, boardroom graphics, etc.

We weren't too concerned with these aspects of programming in the prerequisite course (ITSE 2321) because there was a lot that you needed to learn to prepare yourself for understanding material of this sort. That is about to change.

Much of the actual programming that you will do with Java will involve those aspects of the interface that we commonly refer to as the Graphical User Interface (GUI) .

As of 5/10/98, there are two primary packages that are used for GUI programming under JDK 1.1.6:

  1. java. awt .*
  2. com.sun.java. swing .*

There are, of course, numerous other packages that are used in support of these two packages.

The AWT material was made available to Java programmers early in the life of Java. This was the original material that was used to create graphical user interfaces. Major improvements to the AWT were introduced with the release of JDK 1.1.

The Swing components became available in released form for use with JDK 1.1 around the beginning of 1998. These components added significantly to the ability of the programmer to create GUIs, both in terms of functionality and cosmetics.

The capability and cosmetics of the AWT were very limited but Swing made GUI programming in Java competitive in the real world. A Java programmer no longer need apologize for the quality of the GUIs that she can create.

We expect that these two packages may become more integrated (causing changes in your import directives) with the release of JDK 1.2, (probably sometime in 1998) but hopefully the concepts involved won't be greatly different.

As of 3/5/97, there were more than fifty classes defined in package java.awt . We will discuss some of the more important AWT classes in subsequent modules.

As of 5/10/98, the com.sun.java.swing package contains more than 75 classes and about 20 interfaces. You might expect, therefore, that learning to use this material effectively won't be a trivial task.

It is very important to understand that Swing is an extension of, and not a replacement for the AWT . While it is true that there is some overlap (for example a Swing JButton component might be viewed as an improved functional replacement for an AWT Button component, and once you begin using Swing buttons you may choose to never again use an AWT button) , the basic functionality of Swing is built upon the functionality of the AWT .

Therefore, as students, we cannot simply skip over an understanding of the AWT and move on to Swing . The AWT is the foundation for Swing .

We must first understand the AWT and then understand how Swing extends and improves on the AWT . I will attempt to integrate an understanding of both the AWT and Swing in the remaining modules in this collection.

We will begin by introducing you to a few simple components of each type and use these components to teach you about such topics as event-driven programming, layout, graphics, etc. Then, time permitting, we will dig a little deeper into the more complex aspects of both the AWT and Swing components and other features.

What I won't do is show you a lot of pictures of various AWT and Swing components as is the case with many books and other tutorials (although such pictures can be important for an appreciation of GUI programming) . (Have you noticed how many Java books use copies of the JavaSoft documentation as filler material to make the book appear to contain more information than it actually contains? At least half of many of the books currently in print is nothing more than a reproduction of the documentation that you can download for free from JavaSoft. Oh well, enough of that!)

If you want to see some pictures of AWT and Swing components (which would be only natural) , you can create them yourself on your own computer screen.

For examples of the AWT components, simply look in the folders in the software that you downloaded from JavaSoft. When you install JDK 1.1.6, a folder named "demo" will be created that contains about two-dozen sample programs. Many of these sample programs have graphical user interfaces that make use of the AWT . Just run the programs to see examples of the use of the AWT .

When you download and install Swing 1.0.1, a folder named "examples" will be created. This folder contains about nine folders, each of which contains a demonstration application or applet that makes use of Swing . You can run these programs to see the examples on your computer screen.

A particularly interesting demonstration application is the one named SwingSet . One of the new components in Swing is a tabbed pane that looks much like a common cardboard file folder with a labeled tab on the top, bottom, left, or right. The AWT doesn't contain such a component.

This demonstration starts with about twenty such tabbed panes on the screen, each one of which demonstrates one aspect of the use of Swing . By clicking on each of the labeled tabs, you can select and exercise one aspect of Swing . In addition, there are five menus that contain selections, some of which impact the behavior of some aspect of the demonstration.

While you are there, pay attention to the fact that virtually all of the Swing components are also containers, so it is possible to cause other items (such as images) to be contained in components such as buttons and menus.

Take a look at the pane labeled RadioButtons and see how two different images of JavaSoft's little creature named Duke can be made to function as a radio button. In this case, the selected Duke is waving while the unselected Dukes aren't waving.

Duke shows up again under ToggleButtons where the button which has been toggled has Duke animated in a child's swing.

The Checkboxes pane uses light bulbs that either are or are not illuminated to illustrate selection of Checkbox items.

The examples on the Slider pane are truly impressive (the AWT doesn't have a slider component, although it is possible to use a ScrollBar as a crude slider) .

Take a look at the ListBox pane to see another example of using images inside of a component.

The DebugGraphics pane demonstrates how to run your program in slow motion so that you can see how the components are assembled for debugging purposes. Note that a Slider is used to control the speed of assembly of the components.

And of course, every where you turn in this demo, you will see tool tips that are not a part of the AWT . For a little comic relief, take a look at the ToolTips pane.

Don't forget to pull down the Options menu and select the "look and feel" of the different panes as you view them.

Actually, words are inadequate to describe what you are going to find when you install and run the SwingSet demonstration. To use a corny phrase made famous by an old TV commercial (which many of you are probably too young to remember) , "Try it, you'll like it."

What's next?

The next module in the collection will take a first look at callbacks .

Miscellaneous

This section contains a variety of miscellaneous information.

Note:

Housekeeping material
  • Module name: Java OOP: The AWT and Swing, A Preview
  • File: Java0073.htm
  • Published: 1998
  • Revised: August 20, 2012

Note:

Disclaimers:

Financial : Although the Connexions site makes it possible for you to download a PDF file for this module at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should be aware that some of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. I neither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it was made and published without my prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.

-end-

Collection Navigation

Content actions

Download:

Collection as:

EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

Module as:

PDF | More downloads ...

Add:

Collection to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks

Module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks