parml
parml

Reputation: 649

JavaFX Text Selection background

It is possible to set the fill for a selection area in a javafx.scene.text.Text.

setFill(RED);

However, there seems to be no equivalent setBackground(BLUE)

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        Pane p = new Pane();
        Scene s = new Scene(p);
        s.getStylesheets().add("main.css");
        primaryStage.setScene(s);
        primaryStage.show();

        addTextWithSelection(p);
    }

    private void addTextWithSelection(Pane p) {
        Text t = new Text(20,20, "Some Text selection some more text");
        t.setSelectionStart(11);
        t.setSelectionEnd(18);
        t.setSelectionFill(GREEN);
        p.getChildren().add(t);
    }
}

By default, the fill for the text selection is white. Having the text white is probably for preparing it for a different background, like blue.

How to achieve background for selected text?

Upvotes: 0

Views: 433

Answers (1)

Slaw
Slaw

Reputation: 46070

If you're using JavaFX 9+ then you can use the Text#selectionShape property:

The shape of the selection in local coordinates.

The property gives you a PathElement[] which can be used with a Path to implement a background color for selected text. Here's a proof-of-concept:

import javafx.application.Application;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Path;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public final class App extends Application {

  @Override
  public void start(Stage primaryStage) {
    var text = new Text("Some text to demonstrate the selection-shape property.");
    text.setTextOrigin(VPos.TOP);
    text.setFont(Font.font("Monospaced", 24));
    text.setSelectionStart(5);
    text.setSelectionEnd(24);
    text.setSelectionFill(Color.WHITE); // fill of the actual text when selected

    var path = new Path(text.getSelectionShape());
    path.setStrokeWidth(0);
    path.setFill(Color.FORESTGREEN);

    // add 'path' first so it's rendered underneath 'text'
    primaryStage.setScene(new Scene(new Pane(path, text)));
    primaryStage.show();
  }
}

Note in a real application where the selected text can change you'd want to observe the selectionShape property and update the Path as needed.

Also note that, as documented, the PathElement[] is given in the local coordinate space of the Text. This means any transformations applied to the Text (which don't affect its bounds-in-local) will need to be applied to the Path as well if you want them to align properly.

Upvotes: 1

Related Questions