AWT vs. Swing
To begin, you need to know something of the differences
between the "heavyweight" components of the AWT (Abstract
Windowing Toolkit) and the "lightweight" components of
Swing (a code name that stuck), and how they fit into the
overall scheme of things. Study the handout showing the
hierarchy of both AWT and Swing GUI interface components.
A component is said to be a heavyweight component
if it uses programs provided by your computer's
operating system to display buttons, choice lists,
text fields, and the like. Such operating-system
programs are said to be the components's peers.
A Swing component is said to be a lightweight
component because it does the high-level display
work itself, rather than relying on a program
provided by your computer's operating system.
You should try to avoid mixing heavyweight and
lightweight components since a heavyweight component
will hide any lightweight components that occupy the
same display area.
What are the fundamental questions for GUI development?
Although it's somewhat of an oversimplification,
the essential questions that need to be asked when you
are constructing a GUI interface are these:
-
Which components will I need?
-
Which of the (perhaps many) constructors that might
be available should I use for each component?
-
What properties of each component should I set after
the component has been constructed, and what methods
are available to do this? This may include things like
fonts and colors, for example?
-
How should my components be positioned in the window
of my application? In other words, how and where do I
add my components to the appropriate container? Should
I use a built-in layout of some kind, or should I
consider the absolute positioning of one or more
components?
-
What do I expect my components to do? That is, how do
I arrange for appropriate events to be sent, and for
those events to be received by the appropriate
recipients who are "listening" for them, and then
"handled" properly?
-
How does my GUI interface relate to, and communicate with,
the rest of my program? This relates to the notion of a
"design pattern" called the MVC (or Model-View-Controller)
design pattern.
Random facts to remember during Java GUI development
-
Heavyweight components paint over lightweight components.
-
Use standard layouts if at all possible. If you must
use absolute positioning and sizing in a particular
component, pass a null parameter to the constructor to
disable whatever the default layout is, and then use the
setLocation and setSize (or setBounds) methods.
-
JFrames have a default BorderLayout, and components added
to a JFrame all get placed in the CENTER portion of the
BorderLayout, causing only the last-entered one to show up.
-
If you want to access a "resource" file like an image file
from a jar file, as part of the display of your GUI, use
getResource with the class that needs the file, in a
statement analogous to this one:
ImageIcon redBall = new ImageIcon(MyView.class.getResource("red-ball.gif"));
-
When accessing files the forward slash (/) can be used
as a "universal" directory separator, another instance
of cross-platform nature of Java. In other words, you
do not need to use the double backslashes (\\) to separate
directories in Java code when writing for a Windows platform
(when providing a path to a file for getResource,
for example).
-
If your top-level window extends JFrame (which will normally
be the case), and you don't want the user to be able to resize
it, be sure to call setResizable(false) on your application window.
List of All Topics