user597608
user597608

Reputation: 387

Java HTML files and JEditor Pane

I am trying to figure out how to read an HTML file into a JEditorPane.

I have the following code:

JEditorPane editorPane = new JEditorPane();
URL helpURL = MainFrame.class.getResource("user-manual.html");

Now my problem is that the user-manual.html file needs to be in the bin folder that holds the MainFrame class otherwise it will not work. Now my problem is my directory has to look like this:

as1/
as1/doc/
as1/doc/user-manual.html
as1/doc/system-doc.html
as1/doc/images/
as1/src/

How do I go about reading the user-manual.html file into the URL?

Upvotes: 0

Views: 390

Answers (4)

John
John

Reputation: 2852

possible reasons -

  1. The html files might have been stored at wrong place.
  2. The setContent method might not have been set to "text/html"

Upvotes: 0

limc
limc

Reputation: 40168

How about this?

getClass().getClassLoader().getResource("as1/doc/user-manual.html");

Upvotes: 0

Paŭlo Ebermann
Paŭlo Ebermann

Reputation: 74750

The method Class.getResource accept also semi-relative adresses, like this:

 URL helpURL = MainFrame.class.getResource("/as1/doc/user-manual.html");

Then the path is rooted at the jar-file (or classpath directory) instead of the package directory.

Upvotes: 1

Mark Elliot
Mark Elliot

Reputation: 77044

You can either add the items directory to your classpath and continue access it as you are now or inside the directory structure as:

MainFrame.class.getResource("as1/doc/user-manual.html");

Upvotes: 0

Related Questions