Java Reference
In-Depth Information
Finally, a loop (lines 173 through 178) goes through the arrays and inserts
each member. The getLength() method notifies the Document class where to
append. The getStyle() method specifies the font style.
Because errors might occur when removing and inserting new text in the
JTextPane, the code is included in a try block. If the remove(), insertString(), or
getLength() methods generate an error, it is caught, and a System error message
is generated by line 182.
The following step enters code for the addTextToTextPane() method.
To Code the addTextToTextPane() Method
1. Enter the code shown in Figure 7-24 on the previous page.
TextPad displays the code for the addTextToTextPane() method
(Figure 7-25).
method
header
method to
clear text
method to
insert column
headings
methods to
insert array
data
try
block
catch
block
textPane
returned to
calling method
FIGURE 7-25
The addTextToTextPane() returns a new, refurbished JTextPane in line 185.
The returned object is used locally in the createContentPane() method (line 113
in Figure 7-19 on page 453).
The actionPerformed() Event
Recall that the actionPerformed() event specifies what will happen when users
click various components in the interface. Two methods typically are used to
determine what object was clicked. The getActionCommand() returns the String
previously assigned to a component, and the getSource() method returns the
clicked object itself.
 
Search WWH ::




Custom Search