0

I have a tree list which will open a specific html file when I click at a node. I try loading my html into a Jeditorpanel but it can't seem to work.

Here's my code from main file:

private void treeItemMouseClicked(java.awt.event.MouseEvent evt) {                                      
    DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) treeItem.getSelectionPath().getLastPathComponent();
    String checkLeaf = selectedNode.getUserObject().toString();
    if (checkLeaf == "Java Turtorial 1") {
        String htmlURL = "/htmlFILE/javaTurtorial1.html";
        new displayHTML(htmlURL).setVisible(true);
    }
}

Where I wanna display it:

public displayHTML(String htmlURL) {
    initComponents();
    try {
        //Display html file
        editorHTML.setPage(htmlURL);
    } catch (IOException ex) {
        Logger.getLogger(displayHTML.class.getName()).log(Level.SEVERE, null, ex);
    }
}

My files:

enter image description here

  • What do you mean by this line: `new displayHTML(htmlURL).setVisible(true);`? What are you trying to create (`new`) here? Can you please add the other relevant code? That would be helpful. – Azeem Nov 21 '19 at 05:07
  • It's mean that I will pass String htmlURL into displayHTML.java and run it which was already posted above. htmlURL then will be used as an address for my jEditorPanel to load. –  Nov 21 '19 at 05:14

1 Answers1

0

One simple way to render HTML with JEditorPane is using it's setText method:

JEditorPane editorPane =...

editorPane.setContentType( "text/html" );    
editorPane.setText( "<html><body><h1>I'm an html to render</h1></body></html>" );

Note that only certain HTML pages (relatively simple ones) can be rendered with this JEditoPane, if you need something more complicated you'll have to use thirdparty components

Based on OP's comment, I'm adding an update to the answer:

Update

Since the HTMLs that you're trying to load are files inside the JAR, you should read the file into some string variable and use the aforementioned method setText

Note that you shouldn't use java.io.File because it used to identify resources at the filesystem, and you're trying to access something inside the artifact:

Reading the resource like this can be done with the following construction:

InputStream is = getClass().getResourceAsStream("/htmls/myhtml.html");
// and then read with the help of variety of ways, depending on Java Version of your choice and by the availaility by auxiliary thirdparties

// here is the most simple way IMO for Java 9+ 

String htmlString = new String(input.readAllBytes(), StandardCharsets.UTF_8);

Read Here about many different ways to read InputStream into String

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • But I am required to assign it to a html file so I have to use setPage. So I have somehow manage to display my html local file. I create an HTML file inside my project by Netbeans. Do you think it's understandable for JEditoPanel? –  Nov 21 '19 at 05:35
  • An HTML file is a file inside your JAR, so you must read it from disk into string (after all its just a text and therefor can be assigned to string variable) and then use the method I've mentioned in the answer initially – Mark Bramnik Nov 21 '19 at 05:37
  • Please see the update to the question that elaborates on the complementary question you've asked in the comment – Mark Bramnik Nov 21 '19 at 05:46