Rendering HTML in your Swing Applications

I recently wrote a news reader application (RSS only at the moment) that displays the messages using HTML. In doing so, I discovered the terrible state of the javax.swing.text.html.HTMLEditorKit. From the Javadoc:

The default support is provided by this class, which supports HTML version 3.2 (with some extensions), and is migrating toward version 4.0.


Migrating toward 4.0? I’m afraid we are already way past 4.0 at this point folks. No, we’re in XHTML 1.0 territory and moving ever-so-swiftly toward XHTML 1.1. There are plenty of free pure Java renderers out there, but most of them are not great. There are also some non-free ones, which I did not really consider, but may be fantastic. As a temporary solution I chose to use the JDIC (Java Desktop Integration Components) org.jdesktop.jdic.browser.WebBrowser class to display the HTML. I say “temporary solution” because JDIC is not pure Java, but I want my app to be pure Java. You can get JDIC components here. To use the components, just include the path of the binaries and JAR files in your classpath. After that is very easy to use the WebBrowser component. Here is a simple example.

public class SimpleBrowser extends JPanel { // Create a static html string static String HTML_CONTENT = “<html>\n” + “<head>\n” + “<style>\n” + “h1 {\n” + “font-family: \”Tahoma\”;\n” + “color: \”red\”;\n” + “}\n” + “</style>\n” + “</head>\n” + “<body>\n” + “<h1>Hello World!</h1>\n” + “<p>” + “Lorem ipsum dolor sit amet, ” + “consectetur adipisicing elit, sed ” + “do eiusmod tempor incididunt ut labore ” + “et dolore magna aliqua. Ut enim ad minim ” + “veniam, quis nostrud exercitation ullamco ” + “laboris nisi ut aliquip ex ea commodo consequat. ” + “Duis aute irure dolor in reprehenderit in voluptate ” + “velit esse cillum dolore eu fugiat nulla pariatur. ” + “</p>” + “</body>\n” + “</html>”;   public static void main( String args[] ) { JFrame frame = new JFrame(“WebBrowser Demo”); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final WebBrowser browser = new WebBrowser();   /* Create a temporary html file from the * content created earlier */   // Get a file using my getFile() method File tempFile = getFile( “temp.html” );   // Get a Formatter using my getFormatter() method Formatter output = getFormatter( tempFile );   // write to the file and close output.format( “%s”, HTML_CONTENT ); output.close();   try { // direct the browser to the URL of the file browser.setURL( tempFile.toURL() ); } catch (MalformedURLException e ) { e.printStackTrace(); }   frame.add( browser ); frame.setSize( 400, 400 ); frame.setVisible( true ); } } }

Basically you can just create an html file, get its URL, and then call the WebBrowser setURL method on it. Alternately you can direct the browser to an external URL like Google. So this code …

try { URL google = new URL( “http://www.google.com” ); browser.setURL( google ); } catch ( MalformedURLException e ) { e.printStackTrace(); }

… will take you to Google.com.

The content of my getFormatter() and getFile() methods are as follows:

static File getFile( String fileName ) { File f = new File( “” ); String filePath = f.getAbsolutePath() + fileName; File file = new File( filePath );   try { if ( !file.exists() ) { file.createNewFile(); } } catch ( IOException e ) { e.printStackTrace(); } return file; }   static Formatter getFormatter( File file ) { Formatter output = null;   try { output = new Formatter( file ); } catch ( FileNotFoundException e ) { e.printStackTrace(); }   return output; }
Scroll to Top