Updated July 5, 2023
Introduction to JEditorPane
JEditorPane class in java is used to display text components that can handle different types of text with style. By default, it can handle only HTML, plain and Rich Text Format ( RTF ). JEditorPane is being primarily used to display HTML content with limited basic HTML tags.
In order to have this behavior, this component uses the implementations of the EditorKit. The beauty is that it automatically adjusts to the proper kind of text editor for whichever kind of content it is provided. The EditorKit which is currently installed is used to determine the content that the editor is bound to at any given time. For example, if the content of a component is set to a new URL, then its type is used to determine the EditorKit that should be preinstalled to load the content.
Syntax:
public class JEditorPane extends JTextComponent
By Default this class is preconfigured to the following three types of content:
- text/plain: Plain Text, which is the default type when the content is not recognized. The kit used over here is an extension of DefaultEditorKit that will produce a wrapped plain text view.
- text/HTML: HTML Text. The kit used over here is class javax.swing.text.html.HTMLEditorkit which will provide support till HTML (ver. 3.2).
- text/RTF: RTF Text. The kit used over here is class javax.swing.text.rtf.RTFEditorkit which will provide limited support Rich Text Format.
Constructors of JEditorPane
Below are the constructors of JEditorPane:
- JEditorPane( ): This type of constructor will simply create a new JEditorPane.
- JEditorPane(String URL): This type of constructor will create a JEditorPane based on the string in the parameter containing the URL specifications.
- JEditorPane(URL initial page): This constructor will create the JEditorPane based on the specified URL in the input parameter.
- JEditorPane( String type, String text ): This constructor will create a JEditorPane that has been initialized to the text given within the parameter.
Some Useful Methods of JEditoPane Class
Following are the method follows:
- void setText(String text): This method will set the text of the component with the specified text given in the input, which is expected to be in the same content type as of the editor.
- Void getText( ): This method will return the text of the component within the specified content type of the editor.
- Void setPage(URL page): This method will trigger the JEditorPane to show the specified URL as the current page.
- Void setContentType(String type): This method is used to set the type of content that the editor can handle.
- Void addHyperlinkListener (HyperlinkListener listener): This method will add a hyperlink listener to the component which will help to notify whenever a link or hyperlink is being clicked or selected.
Example of JEditorPane Class
Below are the examples of JEditorPane:
Here in this example, we will create a web page reader using JEditorPane in java. We can’t also consider it as a web browser since JEditorPane can only use to show HTML content and it cant show any CSS or any other styling content but still some webpages with there HTML content can be accessed via the example as well as we also open any HTML file which saved on the local PC.
Over here in order to build a web page viewer, we will first create an editor pane to show the HTML content then create a JTextfield which will be used to fill the URL and a JButton which is used to search the URL on the web. Add an action to the button and hyperlink listener which can be used for any hyperlink on the HTML page. In the end, add all the components to the panel and the panel to the frameset the size of the frame and also make the webpage as read-only so that no changes can be made using the setEditable method as False.
Code:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import java.net.MalformedURLException;// Thrown when a URL doesn't contain http://
import java.net.URL;
import javax.swing.JButton;
import javax.swing.event.HyperlinkEvent;// Provides information on events triggered
import javax.swing.event.HyperlinkListener;// Monitors user activity with links
public class JEditorPaneExample extends JFrame implements HyperlinkListener, ActionListener {
public static void main(String[] args) {
new JEditorPaneExample("http://www.google.com");
}
String defaultURL;
JPanel panel = new JPanel();
JTextField theURL = new JTextField(25);
JButton search = new JButton("Search");
JEditorPane htmlPage;
public JEditorPaneExample(String defaultURL) {
JFrame frame = new JFrame("Java Browser");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.defaultURL = defaultURL;
// If the user interacts with the JButton then actionPerformed method is called
search.addActionListener(this);
// Set the default text in the JTextField
theURL.setText(defaultURL);
// Add the text field to a panel
panel.add(theURL);
panel.add(search);
// Add the panel to the northern quadrant of a frame
frame.add(panel, BorderLayout.NORTH);
try {
htmlPage = new JEditorPane(defaultURL);
// If the user interacts with the JEditorPane actions are triggered.
htmlPage.addHyperlinkListener(this);
// Display webpage in read-only mode
htmlPage.setEditable(false);
JScrollPane scroller = new JScrollPane(htmlPage);
// Add Scroll pane and JEditorPane to the frame
frame.add(scroller, BorderLayout.CENTER);
}
// If something goes wrong with locating the html page this will handle that error
catch (IOException e) {
e.printStackTrace();
}
frame.setSize(1200, 800);
frame.setVisible(true);
}
public void hyperlinkUpdate(HyperlinkEvent e) {
// Checks if the link was clicked
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
// Set the URL to be displayed
htmlPage.setPage(e.getURL());
// toExternalForm creates a String representation of the URL
theURL.setText(e.getURL().toExternalForm());
}
catch (IOException e1) {
e1.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e) {
String pageURL = "";
if (e.getSource() == search) {
pageURL = theURL.getText();
}
else {
pageURL = defaultURL;
// Opens an alert box when there is an error
JOptionPane.showMessageDialog(JEditorPaneExample.this,
"Please Enter a Web Address", "Error",
JOptionPane.ERROR_MESSAGE);
}
try {
// Sets the URL to be displayed
htmlPage.setPage(new URL(pageURL));
theURL.setText(pageURL);
} catch (MalformedURLException e2) {
JOptionPane.showMessageDialog(JEditorPaneExample.this,
"Please use http://", "Error",
JOptionPane.ERROR_MESSAGE);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
Output:
Conclusion
JEditorPane class can be used to display normal HTML, Rich Text Format Content or Plain text with a bit of styling. The JEditorPane class provides an edge over JTextPanes for providing text component as the JEditorPane class provides you constructors to initialize the editor pane form a URL whereas JTextPane doesn’t have such contractors.
Recommended Articles
This is a guide to JEditorPane. Here we discuss constructors, methods, and examples in JEditorPane. You can also go through our other related articles to learn more –