Chapter 20. Constructing a Word Processor
Subpages:
1. Word Processor: part I - Introducing RTF
2. Word Processor: part II - Managing fonts
3. Word Processor: part III - Colors and images
4. Word Processor: part IV - Working with styles
5. Word Processor: part V - Clipboard and undo/redo
6. Word Processor: part VI - Advanced font mangement
7. Word Processor: part VII - Paragraph formatting
8. Word Processor: part VIII - Find and replace
9. Word Processor: part IX - Spell checker [using JDBC and SQL]
In this chapter:
- Word Processor: part I - Introducing RTF
- Word Processor: part II - Managing fonts
- Word Processor: part III - Colors and images
- Word Processor: part IV - Working with styles
- Word Processor: part V - Clipboard and undo/redo
- Word Processor: part VI - Advanced font mangement
- Word Processor: part VII - Paragraph formatting
- Word Processor: part VIII - Find and replace
- Word Processor: part IX - Spell checker [using JDBC and SQL]
This chapter is devoted to the construction of a fully-functional RTF word processor application. Though Swing's HTML and RTF capabilities are very powerful, they are not yet complete. RTF support is further along than HTML, and this is why we chose to design our word processor for use with RTF documents. The examples in this chapter demonstrate practical applications of many of the topics covered in chapter 19. The main focus throughout is working with styled text documents, and the techniques discussed here can be applied to any styled text editor.
Note: When running the examples in this chapter, do not be surprised when you see a series of 'unknown keyword' warnings or exception problems with various Views. You will also see the following message displayed to emphasize the fact that RTF support is still in the works: "Problems encountered: Note that RTF support is still under development."
20.1 Word Processor: part I - Introducing RTF
This basic example uses the capabilities of JTextPane and RTFEditorKit to display and edit RTF documents. It demonstrates very basic word processor functionality, opening and saving an RTF file, and serves as the foundation for our word processor application to be expanded upon throughout this chapter.
Note: In this series of examples our goal is to demonstrate the most significant available features of advanced text editing in Swing (even if they do not all currently work properly). To avoid losing focus of this goal we intentionally omit several typical word processor features such as an MDI interface, status bar, and prompts to save the current file before closing.

