简体   繁体   中英

Change foreground color of selection in JavaFX textarea

I want to change the style of selected text in a JavaFx textarea. I already succeded changing the background color by setting -fx-accent , but I did not find out how to change the foreground color of the text. Does anyone know how to achieve this? I already went trough the modena.css file and tried many attributes, but until now without success.

Thank you very much!

From the JavaFX 8 CSS reference documentation , the css attribute for the foreground fill of selected text in text input controls such as text area, seems to be:

-fx-highlight-text-fill

Sample

On the left is a TextArea which does not have focus and has some selected text. On the right is a TextArea which has focus and some selected text. Custom styles are applied to the selected text foreground and background, differing in color depending upon the focus state.

不专心 专注

text-highlighter.css

.text-input {
    -fx-highlight-fill: paleturquoise;
    -fx-highlight-text-fill: blue;
}
.text-input:focused {
    -fx-highlight-fill: palegreen;
    -fx-highlight-text-fill: fuchsia;
}

TextHighlighter.java

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TextHighlighter extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        TextArea textArea = new TextArea(
                "The quick brown cat ran away with the spoon."
        );
        textArea.selectRange(4, 9);
        textArea.setWrapText(true);

        VBox layout = new VBox(10, new Button("Button"), textArea);
        final Scene scene = new Scene(layout);
        scene.getStylesheets().add(
                this.getClass().getResource("text-highlighter.css").toExternalForm()
        );
        stage.setScene(scene);
        stage.show();
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM