Textediting functions in Java

I’m trying to make a text editor in Java, andI 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. Ifyou download the Swing package, there is an extensive example called Stylepad.java that demonstrates how to change text attributes andeven 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 casea 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);  }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS