Following are the programs to implement JEditorPane

Example 1:

Java




// Java Program to Implement a JEditorPane with HTML Content
// Type
import javax.swing.*;
  
public class JEditorPaneExample {
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(() -> {
            // Create a JFrame
            JFrame frame
                = new JFrame("JEditorPane Example");
            frame.setDefaultCloseOperation(
                JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 300);
  
            // Create a JEditorPane
            JEditorPane editorPane = new JEditorPane();
            // Set content type to HTML
            editorPane.setContentType("text/html");
  
            // Set HTML content
            String htmlContent
                = "<html><body><h1>w3wiki</body></html>";
            editorPane.setText(htmlContent);
  
            // Add the JEditorPane to the frame
            frame.add(editorPane);
  
            // Make the frame visible
            frame.setVisible(true);
        });
    }
}


Output:

Example 2:

Java




// Java Program to Implement a JEditorPane with RTF(Rich
// Text Format) Content Type
import javax.swing.*;
  
public class JEditorPaneExample {
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(() -> {
            // Create a JFrame
            JFrame frame
                = new JFrame("JEditorPane Example");
            frame.setDefaultCloseOperation(
                JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 300);
  
            // Create a JEditorPane
            JEditorPane editorPane = new JEditorPane();
            // Set content type to RTF
            editorPane.setContentType("text/rtf");
  
            // Set RTF content
            String rtfContent
                = "{\\rtf1\\ansi\\deff0 {\\fonttbl {\\f0 Times New Roman;}} {\\colortbl ;\\red0\\green0\\blue255; } w3wiki}\n";
  
            editorPane.setText(rtfContent);
  
            // Add the JEditorPane to the frame
            frame.add(editorPane);
  
            // Make the frame visible
            frame.setVisible(true);
        });
    }
}


Output:

Example 3:

Java




// Java Program to Implement a JEditorPane with different
// Font Style
import java.awt.*;
import javax.swing.*;
  
public class JEditorPaneSetFontExample {
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(() -> {
            // Create a JFrame
            JFrame frame = new JFrame(
                "JEditorPane setFont() Example");
            frame.setDefaultCloseOperation(
                JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 300);
  
            // Create a JEditorPane
            JEditorPane editorPane = new JEditorPane();
            // Set content type to plain
            // text
            editorPane.setContentType("text/plain");
  
            // Set text content
            editorPane.setText("w3wiki");
  
            // Create a custom font
            Font customFont
                = new Font("Serif", Font.BOLD, 16);
  
            // Set the font for the editor pane
            editorPane.setFont(customFont);
  
            // Add the JEditorPane to the frame
            frame.add(editorPane);
  
            // Make the frame visible
            frame.setVisible(true);
        });
    }
}


Output:



Java JEditorPane

A JEditorPane is a component in Java Swing that provides a simple way to display and edit HTML and RTF (Rich Text Format) content. It is a versatile component that allows you to display styled text, links, and images, making it useful for creating simple web browsers, rich-text editors, and content viewers within your Java applications. JEditorPane is a part of javax.swing package.

Similar Reads

Commonly used constructors

...

Some Methods of the JEditorPane

Methods Description setText(String text) Sets the text content of the editor pane. setPage(String url) Sets the page content from the specified URL. The content type is determined based on the URL’s file extension. setContentType(String type) Sets the content type of the editor pane (e.g., “text/plain,” “text/html,” “text/rtf”). setEditorKit(EditorKit kit) Sets the editor kit used to interpret and display the content. getDocument() Returns the document model associated with the editor pane. getContentType() Returns the content type of the editor pane. getEditorKitForContentType(String type) Returns the default editor kit for the specified content type. getCaret() Returns the caret (cursor) used for text editing. setEditable(boolean editable) Sets whether the editor pane is editable or not. getSelectedText() Returns the currently selected text....

Following are the programs to implement JEditorPane

Example 1:...

Contact Us