Java Reference
In-Depth Information
J ava 2
v 5 . 0
J ava 2
JAVA UPDATE
v 5 . 0
87 public void createContentPane ()
In J2SE version 5.0, pro-
grammers may simplify
their programs by
omitting the
getContentPane()
method reference when
adding components.
For example,
myJFrame.add() is
equivalent to
myJFrame.getContent-
Pane.add(). Similarly, the
setContentPane()
method may be omitted.
112 //Container c = getContentPane();
113
setLayout( new BorderLayout ( 10,10 )) ;
114
add ( northPanel, BorderLayout .NORTH ) ;
115
add ( centerPanel, BorderLayout .CENTER ) ;
116
117 //return c;
321 f.createContentPane () ;
Tabs and Styles
Tabs and styles apply to various text components, such as JTextAreas and
JTextPanes. The TabStop class allows programmers to create predetermined tab
positions at specified distances from the left margin—aligned in a specified way
and with an optional specified leader. TabStops usually are contained in TabSets.
The TabSet class is composed of multiple TabStops and offers methods for locat-
ing the closest TabStop to a given position and finding all the potential
TabStops. Table 7-9 displays some of the available TabStop attributes.
If you want to amend
the DVD program to use
the add() methods
without the direct refer-
ence to the get() and
set() methods associated
with the content pane,
you must replace lines
87, 113, 114, 115, and
321 (created later in the
chapter) and comment
out lines 112 and 117 as
shown in the following
code. The createContent-
Pane() method no longer
will return a Container
data type but simply will
be called from the main()
method.
Table 7-9
TabStop Attributes
ATTRIBUTE
FUNCTION
LEAD_EQUALS
Assigns a leading character to the tab stop
LEAD_NONE
Assigns no leading character to the tab stop
LEAD_DOTS
Assigns periods (or dots) as the leading character to the tab
stop
ALIGN_RIGHT
Sets a right-aligned tab stop
ALIGN_CENTER
Sets a centered tab stop
ALIGN_JUSTIFED
Sets a justified tab stop
ALIGN_LEFT
Sets a left-aligned tab stop
Formatting styles associated with JTextPanes can be more complicated.
First-time programmers typically create a style context for elements in their
JTextPanes. The StyleContext class contains a pool of styles—such as font and
color—and their associated resources, which are reused by various style defini-
tions. If desired, a StyleContext can be shared by multiple components to maxi-
mize the sharing of related resources, or it can be assigned to just one
component.
Methods from the AttributeSet class use a defined StyleContext to set
attributes for the current object. Attributes are used to describe features that will
contribute to a graphical representation, such as tabs or paragraph formatting.
A Style is a related concept, but larger and more inclusive. The Style class
allows programmers to store a collection of attributes associated with an element
in a document. Styles must be declared and assigned and typically are used to
associate character and paragraph styles with the element. The following code
obtains the default style:
 
Search WWH ::




Custom Search