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.
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:
Hyperlink, ActionEvent
| Modifier and Type | Field and Description |
|---|---|
| private static final String | DEFAULT_STYLE_CLASS
* Stylesheet Handling * * |
| private ObjectProperty | |
| private final StringProperty |
| Access | Constructor and Description |
|---|---|
| public | |
| public | HyperlinkLabel(String
The text to display to the user. text)Creates a HyperlinkLabel instance with the given text value used as the initial text. |
| Modifier and Type | Method and Description |
|---|---|
| protected Skin | createDefaultSkin()
Overrides javafx. Create a new instance of the default skin for this control. |
| public final EventHandler | |
| public final String | |
| public final ObjectProperty | Returns: anObjectProperty representing the action.The action, which is invoked whenever a hyperlink is fired. |
| public final void | setOnAction(EventHandler<ActionEvent> value)
Sets a new EventHandler which will be invoked whenever a hyperlink is fired. |
| public final void | |
| public final StringProperty | Returns: aStringProperty.Return a |
| DEFAULT_STYLE_CLASS | back to summary |
|---|---|
| private static final String DEFAULT_STYLE_CLASS * Stylesheet Handling * * | |
| onAction | back to summary |
|---|---|
| private ObjectProperty<EventHandler<ActionEvent>> onAction | |
| text | back to summary |
|---|---|
| private final StringProperty text | |
| HyperlinkLabel | back to summary |
|---|---|
| public HyperlinkLabel() Creates an empty HyperlinkLabel instance with no | |
| HyperlinkLabel | back to summary |
|---|---|
| public HyperlinkLabel(String text) Creates a HyperlinkLabel instance with the given text value used as the initial text.
| |
| createDefaultSkin | back to summary |
|---|---|
| protected Skin Overrides javafx. Doc from javafx. Create a new instance of the default skin for this control. This is called to create a skin for the control if
no skin is provided via CSS
| |
| getOnAction | back to summary |
|---|---|
public final EventHandler
| |
| getText | back to summary |
|---|---|
| public final String getText() Return the text currently displayed.
| |
| onActionProperty | back to summary |
|---|---|
| public final ObjectProperty 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.
| |
| setOnAction | back to summary |
|---|---|
| public final void setOnAction(EventHandler<ActionEvent> value) Sets a new EventHandler which will be invoked whenever a hyperlink is fired. | |
| setText | back to summary |
|---|---|
| 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. | |
| textProperty | back to summary |
|---|---|
| public final StringProperty textProperty() Return a
| |