0

I want to show webpage in Java on Jframe using JeditorPane code I used:

 edPane.setContentType("text/html"); 
    String data="<html>\n" + "<head>\n" + "<script type=\"text/javascript\" src=\"http://maps.google.com/maps/api/js?sensor=false\"></script>\n" + 
    "<script type=\"text/javascript\">\n" + "  function initialize() \n" + "  {\n" + "              \n" + "    
var map = new google.maps.Map(document.getElementById(\"map_canvas\"), myOptions);\n" + "</script>\n" + "</head>\n" + "<body onload=\"initialize()\">\n" + 
"  <div id=\"map_canvas\" style=\"width:100%; height:100%\"></div>\n" + "</body>\n" + "</html>";         edPane.setText(data);

Ouutput: It is displaying nothing in UI.. I want to display the webpage... Help please

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Try looking at [this page][1] it has a very similar question. [1]: http://stackoverflow.com/questions/5176806/add-a-web-browser-in-my-java-application – sazzy4o Apr 22 '14 at 01:46
  • http://docs.oracle.com/javafx/2/swing/swing-fx-interoperability.htm All you need – Bogdan M. Apr 23 '14 at 10:16

6 Answers6

5

The problem is that JEditorPane has no Javascript or HTML5 (canvas) support, so you should use another container. There is a similar question here where the solution was moving to JavaFX.

Community
  • 1
  • 1
Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
2

Have a look at The DJ Project.

It is open source and released under LGPL.

The JWebBrowser also provides the functionality to execute javascript code. Sample code as follows:

private static final String LS = System.getProperty("line.separator");

JWebBrowser webBrowser = new JWebBrowser();
webBrowser.setBarsVisible(false);
webBrowser.setStatusBarVisible(true);
final String htmlContent =
  "<html>" + LS +
  "  <body>" + LS +
  "    <h1>Some header</h1>" + LS +
  "    <p>A paragraph with a <a href=\"http://www.google.com\">link</a>.</p>" + LS +
  "  </body>" + LS +
  "</html>";
webBrowser.setHTMLContent(htmlContent);

// [...] Add webBrowser to a container
webBrowser.executeJavascript("document.bgColor = '#FFFF00';");

Please see the project page for more samples.

trylimits
  • 2,575
  • 1
  • 22
  • 32
0

I would recommend embedding JavaFX WebView rather than using a native library like DJ Project. Have a look at my answer here. I also mention some problems I had with dj project, when it comes to platform independence.

Community
  • 1
  • 1
haferblues
  • 2,155
  • 4
  • 26
  • 39
0

By Using a JEditorPane to display a web page:

 import javax.swing.text.*;
    import javax.swing.*;
    import java.io.*;
    import java.awt.*;

public class GoogleHomePage {

  public static void main(String[] args) {

     JEditorPane jep = new JEditorPane();
     jep.setEditable(false);   

     try {
       jep.setPage("http://www.google.com");
     }
     catch (IOException e) {
       jep.setContentType("text/html");
       jep.setText("<html>Could not load http://www.google.com </html>");
     } 

     JScrollPane scrollPane = new JScrollPane(jep);     
     JFrame f = new JFrame("O'Reilly & Associates");
     // Next line requires Java 1.3
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     f.getContentPane().add(scrollPane);
     f.setSize(512, 342);
     f.show();

  }

}
Christopher
  • 15
  • 1
  • 5
testing
  • 1,736
  • 15
  • 46
  • 75
0

There is a very easy way to show pages stored in local disk in a JEditorPane on Windows

  1. create a website in IIS
  2. the website should have folders associated with the pages.
  3. put the web address into the setPage() function of JEditorPane

You are done.

The benefit of this approach is that you can keep your pages separate from application and protect them from write changes from the application.

Usually you cannot put relative address of webpage or an address without protocol into setPage() function. The above approach that I mentioned gives you both benefit of relative addresses and a protocol.

Enjoy.

-1

If I understand correctly, you want to have HTML show up in a JEditorPane. One method is to create a HTML page. You have 2 options:

  1. Put your HTML file on a clout like DropBox or Google Drive.
  2. Have it locally in a File.

        JEditorPane editorPane_WEB = new JEditorPane();
        editorPane_WEB.setEditable(false); //OPTIONAL
        editorPane_WEB.setBorder(null); //OPTIONAL
        try {
            editorPane_WEB.setPage("http://---------------.com/--.html"); //Or Path/File destination
        } catch (IOException error) {/*ERROR*/}   
    
        JScrollPane scrollPane_WEB = new JScrollPane(editorPane_WEB);
        scrollPane_WEB.setBounds(67, 27, 798, 465); // Don't use null layout.
        scrollPane_WEB.getVerticalScrollBar().setUnitIncrement(10);
    
        add(scrollPane_WEB); //class extends JFrame/JPanel
    

If this doesn't awnser you question, don't downvote, just put a comment below!

3751_Creator
  • 656
  • 2
  • 8
  • 22
  • OP's HTML code includes Javascript (google maps API) and requires a canvas, so HTML5 and a modern Javascript engine is needed to render it. JEditorPane has an old HTML viewer with no JS engine – Pablo Lozano Apr 22 '14 at 09:37
  • @PabloLozano yeah your right. Java should get a clean up of its existing code and make a bit more up to date. – 3751_Creator Apr 22 '14 at 15:50