1. Introduction to JEditorPane
The JEditorPane extends from Java Swing’s JTextComponent. So it supports various document types. One can use it to display Plain document (txt), Rich Text Document (rtf) and html document. In this example, we will create a JEditorPane and load a html document into it.
2. About The Example
Below screenshot shows the JEditorPane example we create here:

The example is a simple JFrame, which houses a JEditorPane in it. The editor pane is displaying a html table above and the user can scroll the document using the scrollbars. These scrolls are useful when the document is bigger than the editor pane. Now, we will proceed with the example.
3. Html Document for the Example
In this example, we are going to load a html document in the Editor Pane. The html document is a table with heading text. We no need to go into much detail about this html content. Name this a Test.html and place it along with the java files (Found in Code Reference Section).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Untitled Page</title> </head> <body> <H1 style="font-family: Verdana"> <span style="color: red">This is a sample Html Page</span></H1> <p> <table border="1" cellpadding="4" cellspacing="4" style="width: 430px; height: 265px"> <tr> <td style="width: 100px; background-color: seashell"> <span style="font-size: 14pt; font-family: Tahoma"> Cell 1</span></td> <td style="width: 100px; background-color: seashell"> <span style="font-size: 14pt; font-family: Tahoma"> Cell 2</span></td> <td style="width: 100px; background-color: seashell"> <span style="font-size: 14pt; font-family: Tahoma"> Cell 3</span></td> </tr> <tr> <td style="width: 100px; background-color: seashell"> <span style="font-size: 14pt; font-family: Tahoma"> Cell 4</span></td> <td style="width: 100px; background-color: seashell"> <span style="font-size: 14pt; font-family: Tahoma"> Cell 5</span></td> <td style="width: 100px; background-color: seashell"> <span style="font-size: 14pt; font-family: Tahoma"> Cell 6</span></td> </tr> <tr> <td style="width: 100px; background-color: seashell"> <span style="font-size: 14pt; font-family: Tahoma"> Cell 7</span></td> <td style="width: 100px; background-color: seashell"> <span style="font-size: 14pt; font-family: Tahoma"> Cell 8</span></td> <td style="width: 100px; background-color: seashell"> <span style="font-size: 14pt; font-family: Tahoma"> Cell 9</span></td> </tr> </table> <span style="color: red"></span> </p> </body> </html> |
4. Create JEditorPane
In the below code snippet, at line 7, we create a
JEditorPane via an empty constructor. By default, Java Swing keeps editor pane in the disabled state. So we make a call to setEnable
to override the default state.
1 2 3 4 5 6 7 8 |
//Sample 01: Set Size and Position setBounds(100, 100, 600, 500); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 02: Create Editor Pane JEditorPane jep = new JEditorPane(); jep.setEnabled(true); |
5. Scrolling Support
When the html document is big, the JEditorPane will not show the entire html content in it. We need to provide scrolling support for the editor pane. At line 2, we create a JScrollPane
instance and set a preferred size for it. This helps in providing more container space (JFrame) for other controls. The call, setVerticalScrollBarPolicy
tells we always need the vertical scrollbar. So even if the html document is small, Java Swing will display a vertical scrollbar without scroll thumb.
1 2 3 4 5 6 7 |
//Sample 03: Create Scroll Pane for the Editor JScrollPane scroll = new JScrollPane(jep); Dimension ScrollSize = new Dimension(570, 450); scroll.setPreferredSize(ScrollSize); scroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); ControlHost.add(scroll); |
6. JEditorPane – Set Html Document
At line 3, we make a call to
getResource method to load out html document. The
URL reference refers this html document and we name it HtmlPage
. Next, at line 6, the
setPage method of the JEditorPane takes this html document as a parameter and displays it as the pane content. Since it is an IO action, we must handle the IOException
. Note, the Scrolling support provided in the past section enables scrolling of the html page inside the editor pane.
1 2 3 4 5 6 7 8 9 10 11 |
//Sample 04: Get URL of the HTML //Run 02 Check how JScrollPane Displays URL HtmlPage = JEditorPaneExample.class.getResource("Test.html"); try { jep.setPage(HtmlPage); } catch (IOException e) { e.printStackTrace(); } |
7. Youtube Demo
8. Code Reference
8.1 MainEntry.java
1 2 3 4 5 6 7 8 9 |
package tube.coding.examples; public class MainEntry { public static void main(String[] args) { //Sample 07: Create Instance of JFrameDemo JEditorPaneExample frame = new JEditorPaneExample("JEditorPane and Html Example"); frame.setVisible(true); } } |
8.2 JEditorPaneExample.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
package tube.coding.examples; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.HeadlessException; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.io.IOException; import java.net.URL; import javax.swing.ButtonGroup; import javax.swing.JCheckBox; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JTextField; public class JEditorPaneExample extends JFrame { public JEditorPaneExample(String title) throws HeadlessException { super(title); //Sample 01: Set Size and Position setBounds(100, 100, 600, 500); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 02: Create Editor Pane JEditorPane jep = new JEditorPane(); jep.setEnabled(true); //Sample 03: Create Scroll Pane for the Editor //Run 01 Check how JScrollPane Displays //Run 03 Change 500 to 100 & check Scrolling JScrollPane scroll = new JScrollPane(jep); Dimension ScrollSize = new Dimension(570, 450); scroll.setPreferredSize(ScrollSize); scroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); ControlHost.add(scroll); //Sample 04: Get URL of the HTML //Run 02 Check how JScrollPane Displays URL HtmlPage = JEditorPaneExample.class.getResource("Test.html"); try { jep.setPage(HtmlPage); } catch (IOException e) { e.printStackTrace(); } } } |
Categories: Swing
Tags: getResource, JEditorPane, JScrollPane, setPage, URL