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.
Contact Us