Please send questions to
st10@humboldt.edu .
Random notes for CIS 480 - Advanced Java Programming, Week 4, 2-12-01
Topic: Intro to SWING, Part 1
* (first: discussed how to paint in an AWT Frame --- see example
GuiApplic4.java.)
* references for the below material:
[1] Horstman, "Computing Concepts with Java 2 Essentials", Wiley, 2000.
[2] Morelli, "Java, Java, Java", Prentice Hall, 2000.
[3] Sun's Java "The Swing Tutorial",
http://web2.java.sun.com/docs/books/tutorial/uiswing/start/swingIntro.html
* (definitions from Sun's Java glossary,
http://java.sun.com/docs/glossary.html)
* "Swing set" - the code name for a collection of GUI components
that runs uniformly on any native platform which supports the
Java Virtual Machine (JVM).
* written entirely in Java...
* "AWT (Abstract Window Toolkit)" - a collection of GUI components
that were implemented using NATIVE-platform versions of
the components.
* both hoped to meet the goal of
"write once, run anywhere" (portability!)
* but AWT does this with "heavyweight" components,
dependent on native code;
* so: different programs LOOK different on
the different platforms;
* unfortunately, these components do not always
BEHAVE the same on the different platforms;
* "write once, debug everywhere"
* more suitable for applets than for the development
of full-scale GUI applications;
* Swing, instead, renders and paints and etc. the components
itself --- that is, ALL in Java.
* "pure Java"
* the components look the same, should act the same
regardless of the platform;
* "lightweight" components --- that means, here,
that they are written all in Java, without any
"native" code;
* a little history...
* AWT came first; it has been part of Java since
Java version 1.0, although it underwent significant
improvements (and a new event model!!!!) in Java 1.1
* some software developers built their OWN sets
of GUI components... to get around AWT problems...
* Netscape's Internet Foundation Classes (IFC) were
built upon AWT 1.0 classes;
* at the 1997 JavaOne conference, the development of
the Java Foundation Classes (JFC) was announced;
* Netscape and JavaSoft (Sun) working on
* goal was to "encompass a group of
features to help people build GUI's"
* the result, the JFC, includes the Swing component set;
* JFC 1.1 - indeed implemented without any native code;
although it does rely on the API defined in JDK 1.1
* so, they DID become available as an
extension to JDK 1.1
* (thus, the name javax.swing.*...)
* JFC 1.1 included:
* the Swing components (a code name for
the project that developed the new
components, often used now to refer to
the new components and related API)
* Pluggable Look and Feel support
(*.plaf)
* accessibility API - enables assisitive
technologies (Braille displays, etc.)
to get info from the user interface
* JFC 1.1's API is sometimes called
"the Swing API"
* now the Java 2 platform (JDK 1.2) comes along;
* two more inclusions were added to the JFC:
* Java 2D API
(high-quality 2D graphics,
text, images)
* Drag and Drop support
(ability to drag and drop
between a Java app and
a native app)
* so, note: the Swing classes are available as an
EXTENSION for Java 1.1; you get them as part of
Java 2;
* there are 15 public packages considered to
be part of the Swing API;
* javax.swing....
* we'll be looking most, right now,
at javax.swing anf javax.swing.event
* we have Swing version 1.1.1 built into the Java JDK 1.2.2
version of the Java 2 platform
* a little more comparative discussion:
* AWT is somewhat simpler;
* Swing components provide greater functionality,
increased portability;
* often, there's much similarity between the
AWT component and its Swing counterpart;
* if have component Comp in AWT, you
have JComp in Swing;
* Button --> JButton
* Frame --> JFrame
* often, the Swing version has
versions of the AWT methods;
* BUT --- there's often ADDITIONAL
functionality, methods to the Swing version;
* some have mentioned problems with Swing's
speed, size ("resource hog")
* note that some of Swing IS built on top of the AWT;
* Swing's top-level window classes:
* JApplet
* JDialog
* JFrame
* JWindow
...are defined as extensions to their
AWT counterparts;
* also, Swing classes still use AWT's layout
managers, fonts, colors, and other NON-component
classes of the AWT;
* (then, note that all Swing components except for these
4, above, are indeed lightweight)
* simple example #1: TryJOptionPane1.java
* (see link from course web page to Java's
visual index to the Swing components,
http://web2.java.sun.com/docs/books/tutorial/uiswing/components/components.html)
* some important JFrame differences:
* some important structural differences;
* every JFrame is covered with 4 panes:
root pane
* holds glass pane and layered
pane together;
layered pane
* holds the menu bar and the content
pane together
glass pane
* is transparent, and its main purpose
is to capture mouse events
content pane
* holds the components to be displayed
* most Java programmers are concerned with the
content pane;
* holds the components that you want to
display;
* you DO NOT ADD components directly
to a JFrame;
* you need to call getContentPane() to get a
reference to your JFrame's content pane;
* note that getContentPane()
returns a reference to type Container
* then, THIS container is what you add
your components to;
* see TryJFrames1.java, a Swing conversion of GuiApplic2.java;
* and, is similar for applets: see
* ManyButtons2.java - AWT components
...converted to...
* ManyJButtons2.java - Swing components