|
Help is available for each task, or you can go straight to
the solution
source code.
Task 1
Import the Swing text package. The text-handling support classes are all
located here.
import javax.swing.text.*;
Task 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.
JTextPane pane;
StyledDocument doc;
Task 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.
doc = new DefaultStyledDocument();
pane = new JTextPane (doc);
JScrollPane sp = new JScrollPane (pane);
c.add(sp, BorderLayout.CENTER);
Task 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
To clear out the current document, create a new document:
pane.setStyledDocument (
doc = new DefaultStyledDocument());
To insert into the document, use the new StyledDocument just
created:
doc.insertString(doc.getLength(),
new String (buffer, 0, len), null);
Task 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.
At the appropriate location in the source, add the following lines:
item.setIcon (new ColoredBox(Color.red));
...
item.setIcon (new ColoredBox(Color.orange));
...
item.setIcon (new ColoredBox(Color.yellow));
...
item.setIcon (new ColoredBox(Color.green));
...
item.setIcon (new ColoredBox(Color.blue));
...
item.setIcon (new ColoredBox(Color.magenta));
Task 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.
At the appropriate location in the source, add the following lines:
item.addActionListener (new
StyledEditorKit.ForegroundAction (
"set-foreground-red", Color.red));
...
item.addActionListener (new
StyledEditorKit.ForegroundAction (
"set-foreground-orange", Color.orange));
...
item.addActionListener (new
StyledEditorKit.ForegroundAction (
"set-foreground-yellow", Color.yellow));
...
item.addActionListener (new
StyledEditorKit.ForegroundAction (
"set-foreground-green", Color.green));
...
item.addActionListener (new
StyledEditorKit.ForegroundAction (
"set-foreground-blue", Color.blue));
...
item.addActionListener (new
StyledEditorKit.ForegroundAction (
"set-foreground-magenta", Color.magenta));
Task 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.
Pick a default color to use as the last parameter to showDialog.
Color color = JColorChooser.showDialog(
this, "Color Chooser", Color.cyan);
If the returned value is not null, change the foreground text color:
if (color != null) {
MutableAttributeSet attr =
new SimpleAttributeSet();
StyleConstants.setForeground(attr, color);
pane.setCharacterAttributes(attr, false);
}
Task 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.
Be sure to check how the font size and family change, too.
For font bold and italics, add the following lines at the appropriate location
in the source:
item.addActionListener (
new StyledEditorKit.BoldAction ());
...
item.addActionListener (
new StyledEditorKit.ItalicAction ());
Task 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.
Use getSelectedFile() to find the selected File.
File file = chooser.getSelectedFile();
Get the file as an ImageIcon.
Icon icon = new ImageIcon (
file.getAbsolutePath());
Use insertIcon() to add the icon to the pane.
pane.insertIcon (icon);
Task 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.
javac Formatter.java
java Formatter
Copyright © 1998-1999
MageLang Institute.
All Rights Reserved.
|