Web browser in Swing

Swing HTML Document rendering engine supports HTML 3.2 and CSS, and is able to show most HTML pages similar to popular browsers. So, using Web pages and an embedded “browser” is a good solution for help systems and online documentation.

JEditorPane

JEditorPane is specially useful for this, because it can automatically load documents from HTTP sources. Remember to set the editable property of the pane to false after you create it. Also, the JEditorPane does not follow links automatically - you have to implement a hyperlink listener (there is an example in the JEditorPane javadoc page).

This is an example of JDialog containing a JEditorPane and a “close” button on the bottom (used in several live applications as a window for an on-line help system). It forwards exceptions to an exception listener, and automatically follows http links.

 
package com.neuri.common.gui;
 
import java.awt.*;
import java.awt.event.*;
import java.beans.ExceptionListener;
 
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.html.*;
public class WebBrowserClient  {
	  private JEditorPane jep;
	  private ExceptionListener listener;
	  private JDialog dialog;
	  public WebBrowserClient(ExceptionListener listener, Frame topFrame, String exitButtonCaption, Dimension d)
	  {
	    dialog=new JDialog(topFrame);
	    this.listener=listener;
	    dialog.setModal(true);
	    jep=new JEditorPane();
	    jep.setSize(d);
	    jep.setEditable(false);
	    
	    dialog.setSize(d);
	    dialog.getContentPane().setLayout(new BorderLayout());
	    dialog.getContentPane().add(new JScrollPane(jep),BorderLayout.CENTER);
	    JButton jb=new JButton(exitButtonCaption);
	    dialog.getContentPane().add(jb,BorderLayout.SOUTH);
	    jb.addActionListener(new HideListener());
	    jep.addHyperlinkListener(new Hyperactive());
	  }
	  public void showUrl(String url, String title)
	  {
	  try{
	  	java.net.URL helpUrl=null;
	  	if (url.startsWith("http://"))
	  		helpUrl=new java.net.URL(url);
	    else
	    	helpUrl=ClassLoader.getSystemClassLoader().getResource(url);
	    dialog.setTitle(title);
            //here is how to override the content type
	    //jep.setContentType("text/html;charset=Cp1250");
	    jep.setPage(helpUrl);
	    dialog.show();
	  }
	  catch (Exception e)
	  {
	  	if (listener!=null)
	  		listener.exceptionThrown(e);
	  	else{
	  		e.printStackTrace();
	  	}	    
	  }
	  }
	  private class HideListener implements ActionListener{
	  public void actionPerformed(ActionEvent e)
	  {
	    dialog.hide();	   
	  }	  
	  }
	  
    class Hyperactive implements HyperlinkListener {
       public void hyperlinkUpdate(HyperlinkEvent e) {
          if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
		      JEditorPane pane = (JEditorPane) e.getSource();
	      if (e instanceof HTMLFrameHyperlinkEvent) {
		          HTMLFrameHyperlinkEvent  evt = (HTMLFrameHyperlinkEvent)e;
		          HTMLDocument doc = (HTMLDocument)pane.getDocument();
		          doc.processHTMLFrameHyperlinkEvent(evt);
		      } else {
		          try {
		          		pane.setPage(e.getURL());
		          } catch (Exception t) {
		          		listener.exceptionThrown(t);
		          }
		      }
          }
	      }
    }
 
}

Rendering engines

Other projects

  • JDIC enables native OS-specific browser launching

(X)HTML/Java mixins

While one can write a client application fully in Java, this does not imply (X)HTML is useless in a Java context. There is a lot of possible (X)HTML/Java mixins and the following post Revisiting XHTML as a base (?) for XUL-like programming lists some possibilities. The situation is still evolving as, for example, Swing may be including in the future styling properties a la CSS (see propositions Fuse, UI Oriented Resource Injection or Style Swing components using CSS) and as markup programming languages, like JAXX, mix XML and Java/Swing approaches.

See also

 

Comments? Corrections? Contact us or Login to edit pages directly (registration is free and takes less than displaying a JLabel)
  howto/web_browser.txt · Last modified: 2006/10/11 06:16 by 145.242.3.30 (dmdevito)
 
Recent changes | RSS changes | Table of contents | News Archive | Terms And Conditions | Register