Learn to Code via Tutorials on Repl.it!

← Back to all posts
How to load HTML in a Java Swing Project
EpicGamer007 (1535)

How to load HTML in a Java Swing Project

Great news for you Java Swing developers. I recently found out about how to load HTML and CSS in a Java Swing Project using a JEditorPane and best of all, it is really simple!

Note: Input and JS is unsupported. Try a web browser instead for that.

So here is how it goes:

import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;

public class Main{

	public static void main(String[] args){

		JFrame frame = new JFrame("Test");
		frame.setSize(500, 500);

		JEditorPane editorPane = new JEditorPane();
		try {
			URL theURL = new File("res/index.html").toURI().toURL();
			if (theURL != null) {
			    try {
			    } catch (IOException e) {
			        System.err.println("Attempted to read a bad URL: " + theURL);
		} catch (MalformedURLException e1) {




So what is going on?

  1. We create a basic JFrame

  2. We create a JEditorPane

  3. We create a File then convert it to a URL

  4. We make the JEditorPane's page the url

  5. Add that JEditorPane to the frame

Optional Steps:

  • Create a ScrollBar and link that to the JEditorPane to make it fit the screen
  • Set the size of the JEditorPane

Hope you learned something :)

Note: Those who have been following my Java Swing Tutorial, I would just like to let you know that It might be a long time before the next tutorial because it will be the last tutorial. I didn't realize what I was doing was bad(i.e. cycle squezing) so I will stop doing that and create a LOOOONG one for the last one.


Ignore the hangman header, that is just the next project I am working on and I am testing it in there.
JustAWalrus (1181)

If I would like one tutorial series to overtake my prior monopoly on the tutorials board, it would be yours. They are short whilst helpful.

EpicGamer007 (1535)

@Wuru , I feel honored. Thank you

JustAWalrus (1181)

Maybe a collab is on the rise? @AbhayBhat

HahaYes (1901)

Good job, if anyone deserves my monopoly, its you.

Jakman (473)

This is a nice tutorial I learned about urls this time.

EpicGamer007 (1535)

@Jakman, I forgot to include this at the start but make sure to

import java.net.MalformedURLException;
import java.net.URL;
Highwayman (1459)

where does helpURL come from?

EpicGamer007 (1535)

@Highwayman, helpURL is just the name of the url, lol idk why not. I just copied it from one of my projects

Highwayman (1459)

@AbhayBhat but, uh... where is it defined?

EpicGamer007 (1535)

@Highwayman, oh yea shoot, i made a mistake, lemme change that lol

KuGaming2 (10)

man I did java class and it is such a hard language

EpicGamer007 (1535)

@KuGaming2, heh, this is why I believe java is the best first language to learn. It isn't so hard that you would want to quit, But it isn't as easy as some other languages which makes other languages seem hard.

k9chelsea2 (768)

oh kool, I don't use java but if i ever do this will be useful