Figure 20.1 JTextPane displaying an RTF document.
<<file figure20-1.gif>>
The Code: WordProcessor.java
see \Chapter20\1
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.text.rtf.*;
public class WordProcessor extends JFrame
{
protected JTextPane m_monitor;
protected StyleContext m_context;
protected DefaultStyledDocument m_doc;
protected RTFEditorKit m_kit;
protected JFileChooser m_chooser;
protected SimpleFilter m_rtfFilter;
protected JToolBar m_toolBar;
public WordProcessor() {
super("RTF Word Processor");
setSize(600, 400);
// Make sure we install the editor kit before creating
// the initial document.
m_monitor = new JTextPane();
m_kit = new RTFEditorKit();
m_monitor.setEditorKit(m_kit);
m_context = new StyleContext();
m_doc = new DefaultStyledDocument(m_context);
m_monitor.setDocument(m_doc);
JScrollPane ps = new JScrollPane(m_monitor);
getContentPane().add(ps, BorderLayout.CENTER);
JMenuBar menuBar = createMenuBar();
setJMenuBar(menuBar);
m_chooser = new JFileChooser();
m_chooser.setCurrentDirectory(new File("."));
m_rtfFilter = new SimpleFilter("rtf", "RTF Documents");
m_chooser.setFileFilter(m_rtfFilter);
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(wndCloser);
setVisible(true);
}
protected JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu mFile = new JMenu("File");
mFile.setMnemonic('f');
ImageIcon iconNew = new ImageIcon("file_new.gif");
Action actionNew = new AbstractAction("New", iconNew) {
public void actionPerformed(ActionEvent e) {
m_doc = new DefaultStyledDocument(m_context);
m_monitor.setDocument(m_doc);
}
};
JMenuItem item = mFile.add(actionNew);
item.setMnemonic('n');
ImageIcon iconOpen = new ImageIcon("file_open.gif");
Action actionOpen = new AbstractAction("Open...", iconOpen) {
public void actionPerformed(ActionEvent e) {
WordProcessor.this.setCursor(
Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
Thread runner = new Thread() {
public void run() {
if (m_chooser.showOpenDialog(WordProcessor.this) !=
JFileChooser.APPROVE_OPTION)
return;
WordProcessor.this.repaint();
File fChoosen = m_chooser.getSelectedFile();
// Recall that text component read/write operations are
// thread safe. Its ok to do this in a separate thread.
try {
InputStream in = new FileInputStream(fChoosen);
m_doc = new DefaultStyledDocument(m_context);
m_kit.read(in, m_doc, 0);
m_monitor.setDocument(m_doc);
in.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
WordProcessor.this.setCursor(Cursor.getPredefinedCursor(
Cursor.DEFAULT_CURSOR));
}
};
runner.start();
}
};
item = mFile.add(actionOpen);
item.setMnemonic('o');
ImageIcon iconSave = new ImageIcon("file_save.gif");
Action actionSave = new AbstractAction("Save...", iconSave) {
public void actionPerformed(ActionEvent e) {
WordProcessor.this.setCursor(
Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
Thread runner = new Thread() {
public void run() {
if (m_chooser.showSaveDialog(WordProcessor.this) !=
JFileChooser.APPROVE_OPTION)
return;
WordProcessor.this.repaint();
File fChoosen = m_chooser.getSelectedFile();
// Recall that text component read/write operations are
// thread safe. Its ok to do this in a separate thread.
try {
OutputStream out = new FileOutputStream(fChoosen);
m_kit.write(out, m_doc, 0, m_doc.getLength());
out.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
// Make sure chooser is updated to reflect new file
m_chooser.rescanCurrentDirectory();
WordProcessor.this.setCursor(Cursor.getPredefinedCursor(
Cursor.DEFAULT_CURSOR));
}
};
runner.start();
}
};
item = mFile.add(actionSave);
item.setMnemonic('s');
mFile.addSeparator();
Action actionExit = new AbstractAction("Exit") {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
};
item = mFile.add(actionExit);
item.setMnemonic('x');
menuBar.add(mFile);
m_toolBar = new JToolBar();
JButton bNew = new SmallButton(actionNew, "New document");
m_toolBar.add(bNew);
JButton bOpen = new SmallButton(actionOpen, "Open RTF document");
m_toolBar.add(bOpen);
JButton bSave = new SmallButton(actionSave, "Save RTF document");
m_toolBar.add(bSave);
getContentPane().add(m_toolBar, BorderLayout.NORTH);
return menuBar;
}
public static void main(String argv[]) {
new WordProcessor();
}
}
// Class SmallButton unchanged from section 12.4
// Class SimpleFilter unchanged from section 14.1.9
Understanding the Code
Class WordProcessor
This class extends JFrame to provide the supporting frame for this example. Several instance variables are declared:
JTextPane m_monitor: text component.
StyleContext m_context: a group of styles and their associated resources for the documents in this example.
DefaultStyledDocument m_doc: current document model.
RTFEditorKit m_kit: editor kit that knows how to read/write RTF documents.
JFileChooser m_chooser: file chooser used to load and save RTF files.
SimpleFilter m_rtfFilter: file filter for ".rtf" files.
JToolBar m_toolBar: toolbar containing open, save, and new document buttons.
The WordProcessor constructor first instantiates our JTextPane and RTFEditorKit, and assigns the editor kit to the text pane (it is important that this is done before any documents are created). Next our StyleContext is instantiated and we build our DefaultStyledDocument with it. The DefaultStyledDocument is then set as our text pane's current document.
The createMenuBar() method creates a menu bar with a single menu titled "File." Menu items "New," "Open," "Save," and "Exit" are added to the menu. The first three items are duplicated in the toolbar. This code is very similar to the code used in the examples of chapter 12. The important difference is that we use InputStreams and OutputStreams rather than Readers and Writers. The reason for this is that RTF uses 1-byte encoding which is incompatible with the 2-byte encoding used by readers and writers.
Warning: An attempt to invoke read() will throw an exception when JTextPane is using an RTFEditorKit.
Running the Code
Use menu or toolbar buttons to open an RTF file (a sample RTF file is provided in the \swing\Chapter20 directory). Save the RTF file and open it in another RTF-aware application (such as Microsoft Word) to verify compatibility.


RSS feed Java FAQ News