Glendale Community College
Instructor: Gary Marrer
This Week's Objectives:
Letting it sink in…
AWT versus Swing
Graphical Controls 101
Layout Managers (more AWT)
Letting it Sink In…
We have reached a point in class where most of the hard work is behind us. I told all of
you the first week that my goal was to
turn all of you into Java maintenance
programmers. To do this
we need to understand how to implement structured and
object orientated programming in Java. We have covered all of the concepts necessary
to do this. What is left in the textbook and my les
sons are continued analysis of how
Java OOP works with different features
(namespaces and classes)
. We will use more
controls, work with new events and learn to save data in files. All of these techniques
will build on what we have already learned and sim
ply build on our experience.
time to let it sink in and work some more examples to make sure you understand it.
So everyone should
understand Java? No yet. Hopefully the light bulb is a
little brighter but what I will be doing in my les
sons (written and video) is to review
sample programs to help you make the light bulb a little brighter. The
can do for yourself from this point forward is to open a lot of Java examples and run the
on all of them. The debugger wi
ll help guide you through the interaction
between classes. This interaction is the hard part and what all new Java programmers
must struggle with. My hope is that you will find, as I did, that Java is VERY straight
forward and just as easy to understand
and use and any other programming language.
Use this week to catch up.
Read the textbook.
Listen to my
Video Lesson on
the Calculator program
ork on homework
read the textbook for the parts that you still don’t understand.
Read through the last couple of weeks of lessons.
Ask me questions.
Get caught up.
AWT versus Swing Packages
Even though Java is a relatively new programming language, its beginnings coincided
with Microsoft Windows 3.0.
The graphical user interface (GUI) was
a known and
when Java was initially developed so Java has always had the
create window'ed applications. Some of us are old enough to remember what
GUI programming interface looked like but most of you are
probably more familiar with later versions of
Windows which had much more
robust graphical e
The first graphical objects
(controls like textboxes, command buttons, checkboxes)
programs came from the
abstract windows toolkit
also known as
The name abstract windows tool kit comes from the fact that these controls and
services are highly abstract so that the programmer only needs to know how to set
properties that control the display
and what events are thrown by the object
ay, when you use command button from AWT, you don't need to worry about
drawing the button you only need to worry about the text that's displayed on the
The AWT contains all the programming necessary to make that button
All you have to d
o is to create event handlers for all of the events you wish to
process (i.e. click events).
For those of you who are familiar with
or C#, the
toolbox provides the same type of support
AWT is a namespace that includes text
box, command button, radio button, etc.
controls found in most graphical applications.
The AWT namespace was the only option
for creating graphical applications until Java 2 (also known as Java 1.2).
what is called a
"heavy weight" controls
along with event handling and layout
These controls are called heavyweight because they rely on the platform
operating system to display in control graphical controls.
In the case of
would call on routines built into
Windows to display text
boxes, command buttons in other graphical controls.
The problem with this approach is
that not all platforms draw controls or even provide support two controls in the same
Since java was designed to be a write once r
un everywhere solution, the
problem developed and when graphical programs developed and windows looked
different when run on an apple platform.
Starting with Java 2,
un introduced another alternative to AWT called
It is generally accepte
wing controls are much more robust
both in controls
options and the number of different controls
we have used
extensively in class
is a member of the
JOptionPane is a dialog box
consists of a col
lection of controls providing input and output dial
to the programmer.
There's nothing within the AWT toolkit that contains this level of
flexibility or complexity.
The AWT toolkit contains a much smaller number of controls.
ontrols are considered
because they rely on
the java runtime environment to paint the graphics thus removing the dependency on
the native operating system.
Because now all controls are drawn the same way, java
GUI applications look
the same from one operating platform to the next.
This is one of
the fundamental benefits of using Java.
Which one is better?
Swing is the preferred solution for most programmers but this is not to say that AWT
should be ignored.
event handling along with some helper classes still
come from AWT namespace
so even if you use Swing you will almost always still use
AWT for the events
Secondly, AWT has been said to be quicker then
wing since calling
the operating system calls is qui
cker than interpreting by code in the
Lastly, some devices (especially those with embedded operating systems like
cell phones) do not work with
wing and sometimes require AWT as the only option for
As you will
also see, if you continue to program
ava on less
traditional devices (my PDA for example), that there are still more namespace is they
can provide graphical support for various devices.
I've used a
ava runtime environment
which requires support of the namespace specifically
designed to work with
of that PDA.
I'm saying is, it's not only a Swing
vs. AWT decision but also you may have other namespaces you'll need to learn to
perform graphical event handling
ee discussion of
below for more on
There is one other graphical control namespace that is also poplar among Java
programmers. SWT (Standard Widget Toolkit)
with IBM products and the
Mixing and matching the two.
Generally, programmers are discouraged from mixing the AWT and
wing controls in the
This is not to say that you should always use
wing controls because they
are the newest.
However, if you're afraid your program will not run correctly on
different computer platforms then you should use
If your program is
going to need to run on devices that support older
ime engines, then perhaps
you would want use AWT.
If your application needs to run quickly
n AWT might be
the better answer.
If you are interested in the efficient use of resources
wing has an
Mixing them can create problems and if you choose
this approach you should
also plan on doing more extensive testing to ensure that the controls do not conflict.
There are web pages that will discuss this topic and greater detail for those of you who
would like to do more research in this area.
The controls of AWT and Swing share the same names except Swing controls will
start with an upper case J (i.e.
Common Graphical Controls 101
This list below is designed to represent graphic controls com
monly found in most
languages that support graphical interface programming. The list is not extensive and
also does not take in consideration third party controls which are developed by
independent companies not associated with a programming language or op
Figure 1: Graphical Controls on Main Window
Command button control
The command button control is designed to trigger
events that execute functions within the program. Most programs display an OK
or CANCEL the command button to contin
ue or terminate a function of the
The text box control is designed to allow the end user to
enter or edit character text. The text box control can display single or multiple
lines of information. Most of the time, the textboxes used for program input.
Java also supports a TextArea contr
ol for when you need to support in the input
of multiple lines.
The label control positions text on the window. This text is not
editable and is used to label various controls on the main window or to display
logic output from the program.
Option button control
The option button is also known as a radio button and is
presented on the interface as a small circle that is either filled (true) or unfilled
(false). Option buttons are useful for designating program parameters which are
exclusive. For example, you would use option buttons to represent
gender (male / female) because only one of those options could be true at one
Check box control
The check box control is similar to the option button control
except that it is not mu
tually exclusive. This means that a group of check boxes
are all independent of each other and therefore all could be checked (true) or
unchecked (false) or any combination of true and false.
List box control
A list box control is a rectangle that optionally contains
horizontal and vertical scroll bars depending on the number and size of the items
stored within the list. A list box is useful when only certain values are valid and
typing them into a text box
would require additional validation.
Combo box control
A combo box control is a specialization of the list box. It
also displays a list of items but has the distinction of being collapsible. Combo
boxes normally take up one line of text but can be option
ally expanded by
clicking the down pointing arrow to the right of the combo box text. Combo
boxes are popular because they take up less space on the window then a list box.
Dialog box control
The dialog box control allows the programmer to invoke a
indow on top of the main window which can either collect input or display
output to the user. Dialog boxes could be created by the programmer as new
windows but are included in most programming languages as a productivity aid
since dialog boxes typically a
re numerous and most graphical applications.
Drop down menus control
Drop down menus fall from the taskbar and allow
the programmer to provide a menu to interface into program functions. Each
menu item maps to an event that’s has associated event handlin
statements. The menu item works much like a command button.
Panel (not shown)
Panels are windows with in windows and help to organize
sections of the window with different layout managers.
topic which I
d up to this point
but has been introduced in
Layout managers are used to position
controls inside a
They are used with both AWT and
The book covers the five of the more popular layout managers but does not
include them all.
For those of you who have used the NetBeans I
, you will of noticed
that are not constrained by having to position controls in a region of your wi
ndow or let
the size of the window control positioning of your controls.
absolute layout manager
will allow you to
controls into position
relative to the window and remove this scenario where a user maximizes a window
destroys your hard work positioning text boxes and labels so that the display is both
intuitive and functional.
For my money,
when not using the null layout manager,
had the most success with a
because most screens are laid
a book to be read top to bottom, left to right with the controls typically paired (label
and then related textbox).
A grid layout manager will let you do this.
suggest that the best way to understand layout managers is to work wi
th each and pick
one or a couple of layout managers which seemed to work best with your screen
Where we are at today…
At this point in your Java training, we have
covered all of the essentials. From here on in
we will be practicing and perfecting what we have learned in the textbook and in my
lessons. Adding menus, building an array of button controls are all topics covered
earlier with a different purpose and prob
ably more sophisticated then what was
covered on our first pass. The book has some new features to show you and more
examples to review.
Other things you can do with Java:
(Google Web Toolkit)
One of the more interesting tools to come out of Google is the GWT namespace and
related tools. Google has been one of the leaders in bringing more interactivity into
web applications. By using technologies like AJAX and reducing re
turn trips to the web
server, Google has been able to create word processing, spreadsheet and presentation
tools (see Google Docs) not to mention highly performing map software. With GWT,
you can take your Java programming skills and have Google tools co
nvert those Java
applications make use of
AJAX and create another use for your Java expertise. I have included the link for those
of you who would like to learn more.
What are the different layout managers and their differences
When do we use AWT versus Swing controls
Why do we say you need AWT with Swing