Java Reference
In-Depth Information
• Containers to hold GUI objects, such as
JFrame
for our outermost win-
dow,
JPanel
for an assortment of objects, and
JScrollPane
for viewing
larger objects through a scrollable window.
• Labels (
JLabel
class) to hold either a short bit of text, or an image, or
both; it can even take snippets of HTML, for fancier formatting and
coloring of text.
• Buttons (
JButton
class) to which we attached actions—the code frag-
ments that get called when the buttons get pushed; a button could have
text and/or an image displayed in it.
• Actions—whether for buttons or selections (or other triggers yet to be
discussed), an action is the code that runs when the event (e.g., button
press) occurs.
• Text fields (
JTextField
class) to take small amounts of user input; our
application didn't need the other types of text fields (
JTextArea
and
JTextPane
) useful for much more extensive user input.
•A
JTable
instance and its associated
TableModel
,
SelectionModel
, and
TableCellRenderer
which provide tremendous flexibility and control
over table behavior and contents.
•A
JDialog
instance with custom content, to allow for multiple user in-
puts; the dialog comes and goes with its visibility; since it's a modal dialog,
when it is visible, it “hogs” all the user interactions; it is possible to make
nonmodal dialogs, but our application didn't need to.
•
LayoutManager
s for our
JFrame
and
JPanel
s, used to place objects
within a container with various algorithms for placement and expansion.
16.9
W
HAT
Y
OU
S
TILL
D
ON
'
T
K
NOW
One could spend a career learning the vagaries of layout managers, especially
the way they interact (e.g., a
BoxLayout
inside the various regions of a
BorderLayout
). There is still an art to getting all the interactions right; it's
often quickest to prototype the layout before you get too committed to a par-
ticular layout. Also, putting objects into containers can help you subdivide the
layout problem into more manageable pieces. You can even go so far as to write
your own
LayoutManager
, a topic we do not cover in this topic.
The information that we display in the
JTable
in our example is hierar-
chical. Swing provides a
JTree
object for displaying such information. Like a