Browser Mark One

The class javax.swing.JEditorPane allows you to display a "page". Its default use is to render HTML, i.e., to display WWW pages. So its simplest use is:

   JEditorPane tJEditorPane = new JEditorPane();
   tContentPane.add(tJEditorPane, BorderLayout.CENTER);
where tContentPane is a pointer to the content pane of some JFrame object. When this JFrame is made visible, the CENTER will display the specified WWW page. Using these statements, you have the means to create a simple browser. You will find statements like these in the file

Of course, one of the key aspects of a browser is that, when you click on a link of a WWW page, the browser displays the WWW page pointed to by the link. This can be achieved by creating an object that acts as a listener for clicks on links:

where the variable tHTMLListener points to an object that is of a class that implements the HyperlinkListener interface. This statement also appears in the file

So, suppose that tHTMLListener is of the class HTMLListener. Then this needs to be a class that has the following heading:

   public class HTMLListener implements HyperlinkListener
If you look at the WWW page for javax.swing.event.HyperlinkListener, you will see that this interface requires us to implement one method:
   public void hyperlinkUpdate(HyperlinkEvent pHyperlinkEvent)

There are three kinds of "hyperlink events" that will cause this method to be executed. If the user of your browser moves the mouse so that it is on top a link, an ENTERED event has occurred, and he/she moves the mouse away from the link an EXITED event occurs, and if the mouse is clicked whilst on top of a link an ACTIVATED event occurs. The code of hyperlinkUpdate can distinguish between these three kind of events by looking at the value of the pHyperlinkEvent object that is passed at the call of hyperlinkUpdate. When an ENTERED event occurs, you may want to change some "status" textfield to contain the URL of the link over which the mouse is currently hovering; when an EXITED event occurs, you may want to change the status textfield to the empty string; and when an ACTIVATED event occurs, you will want to change the WWW page being displayed by the browser. In skeleton form, the latter can be done by putting the following into the declaration of hyperlinkUpdate:

   EventType tEventType = pHyperlinkEvent.getEventType();
   if (tEventType==EventType.ACTIVATED)
      tJEditorPane.setPage("" + pHyperlinkEvent.getURL());
It's not as easy as that, as tJEditorPane is not in scope. The full code is given in

Another key aspect of any browser is that the user can choose to display some other WWW page by typing in a URL. We can provide the browser with a JTextField object for this:

   JTextField tURLJTextField = new JTextField();
Now, we want to execute some code whenever the user types something into this textfield. We can do this by setting up an object that acts as a listener for any ActionEvents on this JTextField:
   URLJTextFieldListener tURLJTextFieldListener =
			          new URLJTextFieldListener();
The above statements appear in the file

The class URLJTextFieldListener has to implement the ActionListener interface. So, it has to have an actionPerformed method. In this actionPerformed method, we want to change the page being displayed by the browser to that page whose URL is given in the tURLJTextField. So essentially the body of actionPerformed wants to do:

Once again, it's not as easy as that, as neither tJEditorPane nor tURLJTextField are in scope: the full code is given in the file

In the above description, there have been several calls of setPage to change the WWW page being displayed. Of course, the String passed to setPage is meant to be that of a URL, but the String may be incorrect. The setPage method generates an IOException in these circumstances. Instead of having each setPage accompanied by its own try statement, the code delegates the changing of the WWW page to a method of a class called PagesHandler. The method is also called setPage. For more details, see the file

The program also contains an ExitOnWindowClosing class that has the same code, and is used in the same way, as the ExitWindowListener class that appears in the book. See the file

Click here for details about the next version of this WWW browser.