Share the post "How To Add A Titled Border On A TextArea In Java FX"
You may want yo create something like this.
Now, even using CSS this is not possible if you directly add a label on the TextArea itself and change its z-location. I came across a post somewhere in the Stack Overflow forum and user jewelsea created a custom made BorderedTitledPane class that simulates a Titled Border in Java’s Swing.
Using this class I added a TextArea to the BorderedTitledPane and with a little CSS tweaking, removed the border and changed its z-location so that the Label title will be positioned right between the border of the TextArea.
Here is the BorderedTitlePane.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class BorderedTitledPane extends StackPane { public BorderedTitledPane(String titleString, Node content) { Label title = new Label(" " + titleString + " "); title.getStyleClass().add("bordered-titled-title"); StackPane.setMargin(title, new Insets(0, 25, 0, 0)); StackPane.setAlignment(title, Pos.TOP_RIGHT); StackPane contentPane = new StackPane(); contentPane.getChildren().add(content); getStyleClass().add("bordered-titled-border"); getChildren().addAll(contentPane, title); } } |
And here is the CSS code to make that possible.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
.bordered-titled-title { -fx-background-color: darkgray; -fx-translate-y: -13; -fx-padding: 0 0 5 0; } .bordered-titled-border { -fx-content-display: top; -fx-border-insets: 15 0 0 0; -fx-border-color: transparent; } .bordered-titled-content { -fx-border-insets: 20 0 0 0; } |
Doing this in Swing is possibly tedious but Java FX and CSS gives developers so much flexibility creating new layouts.
To use the class, call it like this.
|
1 |
BorderedTitledPane btp = new BorderedTitledPane("My Title, myTextArea); |
And add it to the scene’s container.