Technical Support
Discussion Forum
Online Training
Technical Articles
Bug Parade
JDC Resources
DukeDollars
Early Access
Toolbar
Community Process
Technology Sites

JDC Home Page

Solaris Developer Connection

Java Technology
java.sun.com

Java Industry Connection

Log Out

Online Training
SearchFAQContact Us

Magercise 3
Using JTextPane

[Help | API Docs | Short Course | Magercises]

Magercise 3 Prerequisites

Using Menus, Toolbars, and Tool Tips - from Part I

Skeleton Code

Formatter.java

In this magercise, you will explore the JTextPane class and its support classes in the javax.swing.text package. You'll create an editor that allows you to change the font, color, size, and style of the displayed text. You'll also use the Swing Chooser classes to pick colors and files, as well as display images within the JTextPane.

Working from the skeleton code, you'll setup the StyledDocument to read in a text file and activate various menu options.

Tasks:

  1. Import the Swing text package. The text-handling support classes are all located here.

  2. Create an instance variable pane of type JTextPane, and an instance variable doc of type StyledDocument. The document represents the model of the text, while the pane represents the view of it to the user.

  3. Create a JTextPane, place in JScrollPane, and place in center of the screen. You will need to create a DefaultStyledDocument, and then pass that along to the JTextPane constructor.

    Remember to save the references to the JTextPane and StyledDocument in the instance variables, as they will be needed later.

  4. The "Load Text" menu item under the File menu calls the doLoadCommand() method to load a text file. The method has two pieces missing:
    • Clear out the current document
    • For each line read in from the file, add the line to the document with the default style

  5. For the Color menu, add a ColoredBox icon for each menu item.

    The ColoredBox class is provided as a support class. Its constructor requires a Color.

  6. For each of the color menu items, add an ActionListener to change the foreground color of the text.

    The StyledEditorKit class has a static inner class to help here. Its called ForegroundAction and takes two parameters. The first is a text string describing the change, while the second is the new foreground color.

  7. For the Custom Color menu item, you need to complete the doColorCommand() method to allow the user to enter any color, besides just the canned colors. In doColorCommand(), show a JColorChooser, and change the foreground color to the color selected.

  8. Most of the Font change support menu is already done for you. The only two menu items that need action listeners are the Bold and Italic entries. Add an ActionListener to each of these items to change the appropriate font characteristic.

    Like the ForegroundAction inner class, there are two more inner classes for these events. They are called BoldAction and ItalicAction.

  9. For the Image File menu item, you need to complete the doInsertImageCommand() method so that a user can display an image within the JTextPane. The main JFileChooser usage source has already been created for you, you only need to finish the approve response, which is called when the user selects the Okay or Open button. Here you need to find the image file selected, create an ImageIcon for it, and insert it into the JTextPane.

  10. Save everything and compile the program. Then run it to see the results. Load a file and change its display style. You can save the document and reload the document as a serializable object. Also, try to add an image to the document.

[Result]

Where help exists, the task numbers above are linked to the step-by-step help page.

Solution

The following Java™ source file represents a solution to this Magercise:
Formatter.java

Magercise 4

Short Course

About This Short Course

Copyright © 1998-1999 MageLang Institute. All Rights Reserved.





Questions?
29-Mar-99
Copyright © 1996-1999 Sun Microsystems Inc.
All Rights Reserved. Legal Terms. Privacy Policy.
Sun Microsystems