Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


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

Textediting functions in Java

Question:
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?

Answer:
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();
    __scrollPane.getViewport().add(__textPane);

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

    __boldCheckBox   = new JCheckBox("Bold");
    __boldCheckBox.setRequestFocusEnabled(false);
    __italicCheckBox = new JCheckBox("Italic");
    __italicCheckBox.setRequestFocusEnabled(false);

    action = new StyledEditorKit.ForegroundAction("set-foreground-black",
						  Color.black);
    __blackButton.addActionListener(action);
    action = new StyledEditorKit.ForegroundAction("set-foreground-green",
						  Color.green);
    __greenButton.addActionListener(action);

    action  = new StyledEditorKit.BoldAction();
    __boldCheckBox.addActionListener(action);

    action  = new StyledEditorKit.ItalicAction();
    __italicCheckBox.addActionListener(action);

    __buttonPanel = new JPanel();
    __buttonPanel.add(__blackButton);
    __buttonPanel.add(__greenButton);
    __buttonPanel.add(__boldCheckBox);
    __buttonPanel.add(__italicCheckBox);

    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();
	window.setVisible(false);
	window.dispose();
	System.exit(0);
      }
    };

    styledText = new StyledText();
    styledText.addWindowListener(exitListener);
    styledText.pack();
    styledText.setSize(320, 200);
    styledText.setVisible(true);
  }
}
DevX Pro
 
Comment and Contribute

 

 

 

 

 


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

 

 

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