11

I am building a property management system for desktop and I am currently working on a payment feed feature. I want the payment amount to be highlighted in a different color within my label to improve readability.

I have tried the following approach:

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

I then 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 !important I would override the styles applied in label2, but instead all text renders to the screen in #fff

How would I go about achieving the desired result?

Halfpint
  • 3,967
  • 9
  • 50
  • 92
  • possible duplicate of [Javafx Text multi-word colorization](http://stackoverflow.com/questions/15081892/javafx-text-multi-word-colorization) – jewelsea Feb 13 '14 at 15:45

2 Answers2

10

Please try using Text inplace of Label for amount. I hope it will fix the issue. You can directly apply color to the Text as well.

Text amount = new Text("350");
amount.setFill(Color.RED);
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
5

My solution for this was to use a TextFlow pane:

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

JoschJava
  • 1,152
  • 12
  • 20