Neil
Neil

Reputation: 6039

Highlighting text in JavaFx Label

I am trying to set the text background of the JavaFx label text as green using the following CSS

label.setStyle("-fx-background-color:rgba(85, 255, 68,0.7););

And the unhighlight using the following

label.setStyle("-fx-background-color:rgba(0,0,255,0);");

However these does not work most of the times when it has to be done back to back.

Is there any way to set the style without using CSS i.e. using JavaFx API itself. I found label.textFill(Paint p) for text color but nothing for background colour i.e. the color of the label itself.

Upvotes: 0

Views: 7249

Answers (2)

Neil
Neil

Reputation: 6039

Finally I found the workarround. I had to give a PauseTransition to give the system some time between unhighlight and highlight. CSS showed effect only after the pausetransaction if the labels were already highlighted. I think it may be a bug. I will file a jira. The duration of paustransition may be as low as 1 milisecond so that there is not lag from the user's point of view.

Upvotes: 0

jewelsea
jewelsea

Reputation: 159341

Is there any way to set the style without using CSS i.e. using JavaFx API itself.

For some styles (such as the text fill) yes. For background colors, background images, borders, etc API methods will not be available until JavaFX 8 is released (see Public API for Region backgrounds and borders in the JavaFX issue tracker for more information - anybody can sign up for access).

these does not work most of the times when it has to be done back to back.

If you just highlight a label and then unhighlight it again without using something like a PauseTransition to give the user some time to see the highlighted label, then, from the user's perspective nothing is going to happen as all the user will see is an unhighlighted label.


Not sure of your use case, but if you only want to highlight part of the text in a label or let the user highlight the text with a mouse, then you can use a TextField with editable set to false.


Possible Workaround

If the Java 8 preview does not work for you and you are experiencing errors due do bugs in the JavaFX CSS processing, then try placing a Pane then a label inside a StackPane. Set the background color of the Pane to label.setStyle("-fx-background-color:rgba(85, 255, 68,0.7);); Bind the Pane's preferred width and height to the Label's width and height and toggle setVisible on the Pane as appropriate.

Upvotes: 1

Related Questions