0

I've been blasting through the Java Swing tutorials on zetcode.com

I've made it all the way to Basic Swing Components II, the JTextPane component. In this example, an HTML document is loaded into memory and placed into the textpane. In order to find the file, zetcode.com uses:

String cd = System.getProperty("user.dir") + "/";
textPane.setPage("File:///" + cd + "test.html");

My IDE of choice is Eclipse Kepler.

I have written the code for this example's class and created the HTML document exactly as zetcode.com has shown on the page. I have placed the HTML file in the same source folder and package as the class which uses it. But when I run the code, I hear a Windows system error sound and the JFrame pops up without any text inside the textpane.

EDIT 01: I've named the package "com/zetcode/swingtutorial/basiccomponents/". I've tried using getClass.getResource("/com/zetcode/swingtutorial/basiccomponents/test.html") and I figure I must have typed this correctly because I do not get an IOException.

EDIT 02: Here's another interesting thing: In zetcode's system, they've used "File:///", which caused Windows to play an error sound. But when I tried "File://", no error sound plays. D'you think that was just a typo on their part? Either way, my html doc still isn't displayed on the pane. :S

Do you know what I could be doing wrong?

Many thanks for your help!

  • Relevant: [Java “user.dir” property - what exactly it means?](http://stackoverflow.com/questions/16239130/java-user-dir-property-what-exactly-it-means/16239152#16239152) – martinez314 Aug 08 '14 at 13:45
  • *"In order to find the file, zetcode.com uses: `String cd = System.getProperty("user.dir")`"* The `user.dir` is a fragile way of going about locating a `File`. – Andrew Thompson Aug 08 '14 at 14:13
  • @AndrewThompson Would you say that getClass.getResource() is a better way to go about doing this? – SutefuChan Aug 08 '14 at 14:23
  • @CindyStarfall I would use `getResource` for application resources (resources that you supply with the application). – Andrew Thompson Aug 08 '14 at 14:25

3 Answers3

2

Try this:

textPane.setPage(YourClass.class.getResource("test.html"));

If its in package

textPane.setPage(YourClass.class.getResource("/packagename/test.html"));
  • Using this system, I get pretty much the same result - an empty textpane. I know the URL I typed must be correct - because if I intentionally type it wrong, I see an exception occur. – SutefuChan Aug 08 '14 at 13:52
2

System.getProperty("user.dir");

Gives you the root context location or the project location folder or you can say the current directory when JVM was started Accordingly give the path or alternately Try loading this way

String pathOfHtmlFile = Thread.currentThread().getContextClassLoader()
                              .getResource("yourHtmlFile").getPath();
textPane.setPage(pathOfHtmlFile);

provided the file is in the classpath.

SparkOn
  • 8,806
  • 4
  • 29
  • 34
0
System.getProperty("user.dir")

Returns the "working folder" from which the application was launched. In Eclipse I believe the working directory is the top level project folder (not in src) or it's in the folder that contains the compiled .class files. Try copying the file to those folders and see when it works.

Xabster
  • 3,710
  • 15
  • 21
  • Looking through the folder directory, I notice that Eclipse has kept a copy of test.html in the matching binaries folder for me anyway. – SutefuChan Aug 08 '14 at 13:49