CIS163AA Lesson 1

guitarchanceSoftware and s/w Development

Aug 15, 2012 (5 years and 4 days ago)

247 views


CIS163AA
-

Java

I

Glendale Community College

Instructor: Gary Marrer

Lesson
Eleven

_________________________________________________________


This Week's Objectives:



Topics:



Letting it sink in…



AWT versus Swing



Optional


Graphical Controls 101



Layout Managers (more AWT)



Misc.



_________________________________________________________



Lecture


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.

It is
time to let it sink in and work some more examples to make sure you understand it.

So everyone should
completely
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
BEST

thing you
can do for yourself from this point forward is to open a lot of Java examples and run the
debugger

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



W
ork on homework
assignment
.



Re
-
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
desirable commodity
when Java was initially developed so Java has always had the
ability to
create window'ed applications. Some of us are old enough to remember what
the first
Windows 3.0
GUI programming interface looked like but most of you are
probably more familiar with later versions of

Microsoft

Windows which had much more
robust graphical e
nvironments.


The first graphical objects

(controls like textboxes, command buttons, checkboxes)

available to
Java

programs came from the
abstract windows toolkit

also known as
AWT
.


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
.


Put
another w
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
button.


The AWT contains all the programming necessary to make that button
function.

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
Visual Basic

or C#, the
Microsoft

toolbox provides the same type of support

as AWT
.


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).


AWT provides
what is called a
"heavy weight" controls

along with event handling and layout
managers.


These controls are called heavyweight because they rely on the platform
operating system to display in control graphical controls.


In the case of
Microsoft

Windows,

AWT

would call on routines built into
Micr
osoft

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
manner.


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,
S
un introduced another alternative to AWT called
S
wing

controls.


It is generally accepte
d that
S
wing controls are much more robust
both in controls
options and the number of different controls
.


JOptionPane
,

which

we have used
extensively in class
,

is a member of the
S
wing namespace.


JOptionPane is a dialog box
control
that

consists of a col
lection of controls providing input and output dial
o
g

boxe
s
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.


In addition,
S
wing c
ontrols are considered

lightweight


controls

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.


For starters,
event handling along with some helper classes still
come from AWT namespace
s

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
S
wing since calling
the operating system calls is qui
cker than interpreting by code in the
J
ava
R
untime
E
ngine.


Lastly, some devices (especially those with embedded operating systems like
cell phones) do not work with
S
wing and sometimes require AWT as the only option for
graphical interfaces.


As you will
also see, if you continue to program
J
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
J
ava runtime environment
for my
D
ell
Axim
PDA

(WabaSoft)

which requires support of the namespace specifically
designed to work with
hardware

of that PDA.


What
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

(s
ee discussion of
Google GWT

below for more on
User Interface

(
UI
) namespaces)
.


Tip:

There is one other graphical control namespace that is also poplar among Java
programmers. SWT (Standard Widget Toolkit)
http://www.eclipse.org/swt/

is popular
with IBM products and the
Eclipse IDE
.


Mixing and matching the two.


Generally, programmers are discouraged from mixing the AWT and
S
wing controls in the
same screen.


This is not to say that you should always use

S
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
S
wing controls.


If your program is
going to need to run on devices that support older
J
ava runt
ime engines, then perhaps
you would want use AWT.


If your application needs to run quickly
,

th
e
n AWT might be
the better answer.


If you are interested in the efficient use of resources
S
wing has an
edge.


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.




Tip:

The controls of AWT and Swing share the same names except Swing controls will
start with an upper case J (i.e.
Applet

in
AWT

becomes
JApplet

in
Swing
).

(
Optional
)
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
erating
system.





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
program.



Text
Field

control

-

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.



Label control

-

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
mutually
exclusive. For example, you would use option buttons to represent
gender (male / female) because only one of those options could be true at one
time.



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
new w
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
g programming
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.






Layout Managers


A

topic which I
have

ignore
d up to this point
(
but has been introduced in
C
hapter
F
ive
)

is
layout managers
.


Layout managers are used to position
graphical
controls inside a
window
’ed

application,
applet

or panel
.


They are used with both AWT and
S
wing
controls.


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
DE
, 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.


Both the
null layout

manager

in
absolute layout manager

will allow you to
drag

controls into position
relative to the window and remove this scenario where a user maximizes a window
and
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,
I
have
had the most success with a
grid layout

manager

because most screens are laid
out like
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.


However, I

would
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
designs.




_________________________________________________________



Book Notes:



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.



_________________________________________________________


MY Turn:


Other things you can do with Java:

GWT

(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
programs into
JavaScript
… yes
JavaScript
. These
JavaScript

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.


http://code.google.com/webtoolkit/overview.html


The
GWT
tool
kit

include
s

programs to convert Java to JavaScript and also tools to te
s
t
your applications.





_________________________________________________________


Chapter Review





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
?

Questions?