JavaFX TextFlow set default text color

[亡魂溺海] 提交于 2020-01-25 07:59:43

问题


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?


回答1:


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;
}


来源:https://stackoverflow.com/questions/47562036/javafx-textflow-set-default-text-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!