Skip to content
Sign UpLog In
This post is read-only. Explore Repls and connect with other creators on Community.View Community
The info in this post might be out of date, check out our docs instead. View docs
14

How to load HTML in a Java Swing Project

EpicGamer007
EpicGamer007

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); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JEditorPane editorPane = new JEditorPane(); editorPane.setEditable(false); try { URL theURL = new File("res/index.html").toURI().toURL(); if (theURL != null) { try { editorPane.setPage(theURL); } catch (IOException e) { System.err.println("Attempted to read a bad URL: " + theURL); } } } catch (MalformedURLException e1) { e1.printStackTrace(); } frame.add(editorPane); frame.setLocationRelativeTo(null); frame.setVisible(true); } }

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.

Example:

image

Ignore the hangman header, that is just the next project I am working on and I am testing it in there.
2 years ago

Voters

Comments

TopNew
5
JustAWalrus
JustAWalrus

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.

2 years ago
1
EpicGamer007
EpicGamer007

@Wuru
, I feel honored. Thank you

2 years ago
2
JustAWalrus
JustAWalrus

Maybe a collab is on the rise?

@AbhayBhat

2 years ago
3
HahaYes
HahaYes

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

2 years ago
1
EpicGamer007
EpicGamer007

@HahaYes
, Thank you as well :)

2 years ago
3
Jakman
Jakman

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

2 years ago
1
EpicGamer007
EpicGamer007

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

import java.net.MalformedURLException; import java.net.URL;
2 years ago
2
Highwayman
Highwayman

where does helpURL come from?

2 years ago
1
EpicGamer007
EpicGamer007

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

2 years ago
1
Highwayman
Highwayman

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

2 years ago
1
EpicGamer007
EpicGamer007

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

2 years ago
1
L0STP0LAR
L0STP0LAR

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

2 years ago
1
EpicGamer007
EpicGamer007

@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.

2 years ago
1
k9chelsea2
k9chelsea2

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

2 years ago
Load more