Java Video Tutorial 39

Java JEditorPaneIn this part of the Java Video Tutorial, I cover the JEditorPane Swing component. It allows you to both display rich text and HTML documents.

I also cover how to listen for events in JEditorPane, when users click on links for example. These events are handled by HyperlinkEvent and HyperlinkListener.

Because some people are having trouble with Eclipse, I also show how you can get Eclipse to do some coding for you.

If you like videos like this, tell Google

Always feel free to share

It helps people find my tutorials.

Code from the Video

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

// Thrown when a URL doesn't contain http://
// and other rules like that


// A text component that allows for rich text
// and basic html display

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

// Provides information on events triggered
// through interaction with links

import javax.swing.event.HyperlinkEvent;

// Monitors user activity with links

import javax.swing.event.HyperlinkListener;

public class Lesson39 extends JFrame implements HyperlinkListener, ActionListener{

	public static void main(String[] args){
		new Lesson39("file:///Volumes/My%20Book/Presentations/HTML%20Tutorial/htmlexample.html");
	String defaultURL;
	JPanel toolPanel = new JPanel();
	JTextField theURL = new JTextField(25);
	// Displays basic html pages
	// Doesn't understand JavaScript
	JEditorPane htmlPage;
	public Lesson39(String defaultURL){
		JFrame frame = new JFrame("Java Browser");
	    this.defaultURL = defaultURL;
	    // If the user interacts with the JTextField the
	    // actionPerformed method is called
		// Set default text in the JTextField
		// Add the text field to a panel
		// Add the panel to the northern quadrant of a frame
		frame.add(toolPanel, BorderLayout.NORTH);
		try {
			htmlPage = new JEditorPane(defaultURL);
			// If the user interacts with the JEditorPane
			// actions are triggered. Ex. Click on a link
			// change the JEditorPane page location
			// You can leave this true for rich text documents
			// but it will mess up web page display
			// Add the JEditorPane to a Scroll pane
			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) {
		// Set size of the frame and display it
		frame.setSize(1200, 800);

	public void hyperlinkUpdate(HyperlinkEvent e) {
		// Checks if a link was clicked
		// EventType.ENTERED : Checks for hovering
		// EventType.EXITED : Checks for leaving link
		if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED){
			try {
				// Sets the URL to be displayed
				// getURL gets the URL for the link
				// toExternalForm creates a String representation of the URL
			catch(IOException e1){

	public void actionPerformed(ActionEvent e) {
		String pageURL = "";
		// Gets the Object that had an event triggered
		if(e.getSource() == theURL){
			// Get the text in the JTextField
			pageURL = theURL.getText();
		} else {
			pageURL = defaultURL;
			// Opens an alert box when an error is made
					"Please Enter a Web Address", "Error", 
			// Sets the URL to be displayed
			htmlPage.setPage(new URL(pageURL));
		catch(MalformedURLException e2){
					"Please use http://", "Error", 
		catch(IOException e1){

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
<html xmlns="">
<meta name="generator" content="HTML Tidy for Linux (vers 6 November 2007), see" />
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" />
<style type="text/css">

h1 {color:red}

<meta name="description" content="Welcome" />
<meta name="keywords" content="Flowers," />
<meta name="author" content="Bob" />
<title>Welcome to Bob's Hardware Store, we sell Flowers, Seeds, Rakes</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
 span.c2 {text-decoration: line-through}
 div.c1 {text-align: center}
<h1>Text to Draw Attention to</h1>
<abbr title="A">Something Abbreviated</abbr> <acronym title="as">ASAP</acronym> <strong>I Want this to be Bold</strong> <big>This is going to be Big</big>
<blockquote>Indent me and show I'm important</blockquote>
<div class="c1">Center Me Please</div>
<em>I like Being Italicized</em> <em>I like being Italicized too</em>
<p>This is a big paragraph. Really, it is!</p>
<q>You can quote me on that</q> <span class="c2">I've been struck</span> <small>I feel so small</small> <strong>I'm the strongest</strong> <sub>I'm Subscript</sub> <sup>I'm Superscript</sup> <br /><img src="file:///Volumes/My%20Book/Presentations/HTML%20Tutorial/Frame4.jpg" alt="Photo" height="150" width="200" />
<dd>- Has two Wheels</dd>
<dd>- Has Four Wheels</dd>
<a href="" target="_blank">The New Think Tank Website</a> <a href="">Some Great Stuff</a> <a href="">Send Mail</a>
<table summary="Here">
<caption>Here are some things I did</caption>
<td>Played Ball</td>
<td>Watched Football</td>
<form action="mail2.php" method="post"><strong>Send Message to this Email</strong><br />
<input type="text" name="email" size="40" /><br />
<p><strong>Subject</strong><br />
<input type="text" name="subject" size="40" /><br /></p>
<p><strong>Text Area</strong><br />
<textarea cols="40" rows="10" name="message">
Default Text in Text Area Box
<br /></p>
<p><strong>Text Input</strong><br />
<input type="text" name="textinput" /><br /></p>
<p><strong>Password Input</strong><br />
<input type="password" name="password" /><br /></p>
<p><strong>Radio Input</strong><br />
<input type="radio" name="radioinput" value="Love Radio Buttons" />Love Radio Buttons <input type="radio" name="radioinput" value="Hate Radio Buttons" />Hate Radio Buttons<br /></p>
<p><strong>Checkbox Input</strong><br />
<input type="checkbox" name="checkboxinput" value="Love Checkboxs" />Love Checkboxs <input type="checkbox" name="checkboxinput" value="Hate Checkboxs" />Hate Checkboxs<br /></p>
<p><strong>Select Input</strong><br />
<select name="selectinput">
<option value="loveselect">I love select buttons</option>
<option value="hateselect">I hate select buttons</option>
</select><br /></p>
<p><strong>Option Input</strong><br />
<select name="optioninput">
<option value="loveselect">I love option buttons</option>
<option value="hateselect">I hate option buttons</option>
</select><br /></p>
<p><input type="submit" value="Send" /> <input type="hidden" name="submitted" value="TRUE" /></p>

16 Responses to “Java Video Tutorial 39”

  1. Punit says:

    keep going darek….

  2. mr.roshik says:

    THis is a Nice as usual , bro why not u sometime post some other tutorial beside java. because we want want to develop our skil on different topics for web design. but i love ur tutorials. THanks for sharing.

    • admin says:

      I’m always open to ideas. I used to get a ton of requests in the past for tutorials, but lately all I get are:

      1. Do a tutorial on After Effects (Cost $999)
      2. Do a tutorial on “Beta programming language”
      3. Do a tutorial on “Software Nobody Uses”

      I try to make tutorials that don’t require users to spend a bunch of money on software. I don’t make much money on my site and can’t afford the software either.

      I also don’t like to do tutorials on subjects that are brand new. The reason why is because they are normally flawed and then go away or change dramatically.

      I normally try to cover topics like Java completely. The reason why is because it is hard to drop a tutorial this big for a few weeks and then go back to it. I forget what I covered and try to build on each part of the tutorial.

      I do on occasion stop and cover an off topic subject. Examples of recent tutorial requests I fulfilled:

      1. Do a new tutorial on personalities
      2. Do a tutorial on sabermetrics
      3. How do you make a pizza without a bread machine
      4. Do a tutorial on Minecraft mods
      5. How do you make a WordPress site https compliant

      Tell me what topics you’d like to see and I’ll do my best to cover them.

      Thanks for the help

  3. mr.roshik says:

    THanks , im glad cos i have u, im waiting for the next tutorial 😀

  4. Martinho Arantes says:

    I like your tutorials, very very much. You explain very well each line of code. I started following you a while ago. Don´t have much time, as far as i can i´ll try to understand and follow every JAVA tutorial.

    Note: You are very fast writting tutorials?

    • admin says:

      Thank you. I do my best to cover just about everything. I try to get a tutorial out with no more than a 2 day lag. I should have a new one up today

  5. Martinho Arantes says:

    About the previous topics about what type of tutorials, i´d like to see some IOS programming. I was thinking in switch to a MAC, and i´d love to know how to make an app for IPHONE or IPAD.

    But now i´m focused on JAVA.
    Keep it up.

    Very good job

    • admin says:

      I definitely plan on covering iPhone development. It’s surprisingly easy. Most of the real work is illustration and interface work. I’m going to cover Android first because there is a great deal less competition in the Android world. Since Android is Java, it will be easy for you to learn

  6. Mateusz says:

    Hi, hate to bother you on such
    trivial matter, but is there
    a way to copy your code without
    those line numbers? I usually
    try typing it on my own while
    watching your tutorials, but
    sometimes I misplace something
    and can’t find it.

    Anyway, thanks for good work.

    • Derek Banas says:

      Thank you 🙂 Yes you can copy the code by drifting your mouse over the upper right hand corner of the code panels and then click View Source to eliminate line numbers. Sorry about the confusion

Leave a Reply

Your email address will not be published.