Class HyperlinkLabel

  • All Implemented Interfaces:
    Styleable, EventTarget, Skinnable

    public class HyperlinkLabel
    extends Control
    implements EventTarget
    A UI control that will convert the given text into a series of text labels and hyperlinks, based on the use of delimiter characters to specify where hyperlinks should appear. The delimiter characters are square braces (that is, [ and ]). To create a hyperlink in a string you would therefore do something like hyperlinkLabel.setText("Click [here] for more information!");, with the word 'here' appearing as a hyperlink that a use may click. This approach therefore allows for hyperlinks to be easily embedded within a label.

    Once hyperlinks have been declared in a text string, it is necessary to respond to the user interacting with the hyperlink (most commonly via mouse clicks). To do so, you register a single event handler for action events on the HyperlinkLabel instance, and then determine what to do within that callback. For example:

     hyperlinkLabel.setOnAction(new EventHandler<ActionEvent>() {
         public void handle(ActionEvent event) {
             Hyperlink link = (Hyperlink)event.getSource();
             final String str = link == null ? "" : link.getText();
             switch(str) {
                 case "here": // do 'here' action
                 case "exit": // do exit action

    This simple single-handler approach was chosen over any more complex per-hyperlink solution because it is anticipated that most use cases will normally consist of one, or very few hyperlinks, and it was therefore unlikely that the increased API complexity would be warranted.


    To demonstrate what a HyperlinkLabel looks like, refer to the screenshot below, when the text "Hello [world]! I [wonder] what hyperlink [you] [will] [click]" was passed in to the HyperlinkLabel instance:

    Screenshot of HyperlinkLabel
    See Also:
    Hyperlink, ActionEvent
    • Constructor Detail

      • HyperlinkLabel

        public HyperlinkLabel()
        Creates an empty HyperlinkLabel instance with no text specified.
      • HyperlinkLabel

        public HyperlinkLabel​(String text)
        Creates a HyperlinkLabel instance with the given text value used as the initial text.
        text - The text to display to the user.
    • Method Detail

      • getText

        public final String getText()
        Return the text currently displayed.
        the text currently displayed.
      • setText

        public final void setText​(String value)
        Set a new text to display to the user, using the delimiter characters [ and ] to indicate where hyperlinks should be displayed.
        value -
      • setOnAction

        public final void setOnAction​(EventHandler<ActionEvent> value)
        Sets a new EventHandler which will be invoked whenever a hyperlink is fired.
        value -
      • getOnAction

        public final EventHandler<ActionEvent> getOnAction()
        the action, which is invoked whenever a hyperlink is fired.
      • getUserAgentStylesheet

        protected final String getUserAgentStylesheet​(Class<?> clazz,
                                                      String fileName)
        A helper method that ensures that the resource based lookup of the user agent stylesheet only happens once. Caches the external form of the resource.
        clazz - the class used for the resource lookup
        fileName - the name of the user agent stylesheet
        the external form of the user agent stylesheet (the path)