Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Swing Chapter 20. (Advanced topics) Constructing a Word Processor. Easy for reading, Click here!

Custom Search
Swing Chapter 20. (Advanced topics) Constructing a Word Processor. Easy for reading, Click here!

[ Return to Swing (Book) ]

Page: 2/9 



Previous Page Previous Page (1/9) - Next Page (3/9) Next Page
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.



[ Return to Swing (Book) ]


Top 10 read Java Articles
 Get free "1000 Java Tips eBook"

 Java Calendar and Date: good to know facts and code examples

 Array vs ArrayList vs LinkedList vs Vector: an excellent overview and examples

 How can I convert any Java Object into byte array? And byte array to file object

 The Java Lesson 1: What is Java?

 How do I compare two dates and times, date between dates, time between times and

 Maven vs Ant or Ant vs Maven?

 How to open, read, write, close file(s) in Java? Examples on move, rename and de

 Java Array

 Java: JLabel font and color


[ More in News Section ]
Java Lessons

The Java Lesson 1:
What is Java?
The Java Lesson 2:
Anatomy of a simple Java program
The Java Lesson 3:
Identifiers and primitive data types
The Java Lesson 4:
Variables, constants, and literals
The Java Lesson 5:
Arithmetic operations, conversions, and casts
The Java Lesson 6:
Boolean expressions and operations
The Java Lesson 7:
Bitwise operations
The Java Lesson 8:
Flow control with if and else
The Java Lesson 9:
switch statements
The Java Lesson 10:
for, while, and do-while statements
The Java Lesson 11:
Using break and continue
The Java Lesson 12:
Class methods and how they are called
The Java Lesson 13:
Using the Math class
The Java Lesson 14:
Creating and calling custom class methods
The Java Lesson 15:
Overloading class methods
The Java Lesson 16:
An introduction to objects and object references
The Java Lesson 17:
The String class
The Java Lesson 18:
The StringBuffer class
The Java Lesson 19:
Initializing and processing arrays of primitives
The Java Lesson 20:
Initializing and processing arrays of objects
The Java Lesson 23:
Inheritance and overriding inherited methods
The Java Lesson 24:
abstract classes and polymorphism
The Java Lesson 25:
Interfaces, instanceof, and object conversion and casting
The Java Lesson 26:
Introduction to graphical programming and the java.awt packa
The Java Lesson 27:
The Component class
The Java Lesson 28:
Containers and simple layout managers
The Java Lesson 29:
The Color and Font classes
The Java Lesson 30:
Drawing geometric shapes
The Java Lesson 31:
Choice, List, and Checkbox controls
The Java Lesson 32:
Using the Scrollbar graphical control
The Java Lesson 33:
Menus and submenus
The Java Lesson 34:
An introduction to applets and the Applet class
The Java Lesson 35:
Essential HTML to launch an applet and pass it parameters
The Java Lesson 36:
Mouse event processing
Java Lesson 37:
Menus and submenus
Java Lesson 38:
The WindowListener interface and the WindowAdapter class
Java Lesson 39:
An introduction to GridBagLayout
Java Lesson 40:
An introduction to the Java Collections API
Java Lesson 41:
Exception handling with try, catch, and finally blocks
Java Lesson 42:
Claiming and throwing exceptions
Java Lesson 43:
Multithreading, the Thread class, and the Runnable interface
Java Lesson 44:
An introduction to I/O and the File and FileDialog classes
Java Lesson 45:
Low-level and high-level stream classes
Java Lesson 46:
Using the RandomAccessFile class
Java Lessons by
Joh Huhtala: Update

Latest articles
 Java Profiler JProbe to Resolve Performance Problems Faster

 SSL with GlassFish v2, page 5

 SSL with GlassFish v2, page 4

 SSL with GlassFish v2, page 3

 SSL with GlassFish v2, page 2

 The Java Lesson 2: Anatomy of a simple Java program, page 2

 New site about Java for robots and robotics: both software and hardware.

 Exceptions -III: What's an exception and why do I care?

 Exceptions -II: What's an exception and why do I care?

 Exceptions: What's an exception and why do I care?

 Double your Java code quality in 10 minutes, here is receipt

 Murach's Java Servlets and JSP

 How to get ascii code from a char in Java?

 Can we just try without catch? Yes!

 Make Tomcat page load faster

 Make your Tomcat More secure - limit network address for certain IP addresses

 New Java book online starts now here...

 Implementing RESTful Web Services in Java

 Firefox trimming from 1 GB to 40 Mb with many tabs opened

 SSL with GlassFish v2

 My request to replublish Tech Tips

 Search JavaFAQ.nu site here

 New Advanced Installer for Java 6.0 brings XML updates and imports 3rd party MSI

 EJB programming restrictions

 Maven vs Ant or Ant vs Maven?

 Why Java does not use default value which it should?

 How to unsign signed bytes in Java - your guide is here

 The Java Lesson 3: Identifiers and primitive data types. Page 2

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 4

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 3


[ More in News Section ]


Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy