Reputation: 1541
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!
Upvotes: 1
Views: 3298
Reputation: 159566
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);
}
}
Upvotes: 6