Disable spacing between items in TextFlow in JavaFX

1.2k Views Asked by At

I'm trying to make a line of text which consists of a name and a string of text. I want the name to be a hyperlink and the rest to be just plain text.

I thought TextFlow would be good for this, but the problem is it automatically puts a single space between the hyperlink and the text. What if I want the TextFlow to be for example

Jane's awesome

The TextFlow will make that a

Jane 's awesome

Is there a method or CSS property to disable this behaviour?

1

There are 1 best solutions below

0
On BEST ANSWER

Solution

You can remove the padding via a CSS style:

.hyperlink {
    -fx-padding: 0;
}

Or you can do it in code if you wish:

link.setPadding(new Insets(0));

Background

The default setting can be found in the modena.css file in the jfxrt.jar file packaged with your JRE distribution and it is:

-fx-padding: 0.166667em 0.25em 0.166667em 0.25em; /* 2 3 2 3 */

Sample application

sample application

In the sample screenshot the second hyperlink has focus (hence its dashed border).

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;

public class HyperSpace extends Application {

    @Override
    public void start(Stage stage) {
        TextFlow textFlow = new TextFlow(
            unstyle(new Hyperlink("Jane")), 
            new Text("'s awesome "), 
            unstyle(new Hyperlink("links"))
        );
        stage.setScene(new Scene(new Pane(textFlow)));
        stage.show();
    }

    private Hyperlink unstyle(Hyperlink link) {
        link.setPadding(new Insets(0));
        return link;
    }

    public static void main(String[] args) {
        launch(args);
    }
}