1

I am writing a Java code which also consists of HTML. I want to preview that HTML code in JFrame only. Is there any solution available within the Java API?

Could you please guide me to solve this problem?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
mani
  • 1,012
  • 7
  • 26
  • 47

3 Answers3

4

Yes. The JEditorPane can show (simple) HTML. See this JEditorPane tutorial for more details.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Thomas Uhrig
  • 30,811
  • 12
  • 60
  • 80
  • Is the JEditorPane can shows the background color of HTML body.?? – mani May 02 '12 at 11:32
  • Yes, but I think it depends on how you doing it. If you use a real simple tag like "" it should work (it's working for me ;). But you could have some problems with CSS or if the code gets to complicated. Just test it. Try your HTML file also in a real browser to see whether it's realy working. – Thomas Uhrig May 02 '12 at 11:47
  • *"Try your HTML file also in a real browser to see whether it's realy working."* Given that browsers go to extreme lengths to figure out what the (often invalid, commonly malformed) HTML is actually supposed to mean, I would swap 'browser' in that statement for '[validation service](http://validator.w3.org/)'. – Andrew Thompson May 02 '12 at 12:57
2

JEditorPane supports a sub-set of the HTML 3.2 elements as well as simple styles (CSS). While this task could be achieved using CSS, the HTML 3.2 body element also accepts a bgcolor.

Web page with background color

import java.awt.Dimension;
import javax.swing.*;

class WebPageWithBackground {
    WebPageWithBackground() {
        String html = "<html><body bgcolor='#ffbb99'>Hi!";
        JEditorPane jep = new JEditorPane();
        jep.setContentType("text/html");
        jep.setText(html);
        jep.setPreferredSize(new Dimension(400,50));
        JOptionPane.showMessageDialog(null, jep);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new WebPageWithBackground();
            }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
2

Actually, almost every Swing component that can display custom user text somewhere allows HTML instead of the text. For e.g. JButton text, JTabbedPane tab title, JTable cell/header, JLabel text, JList/JTree/JComboBox cell renderers, component tooltips and others.

To use HTML inside any of those - just add <html> tag at the very start of the text - that will be enough to let the view parser know that you have some HTML inside.

Also HTML that is supported in Java is pretty old and most of new features won't work as you could see them in modern browsers or visual editors. But its still more than enough to create some advanced components content where you need.

And one more thing - if you want to display some local images inside of the HTML you will have to change component's base location so it can properly load them:

component.putClientProperty (
    BasicHTML.documentBaseKey, 
    new File ( "/local/path/" ).toURI ().toURL () 
);

Some proper URL should be under that key (either local or remote).

Mikle Garin
  • 10,083
  • 37
  • 59
  • can we show – gumuruh Dec 26 '20 at 20:04
  • You can't, at least not in Swing. Swing only supports HTML 3.2 which is very outdated by today's standards, video tag came with HTML 5. – Mikle Garin Dec 27 '20 at 02:04
  • ow... if that's the case. We should move on to JavaFX am i right? – gumuruh Dec 27 '20 at 02:15
  • Depends on what you need. Swing still has much better performance compared to FX, lots of 3rd-party libraries with various components and features and it is still supported by Oracle. Swing is also included in standard JDK package, FX is not anymore. FX on the other hand has some great features that are harder to implement in Swing (although not impossible), like proper web browser implementation, video player, some advanced graphical stuff out-of-the-box etc. – Mikle Garin Dec 27 '20 at 12:39