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]
20.2 Word Processor: part II - Managing fonts
The following example adds the ability to select any font available on the system. This functionality is similar to the "Font" menu used in the examples of chapter 12. The important difference here is that the selected font applies not to the whole text component (the only possible thing with plain text documents), but to the selected region of our RTF styled document text.

Figure 20.2 JTextPane word processor allowing font attribute assignments to selected text.
<<file figure20-1.gif>>
The Code: WordProcessor.java
see \Chapter20\2
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;
protected JComboBox m_cbFonts;
protected JComboBox m_cbSizes;
protected SmallToggleButton m_bBold;
protected SmallToggleButton m_bItalic;
protected String m_fontName = "";
protected int m_fontSize = 0;
protected boolean m_skipUpdate;
protected int m_xStart = -1;
protected int m_xFinish = -1;
public WordProcessor() {
// Unchanged code from section 20.1
CaretListener lst = new CaretListener() {
public void caretUpdate(CaretEvent e) {
showAttributes(e.getDot());
}
};
m_monitor.addCaretListener(lst);
FocusListener flst = new FocusListener() {
public void focusGained(FocusEvent e) {
if (m_xStart>=0 && m_xFinish>=0)
if (m_monitor.getCaretPosition()==m_xStart) {
m_monitor.setCaretPosition(m_xFinish);
m_monitor.moveCaretPosition(m_xStart);
}
else
m_monitor.select(m_xStart, m_xFinish);
}
public void focusLost(FocusEvent e) {
m_xStart = m_monitor.getSelectionStart();
m_xFinish = m_monitor.getSelectionEnd();
}
};
m_monitor.addFocusListener(flst);
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(wndCloser);
showAttributes(0);
setVisible(true);
}
protected JMenuBar createMenuBar() {
// Unchaged code from section 20.1
// The following line is added to the end of the
// actionNew and actionOpen actionPerformed() methods:
//
// showAttributes(0);
//
// (see source code; these methods are not shown here
// to conserve space)
// Unchaged code from section 20.1
GraphicsEnvironment ge = GraphicsEnvironment.
getLocalGraphicsEnvironment();
String[] fontNames = ge.getAvailableFontFamilyNames();
m_toolBar.addSeparator();
m_cbFonts = new JComboBox(fontNames);
m_cbFonts.setMaximumSize(m_cbFonts.getPreferredSize());
m_cbFonts.setEditable(true);
ActionListener lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
m_fontName = m_cbFonts.getSelectedItem().toString();
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setFontFamily(attr, m_fontName);
setAttributeSet(attr);
m_monitor.grabFocus();
}
};
m_cbFonts.addActionListener(lst);
m_toolBar.add(m_cbFonts);
m_toolBar.addSeparator();
m_cbSizes = new JComboBox(new String[] {"8", "9", "10",
"11", "12", "14", "16", "18", "20", "22", "24", "26",
"28", "36", "48", "72"});
m_cbSizes.setMaximumSize(m_cbSizes.getPreferredSize());
m_cbSizes.setEditable(true);
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
int fontSize = 0;
try {
fontSize = Integer.parseInt(m_cbSizes.
getSelectedItem().toString());
}
catch (NumberFormatException ex) { return; }
m_fontSize = fontSize;
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setFontSize(attr, fontSize);
setAttributeSet(attr);
m_monitor.grabFocus();
}
};
m_cbSizes.addActionListener(lst);
m_toolBar.add(m_cbSizes);
m_toolBar.addSeparator();
ImageIcon img1 = new ImageIcon("font_bold1.gif");
ImageIcon img2 = new ImageIcon("font_bold2.gif");
m_bBold = new SmallToggleButton(false, img1, img2,
"Bold font");
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setBold(attr, m_bBold.isSelected());
setAttributeSet(attr);
m_monitor.grabFocus();
}
};
m_bBold.addActionListener(lst);
m_toolBar.add(m_bBold);
img1 = new ImageIcon("font_italic1.gif");
img2 = new ImageIcon("font_italic2.gif");
m_bItalic = new SmallToggleButton(false, img1, img2,
"Italic font");
lst = new ActionListener() {
public void actionPerformed(ActionEvent e) {
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setItalic(attr, m_bItalic.isSelected());
setAttributeSet(attr);
m_monitor.grabFocus();
}
};
m_bItalic.addActionListener(lst);
m_toolBar.add(m_bItalic);
getContentPane().add(m_toolBar, BorderLayout.NORTH);
return menuBar;
}
protected void showAttributes(int p) {
m_skipUpdate = true;
AttributeSet a = m_doc.getCharacterElement(p).
getAttributes();
String name = StyleConstants.getFontFamily(a);
if (!m_fontName.equals(name)) {
m_fontName = name;
m_cbFonts.setSelectedItem(name);
}
int size = StyleConstants.getFontSize(a);
if (m_fontSize != size) {
m_fontSize = size;
m_cbSizes.setSelectedItem(Integer.toString(m_fontSize));
}
boolean bold = StyleConstants.isBold(a);
if (bold != m_bBold.isSelected())
m_bBold.setSelected(bold);
boolean italic = StyleConstants.isItalic(a);
if (italic != m_bItalic.isSelected())
m_bItalic.setSelected(italic);
m_skipUpdate = false;
}
protected void setAttributeSet(AttributeSet attr) {
if (m_skipUpdate)
return;
int xStart = m_monitor.getSelectionStart();
int xFinish = m_monitor.getSelectionEnd();
if (!m_monitor.hasFocus()) {
xStart = m_xStart;
xFinish = m_xFinish;
}
if (xStart != xFinish) {
m_doc.setCharacterAttributes(xStart, xFinish - xStart,
attr, false);
}
else {
MutableAttributeSet inputAttributes =
m_kit.getInputAttributes();
inputAttributes.addAttributes(attr);
}
}
public static void main(String argv[]) {
new WordProcessor();
}
}
// Unchanged code from section 20.1
// Class SmallToggleButton unchanged from section 12.4
Understanding the Code
Class WordProcessor
Several new instance variables have been added:
JComboBox m_cbFonts: toolbar component to select the font name.
JComboBox m_cbSizes: toolbar component to select the font size.
SmallToggleButton m_bBold: toolbar component to select the bold font style.
SmallToggleButton m_bItalic: toolbar component to select the italic font style.
String m_fontName: current font name.
int m_fontSize: current font size.
boolean m_skipUpdate: flag used to skip word processor update (see below).
int m_xStart: used to store the selection start position.
int m_xFinish: used to store the selection end position.
The constructor of the WordProcessor class adds a CaretListener to our m_monitor text pane. The caretUpdate() method of this listener is invoked whenever the caret position is changed. The showAttributes() (see below) will be called in response to update the toolbar components and display the currently selected font attributes.
A FocusListener is also added to our m_monitor component. The two methods of this listener, focusGained() and focusLost(), will be invoked when the editor gains and loses the focus respectively. The purpose of this implementation is to save and restore the starting and end positions of the text selection. The reason we do this is because Swing supports only one text selection at any time throughout an app. This means that if the user selects some text in the editor component to modify it's attributes, and then goes off and makes a text selection in some other component, the original text selection will disappear. This can potentially be very annoying to the user. To fix this problem we save the selection before the editor component loses the focus. When the focus is gained we restore the previously saved selection. We distinguish between two possible situations: when the caret is located at the beginning of the selection and when it is located at the end of the selection. In the first case we position the caret at the end of the stored interval with the setCaretPosition() method, and then move the caret backward to the beginning of the stored interval with the moveCaretPosition() method. The second situation is easily handled using the select() method.
The showAttributes() method is now called prior to the display of a new document or a newly loaded document.
The createMenuBar() method creates new components to manage font properties for the selected text interval. First, the m_cbFonts combo box is used to select the font family name. Unlike the example in chapter 12, which used several pre-defined font names, this example uses all fonts available to the user's system. A complete list of the available font names can be obtained through the getAvailableFontFamilyNames() method of GraphicsEnvironment (see 2.8). Also note that the editable property of this combobox component is set to true, so the font name can be both selected from the drop-down list and enetered in by hand.
Once a new font name is selected, it is applied to the selected text through the use of an attached ActionListener. The selected font family name is assigned to a SimpleAttributeSet instance with the StyleConstants.setFontFamily() method. Then our custom setAttributeSet() (see below) is called to modify the attributes of the selected text according to this SimpleAttributeSet.
The m_cbSizes combo box is used to select the font size. It is initiated with a set of pre-defined sizes. The editable property is set to true so the font size can be both selected from the drop-down list and entered by hand. Once a new font size is selected, it is applied to the selected text through the use of an attached ActionListener. The setup is similar to that used for the m_cbFonts component. The StyleConstants.setFontSize() method is used to set the font size. Our custom setAttributeSet() method is then used to apply this attribute set to the selected text.
The bold and italic properties are managed by two SmallToggleButtons (a custom button class we developed in chapter 12): m_bBold and m_bItalic respectively. These buttons receive ActionListeners which create a SimpleAttributeSet instance with the bold or italic property with StyleConstants.setBold() or StyleConstants.setItalic(). Then our custom setAttributeSet() method is called to apply this attribute set.
The showAttributes() method is called to set the state of the toolbar components described above according to the font properties of the text at the given caret position. This method sets the m_skipUpdate flag to true at the beginning and false at the end of it's execution (the purpose of this will be explained soon below). Then an AttributeSet instance corresponding to the character element at the current caret position in the editor's document is retrieved with the getAttributes() method. The StyleConstants.getFontFamily() method is used to retrieve the current font name from this attribute set. If it is not equal to the previously selected font name (stored in the m_fontName instance variable) it is selected in the m_cbFonts combobox. The other toolbar controls are handled in a similar way.
Our setAttributeSet() method is used to assign a given set of attributes to the currently selected text. Note that this method does nothing (simply returns) if the m_skipUpdate flag is set to true. This is done to prevent the backward link with the showAttributes() method. As soon as we assign some value to a combo boc in the showAttributes() method (e.g. font size) this internally triggers a call to the setAttributeSet() method (because ActionListeners attached to combo boxes are invoked even when selection changes occur programmatically). The purpose of showAttributes() is to simply make sure that the attributes corresponding to the character element at the current text position are accurately reflected in the toolbar components. To prevent the combo box ActionListeners from invoking unnecessary operations we prohibit any text property updates from occuring in setAttributeSet() while the showAttributes() method is being executed (this is the whole purpose of the m_skipUpdate flag).
The setAttributeSet() method first determines the start and end positions of the selected text. If m_monitor currently does not have the focus, the stored bounds, m_xStart and m_xFinish, are used instead. If the selection is not empty (xStart != xFinish), the setCharacterAttributes() method is called to assign the given set of attributes to the selection. Note that this new attribute set does not have to contain a complete set of attributes. It simply replaces only the existing attributes for which it has new values, leaving the remainder unchanged. If the selection is empty, the new attributes are added to the input attributes of the editor kit (recall that StyledEditorKit's input attributes are those attributes that will be applied to newly inserted text).
Running the Code
Open an existing RTF file and move the cursor to various positions in the text. Note that the text attributes displayed in the toolbar components are updated correctly. Select a portion of text and use the toolbar components to modify the selection's font attributes. Type a new font name or font size in the editable combobox and press "Enter." This has the same effect as selecting a choice from the drop-down list. Save the RTF file and open it in another RTF-aware application to verify that your changes were saved correctly.
Bug Alert! Bold and italic font properties are often not updated on the screen properly, even though they are assigned and saved correctly. We hope that this problem will be fixed in future Swing releases.



RSS feed Java FAQ News