Link Labels—A Java Component for Hyperlinks

A link label is a Java Swing component that provides hyperlink functionality for Java applications. Link labels display a hyperlink and generate an event when the cursor is moved on and off the hyperlink and when the mouse is clicked over it. Interested clients listen for link label events to enable them to invoke application dependent hyperlink behavior.

1. Introduction

A link label is a Java Swing component (Geary 1999) that provides hyperlink functionality for Java applications. Link labels display a hyperlink that users click on to invoke application dependent hyperlink behavior:

Link labels provide hyperlinks for Java applications

2. Interaction

When the cursor is moved over a link label, the cursor changes to the standard pointing finger hyperlink cursor, and the color of the hyperlink changes from blue to red, as shown below. When the cursor is moved off the link label, the cursor changes back to whatever is was before it was changed to the hyperlink cursor, and the color of the hyperlink text changes from red back to blue.

Cursor changes to pointing finger over a Link Label

3. Implementation

Link labels are implemented by the LinkLabel class. LinkLabel objects are created with a URL and, optionally, the text of the hyperlink. If no hyperlink text is supplied, the URL is used as the text of the hyperlink. The cursor on and off hyperlink colors can be set by client code.

Link labels do not implement any hyperlink behavior themselves; they generate events (LinkLabelEvent objects) that interested clients (objects that implement the LinkLabelEventListener interface) listen for. An event is generated whenever the cursor is moved on and off the hyperlink, and when a mouse button is clicked when the cursor is over the hyperlink. The URL is sent to each event listener to notify them that the hyperlink was activated. The initiating mouse event is also sent to enable clients to determine which mouse button was clicked. The following diagram illustrates the class diagram of the LinkLabel class.

Class diagram of the LinkLabel class

References

  • Geary, D. M., Graphic Java 2 Mastering the JFC, Volume II: Swing, 3rd edition, Prentice-Hall, 1999.