- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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?
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
- Related Articles
- What are the differences between a MouseListener and a MouseMotionListener in Java?
- What are the differences between a JComboBox and a JList in Java?
- What are the differences between a JScrollBar and a JScrollPane in Java?
- What are the differences between a JTextField and a JFormattedTextField in Java?
- What are the differences between a Font and a FontMetrics in Java?\n
- What are the differences between a static block and a constructor in Java?
- What are the differences between a class and an interface in Java?
- What are the differences between C++ and Java?
- What are the differences between C and Java?
- What are the differences between Java classes and Java objects?
- What are the differences between JRadioButton and JCheckBox in Java?
- What are the differences between recursion and iteration in Java?
- What are the differences between GridLayout and GridBagLayout in Java?
- What are the differences between JTextField and JTextArea in Java?
- What are the differences between StackOverflowError and OutOfMemoryError in Java?

Advertisements