<< Chapter < Page | Chapter >> Page > |
Open the URL object in a JEditorPane object
Listing 4 opens the URL object in a JEditorPane object to satisfy the second item in the above list .
(I will ignore the exception handling code while explaining this program.)
Listing 4 - Open the URL object in a JEditorPane object. |
---|
try{
if(website != null) {//Create a JEditorPane containing the web page.
JEditorPane html = new JEditorPane(website);html.setEditable(false); |
Fortunately, the online documentation for the JEditorPane is fairly detailed. I will refer you to that documentation for more information.
The JEditorPane constructor
Listing 4 calls an overloaded JEditorPane constructor that is described in the documentation asfollows:
"Creates a JEditorPane based on a specified URL for input."
( Note that there is another constructor that would allow us to create the JEditorPane object passing a URL string to the constructor. Had I used that constructor, it wouldn't have been necessary tocreate the URL object. For illustration, I will use that constructor in a later program in this module.)
Documentation snippets
There are a couple of snippets from that documentation that are particularly important to thismodule. The first snippet is:
"Some kinds of content may provide hyperlink support by generating hyperlink events. The HTML EditorKit will generate hyperlink events if the JEditorPane is not editable (JEditorPane.setEditable(false); has been called)."
Since we are definitely interested in hyperlink events, Listing 4 calls the setEditable method on the new JEditorPane objectpassing false as a parameter.
A second snippet that will be important later in this module reads:
"The setPage method can be used to initialize the component from a URL."
We will use this capability later to cause a hyperlink event handler to follow a link in a webpage when we click on the hyperlink.
Register a hyperlink listener
Listing 5 uses standard Java event handling code to register a hyperlink listener object on the JEditorPane object. As I mentioned earlier, this object is suitable for use as a listener object.
Listing 5 - Register a hyperlink listener. |
---|
html.addHyperlinkListener(this); |
I will explain the hyperlink listener code shortly.
Display the JEditorPane
Listing 6 uses standard Swing code to display the JEditorPane in a JFrame object. There is nothing new or interesting about this code so I won't discussit further.
Listing 6 - Display the JEditorPane. |
---|
this.getContentPane().add(html);
this.setSize(669,669);this.setVisible(true);
}//end if}catch(Exception e){
e.printStackTrace();}//end catch
}//end constructor |
Listing 6 also signals the end of the constructor for the class named Java4655cHtmlHandler .
The event handler method
Listing 7 shows the event handler method named hyperlinkUpdate in its entirety.
Listing 7 - The hyperlinkUpdate method. |
---|
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() ==HyperlinkEvent.EventType.ENTERED){System.out.println("ENTERED");
}else if (e.getEventType() ==HyperlinkEvent.EventType.EXITED){
System.out.println("EXITED");}else if (e.getEventType() ==
HyperlinkEvent.EventType.ACTIVATED){System.out.println("ACTIVATED");
}//end if}//end hyperlinkUpdate method
//------------------------------------------------------//}//end class Java4655cHtmlHandler |
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?