<< Chapter < Page | Chapter >> Page > |
I will explain this program in fragments. A complete listing of the program is provided in Listing 11 .
Listing 1 shows the beginning of the program.
Listing 1 - Beginning of the program named Java4655c. |
---|
import javax.swing.*;
import javax.swing.event.*;import javax.swing.text.html.*;
import java.net.*;import java.awt.*;
class Java4655c{public static void main(String[]args){
new Java4655c().runner("http://www.austincc.edu/baldwin");
}//end main |
There is nothing new in Listing 1 . This code simply calls a method named runner passing a string description of a URL as a parameter.
The method named runner
The method named runner is shown in Listing 2 .
Listing 2 - The method named runner. |
---|
void runner(String webSiteLink){
try{//Create a new URL object from the website string
URL website = new URL(webSiteLink);//Instantiate an overall web page handler
new Java4655cHtmlHandler(website);}catch(Exception e){
e.printStackTrace();}//end catch
}//end runner}//end class Java4655c |
Listing 2 begins by getting a URL object that is connected to the specified webpage to satisfy the first item in the above list .
Then Listing 2 instantiates a new object of the class named Java4655cHtmlHandler passing the URL object's reference as a parameter to the constructor. From this point forward, the behavior of the program will becontrolled by the object of the Java4655cHtmlHandler class.
Listing 2 also signals the end of the class named Java4655c .
Beginning of the class named Java4655cHtmlHandler
The beginning of the class named Java4655cHtmlHandler and the beginning of the constructor for that class is shown in Listing 3
Listing 3 - Beginning of the class named Java4655cHtmlHandler. |
---|
class Java4655cHtmlHandler extends JFrame
implements HyperlinkListener{//Constructor
public Java4655cHtmlHandler(URL website) {setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Copyright 2014, R.G.Baldwin"); |
There is nothing new or unusual in Listing 3 with the possible exception of the fact that the class implements the interface named HyperlinkListener . This has two important ramifications:
As you will see later, the HyperlinkListener interface declares only one method and it is named hyperlinkUpdate . The method receives one incoming parameter of type HyperlinkEvent .
The hyperlinkUpdate method
The documentation for the hyperlinkUpdate method is rather sparse, stating only that the method is "Called when a hypertext link is updated" and that the incoming parameter represents "the event responsible for the update."
As you will see later, an event occurs whenever the user touches a hyperlink in the webpage with the mouse. The HyperlinkEvent object encapsulates information identifying the event as being one of the followingtypes:
I will pursue the three types of hyperlink events in more detail later.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?