Class HyperlinkLabel

  • All Implemented Interfaces:
    javafx.css.Styleable, javafx.event.EventTarget, javafx.scene.control.Skinnable

    public class HyperlinkLabel
    extends javafx.scene.control.Control
    implements javafx.event.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
                              break;
                 case "exit": // do exit action
                              break;
             }
         }
     });

    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.

    Screenshot

    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
    • Field Summary

      • Fields inherited from class javafx.scene.layout.Region

        USE_COMPUTED_SIZE, USE_PREF_SIZE
      • Fields inherited from class javafx.scene.Node

        BASELINE_OFFSET_SAME_AS_HEIGHT
    • Constructor Summary

      Constructors 
      Constructor Description
      HyperlinkLabel()
      Creates an empty HyperlinkLabel instance with no text specified.
      HyperlinkLabel​(String text)
      Creates a HyperlinkLabel instance with the given text value used as the initial text.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected javafx.scene.control.Skin<?> createDefaultSkin()
      javafx.event.EventHandler<javafx.event.ActionEvent> getOnAction()  
      String getText()
      Return the text currently displayed.
      protected String getUserAgentStylesheet​(Class<?> clazz, String fileName)
      A helper method that ensures that the resource based lookup of the user agent stylesheet only happens once.
      javafx.beans.property.ObjectProperty<javafx.event.EventHandler<javafx.event.ActionEvent>> onActionProperty()
      The action, which is invoked whenever a hyperlink is fired.
      void setOnAction​(javafx.event.EventHandler<javafx.event.ActionEvent> value)
      Sets a new EventHandler which will be invoked whenever a hyperlink is fired.
      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.
      javafx.beans.property.StringProperty textProperty()
      Return a StringProperty representing the text being displayed.
      • Methods inherited from class javafx.scene.control.Control

        computeMaxHeight, computeMaxWidth, computeMinHeight, computeMinWidth, computePrefHeight, computePrefWidth, contextMenuProperty, executeAccessibleAction, getBaselineOffset, getClassCssMetaData, getContextMenu, getControlCssMetaData, getCssMetaData, getInitialFocusTraversable, getSkin, getTooltip, isResizable, layoutChildren, queryAccessibleAttribute, setContextMenu, setSkin, setTooltip, skinProperty, tooltipProperty
      • Methods inherited from class javafx.scene.layout.Region

        backgroundProperty, borderProperty, cacheShapeProperty, centerShapeProperty, getBackground, getBorder, getHeight, getInsets, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getOpaqueInsets, getPadding, getPrefHeight, getPrefWidth, getShape, getUserAgentStylesheet, getWidth, heightProperty, insetsProperty, isCacheShape, isCenterShape, isScaleShape, isSnapToPixel, layoutInArea, layoutInArea, layoutInArea, layoutInArea, maxHeight, maxHeightProperty, maxWidth, maxWidthProperty, minHeight, minHeightProperty, minWidth, minWidthProperty, opaqueInsetsProperty, paddingProperty, positionInArea, positionInArea, prefHeight, prefHeightProperty, prefWidth, prefWidthProperty, resize, scaleShapeProperty, setBackground, setBorder, setCacheShape, setCenterShape, setHeight, setMaxHeight, setMaxSize, setMaxWidth, setMinHeight, setMinSize, setMinWidth, setOpaqueInsets, setPadding, setPrefHeight, setPrefSize, setPrefWidth, setScaleShape, setShape, setSnapToPixel, setWidth, shapeProperty, snappedBottomInset, snappedLeftInset, snappedRightInset, snappedTopInset, snapPosition, snapPositionX, snapPositionY, snapSize, snapSizeX, snapSizeY, snapSpace, snapSpaceX, snapSpaceY, snapToPixelProperty, widthProperty
      • Methods inherited from class javafx.scene.Parent

        getChildren, getChildrenUnmodifiable, getManagedChildren, getStylesheets, isNeedsLayout, layout, lookup, needsLayoutProperty, requestLayout, requestParentLayout, setNeedsLayout, updateBounds
      • Methods inherited from class javafx.scene.Node

        accessibleHelpProperty, accessibleRoleDescriptionProperty, accessibleRoleProperty, accessibleTextProperty, addEventFilter, addEventHandler, applyCss, autosize, blendModeProperty, boundsInLocalProperty, boundsInParentProperty, buildEventDispatchChain, cacheHintProperty, cacheProperty, clipProperty, computeAreaInScreen, contains, contains, cursorProperty, depthTestProperty, disabledProperty, disableProperty, effectiveNodeOrientationProperty, effectProperty, eventDispatcherProperty, fireEvent, focusedProperty, focusTraversableProperty, getAccessibleHelp, getAccessibleRole, getAccessibleRoleDescription, getAccessibleText, getBlendMode, getBoundsInLocal, getBoundsInParent, getCacheHint, getClip, getContentBias, getCursor, getDepthTest, getEffect, getEffectiveNodeOrientation, getEventDispatcher, getId, getInitialCursor, getInputMethodRequests, getLayoutBounds, getLayoutX, getLayoutY, getLocalToParentTransform, getLocalToSceneTransform, getNodeOrientation, getOnContextMenuRequested, getOnDragDetected, getOnDragDone, getOnDragDropped, getOnDragEntered, getOnDragExited, getOnDragOver, getOnInputMethodTextChanged, getOnKeyPressed, getOnKeyReleased, getOnKeyTyped, getOnMouseClicked, getOnMouseDragEntered, getOnMouseDragExited, getOnMouseDragged, getOnMouseDragOver, getOnMouseDragReleased, getOnMouseEntered, getOnMouseExited, getOnMouseMoved, getOnMousePressed, getOnMouseReleased, getOnRotate, getOnRotationFinished, getOnRotationStarted, getOnScroll, getOnScrollFinished, getOnScrollStarted, getOnSwipeDown, getOnSwipeLeft, getOnSwipeRight, getOnSwipeUp, getOnTouchMoved, getOnTouchPressed, getOnTouchReleased, getOnTouchStationary, getOnZoom, getOnZoomFinished, getOnZoomStarted, getOpacity, getParent, getProperties, getPseudoClassStates, getRotate, getRotationAxis, getScaleX, getScaleY, getScaleZ, getScene, getStyle, getStyleableParent, getStyleClass, getTransforms, getTranslateX, getTranslateY, getTranslateZ, getTypeSelector, getUserData, getViewOrder, hasProperties, hoverProperty, idProperty, inputMethodRequestsProperty, intersects, intersects, isCache, isDisable, isDisabled, isFocused, isFocusTraversable, isHover, isManaged, isMouseTransparent, isPickOnBounds, isPressed, isVisible, layoutBoundsProperty, layoutXProperty, layoutYProperty, localToParent, localToParent, localToParent, localToParent, localToParent, localToParentTransformProperty, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToSceneTransformProperty, localToScreen, localToScreen, localToScreen, localToScreen, localToScreen, lookupAll, managedProperty, mouseTransparentProperty, nodeOrientationProperty, notifyAccessibleAttributeChanged, onContextMenuRequestedProperty, onDragDetectedProperty, onDragDoneProperty, onDragDroppedProperty, onDragEnteredProperty, onDragExitedProperty, onDragOverProperty, onInputMethodTextChangedProperty, onKeyPressedProperty, onKeyReleasedProperty, onKeyTypedProperty, onMouseClickedProperty, onMouseDragEnteredProperty, onMouseDragExitedProperty, onMouseDraggedProperty, onMouseDragOverProperty, onMouseDragReleasedProperty, onMouseEnteredProperty, onMouseExitedProperty, onMouseMovedProperty, onMousePressedProperty, onMouseReleasedProperty, onRotateProperty, onRotationFinishedProperty, onRotationStartedProperty, onScrollFinishedProperty, onScrollProperty, onScrollStartedProperty, onSwipeDownProperty, onSwipeLeftProperty, onSwipeRightProperty, onSwipeUpProperty, onTouchMovedProperty, onTouchPressedProperty, onTouchReleasedProperty, onTouchStationaryProperty, onZoomFinishedProperty, onZoomProperty, onZoomStartedProperty, opacityProperty, parentProperty, parentToLocal, parentToLocal, parentToLocal, parentToLocal, parentToLocal, pickOnBoundsProperty, pressedProperty, pseudoClassStateChanged, relocate, removeEventFilter, removeEventHandler, requestFocus, resizeRelocate, rotateProperty, rotationAxisProperty, scaleXProperty, scaleYProperty, scaleZProperty, sceneProperty, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, screenToLocal, screenToLocal, screenToLocal, setAccessibleHelp, setAccessibleRole, setAccessibleRoleDescription, setAccessibleText, setBlendMode, setCache, setCacheHint, setClip, setCursor, setDepthTest, setDisable, setDisabled, setEffect, setEventDispatcher, setEventHandler, setFocused, setFocusTraversable, setHover, setId, setInputMethodRequests, setLayoutX, setLayoutY, setManaged, setMouseTransparent, setNodeOrientation, setOnContextMenuRequested, setOnDragDetected, setOnDragDone, setOnDragDropped, setOnDragEntered, setOnDragExited, setOnDragOver, setOnInputMethodTextChanged, setOnKeyPressed, setOnKeyReleased, setOnKeyTyped, setOnMouseClicked, setOnMouseDragEntered, setOnMouseDragExited, setOnMouseDragged, setOnMouseDragOver, setOnMouseDragReleased, setOnMouseEntered, setOnMouseExited, setOnMouseMoved, setOnMousePressed, setOnMouseReleased, setOnRotate, setOnRotationFinished, setOnRotationStarted, setOnScroll, setOnScrollFinished, setOnScrollStarted, setOnSwipeDown, setOnSwipeLeft, setOnSwipeRight, setOnSwipeUp, setOnTouchMoved, setOnTouchPressed, setOnTouchReleased, setOnTouchStationary, setOnZoom, setOnZoomFinished, setOnZoomStarted, setOpacity, setPickOnBounds, setPressed, setRotate, setRotationAxis, setScaleX, setScaleY, setScaleZ, setStyle, setTranslateX, setTranslateY, setTranslateZ, setUserData, setViewOrder, setVisible, snapshot, snapshot, startDragAndDrop, startFullDrag, styleProperty, toBack, toFront, toString, translateXProperty, translateYProperty, translateZProperty, usesMirroring, viewOrderProperty, visibleProperty
      • Methods inherited from interface javafx.event.EventTarget

        buildEventDispatchChain
      • Methods inherited from interface javafx.css.Styleable

        getStyleableNode
    • 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.
        Parameters:
        text - The text to display to the user.
    • Method Detail

      • createDefaultSkin

        protected javafx.scene.control.Skin<?> createDefaultSkin()
        Overrides:
        createDefaultSkin in class javafx.scene.control.Control
      • textProperty

        public final javafx.beans.property.StringProperty textProperty()
        Return a StringProperty representing the text being displayed.
        Returns:
        a StringProperty.
      • getText

        public final String getText()
        Return the text currently displayed.
        Returns:
        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.
        Parameters:
        value -
      • onActionProperty

        public final javafx.beans.property.ObjectProperty<javafx.event.EventHandler<javafx.event.ActionEvent>> onActionProperty()
        The action, which is invoked whenever a hyperlink is fired. This may be due to the user clicking on the hyperlink with the mouse, or by a touch event, or by a key press.
        Returns:
        an ObjectProperty representing the action.
      • setOnAction

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

        public final javafx.event.EventHandler<javafx.event.ActionEvent> getOnAction()
        Returns:
        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.
        Parameters:
        clazz - the class used for the resource lookup
        fileName - the name of the user agent stylesheet
        Returns:
        the external form of the user agent stylesheet (the path)