JavaFX changes the color of a single word on a shortcut

I am creating a property management system for the desktop, and now I am working on the payment submission function. I want the payment amount to be highlighted in a different color on my label to improve readability.

I tried the following approach:

String datePaid  = "just now";
Label amount = new Label("350");
Label label2 = new Label("paid £" + amount.getText() + " " + datePaid);

Then I tried to apply the following CSS

    amount.setStyle("-fx-text-fill: #000 !important; -fx-highlight-text-fill: #000 !important; -fx-font-family: Arial");
    label2.setStyle("-fx-text-fill: #fff; -fx-font-size: 14px; -fx-translate-x: -36; -fx-translate-y: 24; -fx-font-family: 'Open Sans Light'");

I thought by declaring !importantI would override the styles used in label2, but instead, all the text is displayed on screen in #fff

How can I achieve the desired result?

+6
source share
2 answers

Try using Text inplace of Label for the sum. Hope this solves the problem. You can directly apply color to the text.

Text amount = new Text("350");
amount.setFill(Color.RED);
+7
source

TextFlow :

TextFlow textFlowPane = new TextFlow();
Text redText = new Text("This is red text...");
redText.setFill(Color.RED);
Text greenText = new Text("followed by green text");
greenText.setFill(Color.GREEN);
textFlowPane.getChildren().addAll(redText, greenText);

Red and green text with textFlow

0

Source: https://habr.com/ru/post/1526747/


All Articles