Java Reference
In-Depth Information
The
JEditorPane
and
JTextPane
components are a different order of complexity from the others and
enable you to implement sophisticated editing facilities relatively easily. The
JEditorPane
supports editing
of plaintext, text in HTML, and RTF (Rich Text Format). The
JTextPane
class extends
JEditorPane
and
enables you to embed images or other components within the text managed by the component.
Other Swing Components
Other Swing components you use regularly include the
JPanel
component. The
JPanel
class defines
something like a physical panel that you can use as a container to group a set of components. For example,
you might use two
JPanel
objects to support two separate groups of
JButton
components in the content
pane of an application window.
The
JList<E>
and
JTable
components are also very useful. A
JList<E>
component displays a list
of items of type
E
with one item per line from which a user can select an item. The list of items
displayed by a
JList<E>
component is maintained in an object of type
javax.swing.ListModel<E>
,
where
ListModel<>
is a generic interface type. You can create a
JList<E>
component from a
javax.swing.DefaultListModel<E>
object in which you store the items you want to have displayed in
the list. A
DefaultListModel<>
object is much like a
vector<>
container. You add items to the list model
by calling the
addElement()
member and you can add an item at a particular index position by calling the
add()
method where the first argument is the index position and the second is the item to be added.
A
JTable
component displays items in a rectangular table with column headers with the items stored
in a table model. You can create your own table model class by using the
javax.swing.table.AbstractTableModel
class as a base. A user can edit a single cell, or relocate or de-
lete an entire row or column. A
JTable
component automatically takes care of reordering the columns when
a column is dragged to a new position using the mouse.
In principle, you can add a border to any component, and the
javax.swing.borders
package contains
eight classes that represent different kinds of borders you can use for a component. However, you can also
place a component to which you want to add a border in a
JPanel
container, and add the border to the
JPanel
object. You use
JPanel
containers quite a lot throughout the rest of the topic.
I have not introduced all the Swing component classes by any means, and you meet a few more as you
progress through the rest of the chapters.
USING SWING CONTAINERS
A Swing container is any component of a type that has the
Container
class as a base, so all the Swing com-
ponents are containers. The
Container
class is the direct base class for the
Window
class, and it provides the
capability for a window to contain other components. Because the
Container
class is an
abstract
class,
you cannot create instances of
Container
. Instead, it's objects of the subclasses such as
Window
,
JFrame
, or
JDialog
that inherit the capability to contain other components.