0

In a swing application I have a JFrame and by Dragging a JTextPane on that Jframe But when I try to show html content it does not seem to work properly.

Here is what I'm trying

JEditorPane ep = jp;    //jp is JTextPane.

        ep.setContentType("text/html");
//        ep.setText(text);
        URL u = null;
        try {
            u = new URL("http://www.google.com");
        } catch (MalformedURLException ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            ep.setPage(u);
        } catch (IOException ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

And here is its result Here is the result

Actually I first tried with my own html string code with its setText(); method that html was working in browser and the result in java was not satisfactory then I tried to open google page that is in front of you.

It seems that the the basic html is working but styles and scripts are not working. So please tell me to use it well.
Thanks

Inzimam Tariq IT
  • 6,548
  • 8
  • 42
  • 69
  • JEditorPanes and TextPanes are very limited for HTML and CSS. – DarkV1 Dec 29 '16 at 20:21
  • so how can we show things that may require scripts too? – Inzimam Tariq IT Dec 29 '16 at 20:22
  • Have u tried using JavaFX? – DarkV1 Dec 29 '16 at 20:27
  • 1
    or at least HTMLEditorKit? example http://alvinalexander.com/blog/post/jfc-swing/how-create-simple-swing-html-viewer-browser-java – Vladislav Kysliy Dec 29 '16 at 20:32
  • 3
    Swing support for HTML is limited to a **sub-set** of HTML **3.2** (the latest standard is HTML 5), a small amount of limited CSS, and no support at all for JavaScript. As mentioned by @DarkV1, Java-FX may offer a solution in the form of a [`WebView`](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/web/WebView.html) but if you decide to use it, I would advise making the entire project using Java-FX, rather than trying to embed a Java-FX component into a Swing GUI - which is a PITA if you want to interact with it. – Andrew Thompson Dec 29 '16 at 20:33
  • @AndrewThompson My application is almost ready so I think I can't afford reinventing it in Java-FX – Inzimam Tariq IT Dec 29 '16 at 20:37
  • ^^There are also some random libraries out there that would work...But I can't recommend any as I rarely work with HTML, CSS, or JS for my java code – DarkV1 Dec 29 '16 at 20:38
  • 3
    You think you can 'afford' to tie a Java-FX component into a (complex?) existing Swing GUI in less time? (insert maniacal cackling..) – Andrew Thompson Dec 29 '16 at 20:38
  • 2
    @AndrewThompson +2 for the emotion haha. But InzimamTariqIT, look into JavaFx because it's going to be your best bet. It also isn't that difficult to implement (compared to your search to get JEditorpane to work with HTML5, CSS, and JS)... Trust me. – DarkV1 Dec 29 '16 at 20:41
  • I have to display email that may have attachments so is there any way other than java-FX? It would be great to know if with swing I can show that material otherwise I would switch to Java-FX. – Inzimam Tariq IT Dec 29 '16 at 20:45
  • 1
    Add WebView control on Swing JFrame http://stackoverflow.com/questions/13487786/add-webview-control-on-swing-jframe – Vladislav Kysliy Dec 29 '16 at 20:49
  • Ok. What you are trying to do...I am 99.9999% sure is either VERY complicated with JEditorPane or BASICALLY IMPOSSIBLE. Java Fx is your #1 Best Bet @InzimamTariqIT – DarkV1 Dec 29 '16 at 20:59

0 Answers0