<< Chapter < Page Chapter >> Page >

This hyperlink event handler simply displays the type of event on the command-line screen as the mouse pointer toucheshyperlinks on the webpage. The possible types of events and the actions that cause them are as follows:

  • ENTERED - touch a hyperlink with the mouse
  • EXITED - move the mouse pointer away from a touched hyperlink
  • ACTIVATED - click a hyperlink with the mouse

The code in Listing 7 is straightforward and shouldn't require further explanation.

Listing 7 also signals the end of the class named Java4655cHtmlHandler .

The program output

Figure 1 shows the output that you should see if you compile and run this program. Note however that the content of this page changes each semester so youmay see something a little different.

Figure 1 - JFrame output from the program named Java4655c.

Missing image

As I mentioned earlier, this program doesn't allow scrolling for web pages that are too large to fit in the JEditorPane . As you can see in Figure 1 , the webpage simply spills outside the viewing area. We will deal with that later in this module.

This program also doesn't support link following. If you touch or click a link, the code in Listing 7 simply reports that fact on the command line screen. We will also deal with that later in this module.

Figure 2 shows the result of moving the mouse around inside the hyperlinks in Figure 1 and finally clicking on one of those links to create an ACTIVATED event.

Figure 2 - Command line output from the program named Java4655c.

Missing image

Now that you know the basics, we can put some meat on the skeleton.

The program named Java4655b

This program is similar to the previous program except that it allows you to follow hyperlinks. However, I will continue to defer scrolling until later inthe module.

Once again, I will explain this program in fragments. However, rather than to explain the complete program, I will explain only those things that aresignificantly different from the previous program.

A complete listing of the program is provided in Listing 12 .

The constructor for the Java4655bHtmlHandler class

Listing 8 shows the beginning of the Java4655bHtmlHandler class including the entire constructor.

Listing 8 - The constructor for the Java4655bHtmlHandler class.
class Java4655bHtmlHandler extends JFrame implements HyperlinkListener{JEditorPane html; //Constructorpublic Java4655bHtmlHandler(String websiteString) { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setTitle("Copyright 2014, R.G.Baldwin"); try{if(websiteString != null) { html = new JEditorPane(websiteString);html.setEditable(false); html.addHyperlinkListener(this);this.getContentPane().add(html,BorderLayout.CENTER); this.setSize(669,669);this.setVisible(true); }//end if}catch(Exception e){ e.printStackTrace();}//end catch }//end constructor

Differences from the previous program

One difference between the code in Listing 8 and the corresponding code in the previous program is that this version requires the websiteto be specified as a String instead of a URL .

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask