2

As the title, it's possible to to apply a default color to all text of a TextFlow component?

TextFlow textFlow = new TextFlow();
textFlow.setId("supertextflow");

// Somewhere else in the code
textFlow.getChildren()
    .add(new Text("Dynamic added text! OMG!"));

I tryed different solution but none of them works

#supertextflow {
    -fx-text-fill: red;
}

#supertextflow * .text{
    -fx-fill: red;
}

#supertextflow > * > .text {
    -fx-fill: red;
}

I know that Text is another component, but why i can't style it from it's parent?

Emax
  • 1,343
  • 2
  • 19
  • 30

1 Answers1

3

Well you can't do that with Text cause it's style class doesn't have fill css rule if you look at the JavaFX CSS Reference Guide. So I would suggest to leave the Text and use Label instead. If you do then you could use the css rule below :

#supertextflow > .label {
    -fx-text-fill: blue;
    -fx-font-size : 20px;
}

In case you want to keep using Text you will have to set each element (Text) inside the FlowPane a specific id (ex. #customText) and then use it to set the CSS rule like below :

#supertextflow > #customText {
    -fx-fill: red;
    -fx-font-size : 20px;
}

Edit : As James_D mentioned on the commends below you should use Type Selector (I am guessing that's the correct term) on the CSS rule in order to style all the Text nodes inside your TextFlow without needed to set any ids on them :

#supertextflow > Text { 
    -fx-fill: red;
    -fx-font-size : 20px;
}
James_D
  • 201,275
  • 16
  • 291
  • 322
JKostikiadis
  • 2,847
  • 2
  • 22
  • 34
  • 1
    Doesn't it work with a type selector, i.e. `#supertextflow > Text { -fx-fill: red; }`, with no need to use labels and no need to set style classes or ids on the `Text` instances? – James_D Nov 30 '17 at 01:17
  • @James_D Oh yes! I thought I was forgetting something :P. Using `#supertextflow > .text` will not work but using `#supertextflow > Text` instead will do the trick. Thanks for pointing that out. – JKostikiadis Nov 30 '17 at 01:28