Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the differences between a JTextPane and a JEditorPane in Java?
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.
Methods
The important methods of JTextPane are:
addStyle()
Adds a new style to the logical style hierarchy. Styles can be used to define formatting attributes (e.g., font, color) for text in a JTextPane.
Method Initialization:
JTextPane textPane = new JTextPane();
Style style = textPane.addStyle("BoldRed", null);
getCharacterAttributes()
Fetches the character-level attributes (e.g., font, color) at the current caret position or selection in a JTextPane.
Method Initialization:
JTextPane textPane = new JTextPane(); AttributeSet attrs = textPane.getCharacterAttributes();
getStyledDocument()
Returns the StyledDocument associated with the JTextPane or JEditorPane, which allows manipulation of styled text (e.g., applying fonts, colors).
Method Initialization:
JTextPane textPane = new JTextPane(); StyledDocument doc = textPane.getStyledDocument();
setDocument()
Associates a Document model (e.g., PlainDocument, StyledDocument) with the JTextPane or JEditorPane. This controls the content and structure of the text.
Method Initialization:
JTextPane textPane = new JTextPane(); textPane.setDocument(new DefaultStyledDocument());
setEditorKit()
Sets the EditorKit (e.g., for HTML, RTF, or plain text) that determines how content is handled in JEditorPane or JTextPane.
Method Initialization:
JEditorPane editorPane = new JEditorPane(); editorPane.setEditorKit(new HTMLEditorKit());
setStyledDocument()
Sets a StyledDocument (e.g., DefaultStyledDocument) for JTextPane, enabling styled text (fonts, colors, paragraphs).
Method Initialization:
JTextPane textPane = new JTextPane(); textPane.setStyledDocument(new DefaultStyledDocument());
Use Cases for JTextPane
The following are some of the use cases for JTextPane in Java:
- Advanced text editing applications are similar to word processors.
- Applications requiring detailed text styling, like desktop publishing tools.
- When you need to embed other components within text, such as images or buttons.
Example of JTextPane
Below is an example of JTextPane on Swing GUI in Java:
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 that 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.
Methods
The important methods of JEditorPane are:
setContentType()
The setContentType() method is used to choose the document we want to display.
Method Initialization:
JEditorPane editorPane = new JEditorPane();
editorPane.setContentType("text/html");
setEditorKit()
The setEditorKit() method is used to set a custom editor for JEditorPane explicitly.
Method Initialization:
JEditorPane editorPane = new JEditorPane(); editorPane.setEditorKit(new HTMLEditorKit());
Use Cases for JEditorPane
The following are some of the use cases for JEditorPane in Java:
- Displaying HTML content or help files within an application.
- Basic text editing with HTML support, like simple web page editors or viewers.
- Situations where you need to show formatted text but don't require complex editing features.
Example of JEditorPane
Below is an example of JEditorPane on a Swing GUI in Java:
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
Difference Table
Below is the difference table to show the difference between a JTextPane and a JEditorPane in Java:
| Criteria | JEditorPane | JTextPane |
|---|---|---|
| Purpose | General text display | Advanced text editing |
| Format Support | Plain, HTML, RTF | All of JEditorPane plus more |
| Styling | Basic (document-level) | Character/paragraph-level |
| Embedding | No | Yes (components, images) |
| Complexity | Simpler | More complex |