Table of contents
1.
Introduction
2.
JEditorPane Content Types
3.
Constructors 
4.
Fields
5.
Methods
6.
Example
7.
Load Content
8.
Frequently Asked Questions
8.1.
What is the Jeditor pane?
8.2.
What is a Textpane?
8.3.
What is a Java editor pane?
8.4.
What is a JScrollPane?
8.5.
In Java, what is JRootpane?
9.
Conclusion
Last Updated: Mar 27, 2024

JEditorPane

Author Shiva
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JEditorPane Class is used to create a simple text editor window. To carry out its functions, this component makes use of EditorKit implementations. It effectively transforms into the appropriate type of text editor for the content it receives. The EditorKit currently installed determines which content type that editor is connected to at any one time. If the content is set to a new URL, the type of the content is utilized to determine which EditorKit should be used to load it.

JEditorPane Content Types

JEditorPane supports three types of content:

  • Plaintext

The type specified isn't recognized as plain text, which is the default. The kit in this example is a wrapper for DefaultEditorKit, which generates a plain text view.

  • HTML

Text in HTML format. The class javax.swing.text.html is the kit used in this scenario. HTMLEditorKit is an HTML 3.2 editor kit.

  • RTF text

The kit in this situation is javax.swing.text.rtf.RTFEditorKit, which only supports the Rich Text Format to a limited extent.

Constructors 

Depending on the use case you can create JEditorPane with multiple parameters, JEditorPane has 4 Constructors:

  • JEditorPane()

It makes a new JEditorPane for you.

  • JEditorPane(String url)

It produces a JEditorPane based on a URL specification in a string.

  • JEditorPane(String type, String text)

It generates a JEditorPane with the specified text as its default.

  • JEditorPane(URL initialPage)

It generates a JEditorPane for input based on a URL. 

Fields

JEditor has 2 fields, both of type static string:

  • HONOR_DISPLAY_PROPERTIES

If no font or foreground color is given in the styled text, this key is used to indicate whether the component's default font and foreground color are utilized.

  • W3C_LENGTH_UNITS

If w3c compliant length units are used for HTML rendering, this is the key for a client property.

Methods

Here is a list of some useful methods in JEditorpane Class, for more refer here.

  • setText(String t)

It changes the text of this TextComponent to the provided content, which should be in the format of this editor's content type.

It’s return type is void.

  • setContentType(String type)

It specifies the type of content that this editor is capable of handling.

Its return type is void.

  • setPage(URL page).

It changes the currently displayed URL.

Its return type is void.

  • scrollToReference(String reference)

It moves the view to the specified reference point (that is, the value returned by the UL.getRef method for the URL being displayed).

Its return type is void.

  • getText()

It returns the text that is contained in this TextComponent in terms of the editor's content type.

Its return type is a string.

  • createDefaultEditorKit()

When a component is first formed, it builds the default editor kit (PlainEditorKit).

Its return type is protected EditorKit

Example

Given below is a basic example of JEditorPane Class. Using JEditorPane, we are simply displaying some HTML text.

import javax.swing.*;
public class JEditorPaneClass extends JFrame {
   public JEditorPaneClass() {
      JEditorPane editorPane = new JEditorPane();
      editorPane.setContentType("text/html");
      editorPane.setText("<h1>Heading 1</h1><h2>Heading 2</h2><h3>Heading 3</h3><h4>Heading 4</h4><h5>Heading 5</h5>");
      setSize(350, 275);
      setContentPane(editorPane);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
   }
   public static void main(String[] a) {
      new JEditorPaneClass();
   }
}

 

Output:

Load Content

This component can be loaded with the material in a variety of ways.

  • To initialize the component from a string, use the setText function. In this instance, the existing EditorKit will be used, and this type of content will be expected.
  • To initialize the component from a Reader, use the read method. Note that unless the <base> tag is used or the base property on HTMLDocument is set, relative references (e.g. for things like images) cannot be resolved if the content type is HTML. In this instance, the existing EditorKit will be used, and this type of content will be expected.
  • To initialize the component from a URL, use the setPage function. The content type is determined from the URL in this example, and the registered EditorKit for that content type is set.


Also check out swing component in java

Frequently Asked Questions

What is the Jeditor pane?

A JEditorPane is a text area that supports a variety of text types. JEditorPane supports HTML and RTF (Rich Text Format) by default, but we can create our own editor kits to handle different text types.

What is a Textpane?

JTextPane is a descendant of JEditorPane. For stylized documents with embedded graphics and components, JTextPane is utilized. It's a text component that can be set up with graphically rendered characteristics.

What is a Java editor pane?

A simple text editor window is created using the JEditorPane class. setContentType() and setText() are methods in this class. setContentType("text/plain"): This method is used to make plain text the content type.

What is a JScrollPane?

A scrollable view of a component is provided by a JScrollPane. Use a scroll pane to display a component that is huge or whose size can change dynamically when screen real estate is limited. Split panes and tabbed panes are two other containers for saving screen space.

In Java, what is JRootpane?

A glassPane, an optional menuBar, and a contentPane make up a JRootpane. (The menuBar and contentPane are managed by the JLayeredPane.) The glassPane is positioned on top of everything, allowing it to intercept mouse movements.

Conclusion

In this article, we have extensively discussed JEditorPane in Java.  We hope that the above article will assist you in preparing for your upcoming interviews. Here are more articles that you might find useful: 

Graphics in java swing

Using Tooltip in Java Swing

JLabel in Java

Refer to our guided paths on Coding Ninjas Studio to learn more about DSACompetitive ProgrammingJavaScriptSystem Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.
Do upvote our blog to help other ninjas grow.
Happy Learning!

 

Live masterclass