Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: User Interface Design
Expertise: Beginner
Sep 15, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Textediting functions in Java

I'm trying to make a text editor in Java, and I want the editor to be able to make words and even pieces of words italic, bold, or underlined. It must also be able to change the color of pieces of text and the font. Is there a component that can use different fonts, styles and colors for the text?

The Swing package provides a powerful text component, called JTextPane, which is able to change the styles of arbitrary character runs. If you download the Swing package, there is an extensive example called Stylepad.java that demonstrates how to change text attributes and even include images. Using different styles involes three main parts:
First, there is the Document, in this case a StyledDocument, defined in com.sun.java.swing.text. Classes derived from Document store the text of a document.
Second, there is the EditorKit, in this case a StyledEditorKit, also defined in com.sun.java.swing.text, which takes care of changing the styles contained in a document.
Third, there is the JTextPane, which displays the text in the document, rendering all style attributes. The following example demonstrates how to get started using these classes to change text styles in a JTextPane. For more detailed information, you should write a couple of test programs on your own, read the Swing documentation, and look at the Stylepad example included with the Swing distribution. The following example allows you to switch between two colors and add bold and italic text.

import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
import com.sun.java.swing.text.*;

public final class StyledText extends JFrame {
  private JTextPane __textPane;
  private JScrollPane __scrollPane;
  private JRadioButton __blackButton, __greenButton;
  private JCheckBox __boldCheckBox, __italicCheckBox;
  private JPanel __buttonPanel;

  public StyledText() {
    super("Styled Text Demo");
    Container contentPane;
    StyleContext context  = new StyleContext();
    DefaultStyledDocument document = new DefaultStyledDocument(context);
    ActionListener action;
    ButtonGroup buttonGroup;

    __textPane = new JTextPane(document);

    __scrollPane = new JScrollPane();

    buttonGroup = new ButtonGroup();
    __blackButton  = new JRadioButton("Black", true);
    __greenButton  = new JRadioButton("Green", false);

    __boldCheckBox   = new JCheckBox("Bold");
    __italicCheckBox = new JCheckBox("Italic");

    action = new StyledEditorKit.ForegroundAction("set-foreground-black",
    action = new StyledEditorKit.ForegroundAction("set-foreground-green",

    action  = new StyledEditorKit.BoldAction();

    action  = new StyledEditorKit.ItalicAction();

    __buttonPanel = new JPanel();

    contentPane = getContentPane();
    contentPane.setLayout(new BorderLayout());

    contentPane.add(__scrollPane, BorderLayout.CENTER);
    contentPane.add(__buttonPanel, BorderLayout.SOUTH);

  public static void main(String[] args) {
    StyledText styledText;
    WindowListener exitListener;

    exitListener = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
	Window window = e.getWindow();

    styledText = new StyledText();
    styledText.setSize(320, 200);
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date