How do I draw a border around the text of a JavaFX label?
In order to improve the readability of our text, it was suggested that the text of the shortcut controls be outlined. I know you can iron the text, but we went with shortcuts. I forget why, but I think it was because the Label could do for us what the text form could not.
How do I draw a drawing or border around the letters and words of the label?
I tried -fx-stroke
it but it didn't work and -fx-border
just drew a border around the node. Is there a way to make this work?
source to share
Use CSS to outline the outline of your label text.
/** file: outline.css (place in same directory as OutlinedLabelText) */
.label {
-fx-font-size: 50px;
}
.outline.label .text {
-fx-fill: lightseagreen;
-fx-stroke: firebrick;
-fx-stroke-width: 2px;
}
Sample usage:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class OutlinedLabelText extends Application {
@Override
public void start(Stage stage) throws Exception {
Label label = new Label("Outlined");
label.getStyleClass().add("outline");
StackPane layout = new StackPane(label);
layout.setPadding(new Insets(10));
Scene scene = new Scene(layout, Color.PALEGREEN);
scene.getStylesheets().addAll(getClass().getResource(
"outline.css"
).toExternalForm());
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The example above uses an undocumented selector to select the text to style from a label. Typically the JavaFX CSS documentation for complex nodes that are parent nodes containing child nodes includes a section for child nodes in the documentation section called "Carrier". There is no substructure for Labeled , but if it did, it would read something like the following:
Carrier
- text - the text node in the tagged label
I discovered the selector .text
using a scene graph introspection tool like ScenicView . For Java 9, the JavaFX implementation sets the style class to code in the class constructor for the following class: com.sun.javafx.scene.control.LabeledText
where it calls getStyleClass().addAll("text");
. So anyway, this is where the somewhat magical subcomponent class comes from .text
.
source to share