What is JTextPane - Java Swing

Java examples for Swing:JTextPane

Introduction

JTextPane is a subclass of the JEditorPane class.

JTextPane handles the styled document with embedded images and components.

The following code develops a basic word processor using a JTextPane.

It lets you edit text and apply styles such as bold, italics, color, and alignment to the text.

Demo Code

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

public class Main extends JFrame {
  JTextPane textPane = new JTextPane();

  JButton normalBtn = new JButton("Normal");
  JButton boldBtn = new JButton("Bold");
  JButton italicBtn = new JButton("Italic");
  JButton underlineBtn = new JButton("Underline");
  JButton superscriptBtn = new JButton("Superscript");
  JButton blueBtn = new JButton("Blue");
  JButton leftBtn = new JButton("Left Align");
  JButton rightBtn = new JButton("Right Align");

  public Main(String title) {
    super(title);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = this.getContentPane();

    JPanel buttonPanel = this.getButtonPanel();
    contentPane.add(buttonPanel, BorderLayout.NORTH);
    contentPane.add(textPane, BorderLayout.CENTER);

    addStyles();/*  w ww.j  a  v a 2  s.  co  m*/
    StyledDocument document = textPane.getStyledDocument();
    try {
      document.insertString(0, "Hello JTextPane\n", null);
    } catch (BadLocationException e) {
      e.printStackTrace();
    }
  }

  private JPanel getButtonPanel() {
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(normalBtn);
    buttonPanel.add(boldBtn);
    buttonPanel.add(italicBtn);
    buttonPanel.add(underlineBtn);
    buttonPanel.add(superscriptBtn);
    buttonPanel.add(blueBtn);
    buttonPanel.add(leftBtn);
    buttonPanel.add(rightBtn);

    normalBtn.addActionListener(e -> setNewStyle("normal", true));
    boldBtn.addActionListener(e -> setNewStyle("bold", true));
    italicBtn.addActionListener(e -> setNewStyle("italic", true));
    underlineBtn.addActionListener(e -> setNewStyle("underline", true));
    superscriptBtn.addActionListener(e -> setNewStyle("superscript", true));
    blueBtn.addActionListener(e -> setNewStyle("blue", true));
    leftBtn.addActionListener(e -> setNewStyle("left", false));
    rightBtn.addActionListener(e -> setNewStyle("right", false));

    return buttonPanel;
  }

  private void addStyles() {
    StyleContext sc = StyleContext.getDefaultStyleContext();
    Style defaultContextStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);

    StyledDocument document = textPane.getStyledDocument();
    Style normalStyle = document.addStyle("normal", defaultContextStyle);

    Style boldStyle = document.addStyle("bold", normalStyle);
    StyleConstants.setBold(boldStyle, true);

    Style italicStyle = document.addStyle("italic", normalStyle);
    StyleConstants.setItalic(italicStyle, true);

    Style underlineStyle = document.addStyle("underline", normalStyle);
    StyleConstants.setUnderline(underlineStyle, true);

    Style superscriptStyle = document.addStyle("superscript", normalStyle);
    StyleConstants.setSuperscript(superscriptStyle, true);

    Style blueColorStyle = document.addStyle("blue", normalStyle);
    StyleConstants.setForeground(blueColorStyle, Color.BLUE);

    Style leftStyle = document.addStyle("left", normalStyle);
    StyleConstants.setAlignment(leftStyle, StyleConstants.ALIGN_LEFT);

    Style rightStyle = document.addStyle("right", normalStyle);
    StyleConstants.setAlignment(rightStyle, StyleConstants.ALIGN_RIGHT);
  }

  private void setNewStyle(String styleName, boolean isCharacterStyle) {
    StyledDocument document = textPane.getStyledDocument();
    Style newStyle = document.getStyle(styleName);
    int start = textPane.getSelectionStart();
    int end = textPane.getSelectionEnd();
    if (isCharacterStyle) {
      boolean replaceOld = styleName.equals("normal");
      document.setCharacterAttributes(start, end - start, newStyle, replaceOld);
    } else {
      document.setParagraphAttributes(start, end - start, newStyle, false);
    }
  }
  public static void main(String[] args) {
    Main frame = new Main("Word Processor");
    frame.setSize(700, 500);
    frame.setVisible(true);
  }
}

Related Tutorials