0

I needed a selectable version of a Label, so I decided to style and modify a TextArea so it would look and behave like a regular Label and be selectable. For the SelectableLabel to look like a regular Label I need to remove all of the padding/border. I've been trying to do this in my CSS but I've still got some border/padding left and don't know how to get rid of it.

This is how it looks now

enter image description here

This is the part of the CSS that I use to style my SelectableLabel.

#selectable-label {
    -fx-background-color: #292929;
    -fx-text-fill: white;
}
#selectable-label .content {
    -fx-padding: 0;
}

I have tried to change plenty of other properties I found by searching the internet but so far nothing worked.

This is the source for the example stage:

import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class ExampleStage extends Stage {

    public ExampleStage() {

        GridPane gridPane = new GridPane();
        gridPane.setPadding(new Insets(20));
        gridPane.setHgap(15);
        gridPane.setVgap(20);

        //First row
        Label l1 = new Label("Label 1");
        gridPane.add(l1, 0, 0);
        GridPane.setHalignment(l1, HPos.RIGHT);
        GridPane.setValignment(l1, VPos.TOP);

        Label text1 = new Label("This is a regular label");
        gridPane.add(text1, 1, 0);
        GridPane.setHalignment(text1, HPos.LEFT);
        GridPane.setValignment(text1, VPos.TOP);

        //Second row
        Label l2 = new Label("Label 2");
        gridPane.add(l2, 0, 1);
        GridPane.setHalignment(l2, HPos.RIGHT);
        GridPane.setValignment(l2, VPos.TOP);

        SelectableLabel text2 = new SelectableLabel("This is a selectable label");
        gridPane.add(text2, 1, 1);
        GridPane.setHalignment(text2, HPos.LEFT);
        GridPane.setValignment(text2, VPos.TOP);

        //Third row
        Label l3 = new Label("Label 3");
        gridPane.add(l3, 0, 2);
        GridPane.setHalignment(l3, HPos.RIGHT);
        GridPane.setValignment(l3, VPos.TOP);

        Label text3 = new Label("This is a regular label");
        gridPane.add(text3, 1, 2);
        GridPane.setHalignment(text3, HPos.LEFT);
        GridPane.setValignment(text3, VPos.TOP);

        Scene scene = new Scene(gridPane);
        scene.getStylesheets().add(GUIManager.stylesheet);

        this.setScene(scene);
        this.setTitle("Example Stage");
        this.setResizable(false);
        this.show();

    }

}

This is the source for the SelectableLabel itself:

import javafx.scene.control.TextArea;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;

public class SelectableLabel extends TextArea {

    public SelectableLabel() {
        this("", true);
    }

    public SelectableLabel(String text) {
        this(text, true);
    }

    public SelectableLabel(String text, boolean selectable) {
        super(text);

        this.setMouseTransparent(!selectable);
        this.setEditable(false);
        this.setFocusTraversable(false);
        this.setId("selectable-label");
        this.setWrapText(true);

        sizeToText();

        textProperty().addListener(e -> sizeToText());
    }

    public void setSelectable(boolean selectable) {
        setMouseTransparent(!selectable);
    }

    private void sizeToText() {
        Text t = new Text(getText());
        t.setFont(this.getFont());
        StackPane pane = new StackPane(t);
        pane.layout();
        double width = t.getLayoutBounds().getWidth();
        double height = t.getLayoutBounds().getHeight();
        this.setMaxSize(width, height);
    }

}
DeLoreanDriver
  • 205
  • 1
  • 10

0 Answers0