Tutorialspoint

1 Answer
raja ramesh

A JTextPane is an extension of JEditorPane which provides word processing features like fonts, text styles, colors and etc. If we need to do heavy-duty text processing we can use this class whereas a JEditorPane supports display/editing of HTML and RTF content and can be extended by creating our own EditorKit.

JTextPane

  • A JTextPane is a subclass of JEditorPane.
  • A JTextPane is used for a styled document with embedded images and components.
  • A JTextPane is a text component that can be marked up with the attributes that are represented graphically and it can use a DefaultStyledDocument as the default model.
  • The important methods of JTextPane are addStyle(), getCharacterAttributes(), getStyledDocument(), setDocument(), setEditorKit(), setStyledDocument() and etc.

Example

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class JTextPaneTest {
   public static void main(String args[]) throws BadLocationException {
      JFrame frame = new JFrame("JTextPane Test");
      Container cp = frame.getContentPane();

      JTextPane pane = new JTextPane();
      SimpleAttributeSet set = new SimpleAttributeSet();
      StyleConstants.setBold(set, true);
      pane.setCharacterAttributes(set, true);
      pane.setText("Welcome to");
      set = new SimpleAttributeSet();
      StyleConstants.setItalic(set, true);
      StyleConstants.setForeground(set, Color.blue);

      Document doc = pane.getStyledDocument();
      doc.insertString(doc.getLength(), " Tutorials ", set);
      set = new SimpleAttributeSet();
      StyleConstants.setFontSize(set, 20);
      doc.insertString(doc.getLength(), " Point", set);
      JScrollPane scrollPane = new JScrollPane(pane);
      cp.add(scrollPane, BorderLayout.CENTER);

      frame.setSize(375, 250);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }
}

Output

JEditorPane

  • A JEditorPane is a kind of text area which can display various text formats
  • By default, JEditorPane supports HTML and RTF (Rich Text Format), we can build our own editor kits to handle a specific content type.
  • We can use setContentType() method to choose the document we want to display and setEditorKit() method to set custom editor for JEditorPane explicitly.

Example

import javax.swing.*;

public class JEditorPaneTest extends JFrame {
   public JEditorPaneTest() {
      setTitle("JEditorPane Test");
      JEditorPane editorPane = new JEditorPane();
      editorPane.setContentType("text/html");
      editorPane.setText("<h1>Java</h1><p>is a general-purpose computer programming language that is   concurrent, class-based, object-oriented, and specifically designed to have as few implementation   dependencies as possible.</p>");
      setSize(350, 275);
      setContentPane(editorPane);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
   }
   public static void main(String[] a) {
      new JEditorPaneTest();
   }
}

Output

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.