Java Reference
In-Depth Information
Table 7-10
Document Class Methods
METHOD
PURPOSE
EXAMPLE
getText()
Gets the text contained within the given portion
String myLine =
of the document.
myPane.getText(0, 80);
remove()
Removes text in a certain area of the component.
doc.remove(0, 255));
The method takes two integer arguments that
relate to the beginning and end of the area to
remove.
getLength()
Returns the length of the text in the component.
int len = myDocument.getLength();
insertString()
Appends text to an integer location in the text
insertString(doc.getLength(),
component.
title[j] + "\t",
textPane.getStyle("bold"));
Coding the addTextToTextPane() Method
Figure 7-24 displays the code for the addTextToTextPane() method. First,
line 163 creates a Document, named doc, which uses the previously declared
class level JTextPane. Then, the previous text is removed in line 167. The
insertString() method in line 170 appends the headings, TITLE, STUDIO,
and YEAR, using the large style created earlier.
160
//method to add new text to the JTextPane
161
public JTextPane addTextToTextPane ()
162
{
163
Document doc = textPane.getDocument () ;
164
try
165
{
166
//clear previous text
167
doc.remove ( 0, doc.getLength ()) ;
168
169
//insert title
170
doc.insertString ( 0, "TITLE\tSTUDIO\tYEAR\n" ,textPane.getStyle ( "large" )) ;
171
172
//insert detail
173
for ( int j = 0; j<title.length; j++ )
174
{
175
doc.insertString ( doc.getLength () , title [ j ] + "\t" ,
textPane.getStyle ( "bold" )) ;
176
doc.insertString ( doc.getLength () , studio [ j ] + "\t" , textPane.getStyle (
"italic" )) ;
177
doc.insertString ( doc.getLength () , year [ j ] + "\n" ,
textPane.getStyle ( "regular" )) ;
178
}
179
}
180
catch ( BadLocationException ble )
181
{
182
System .err.println ( "Couldn't insert text." ) ;
183
}
184
185
return textPane;
186
}
187
FIGURE 7-24
 
Search WWH ::




Custom Search