Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX8 How to set a label's ellipsis from the left-side not from the right-side?

JavaFX 8 can auto ellipsisize Label's text started at the right side. For example, if there is a sentence 'Answer your own question', it may be ellipsisized to 'Answer your own que...' because there is no enough space to show the sentence completely.

But now, I want to ellipsize the left-side text, not the right-side. For example, if there is a sentence 'Answer your own question', I want to make it ellipsized to '...our own question'.

On Android, it can be easily implemented by setting the TextView's ellipsize attribute in a XML file, but on JavaFX, I have no idea to do that.

like image 853
user3382499 Avatar asked Jan 10 '23 10:01

user3382499


1 Answers

Use

label.setTextOverrun(OverrunStyle.LEADING_ELLIPSIS);

or

label.setTextOverrun(OverrunStyle.LEADING_WORD_ELLIPSIS);

depending on the exact behavior you want. (Documentation is here.)

You can also do this in FXML:

<Label text="Answer your own question" textOverrun="LEADING_ELLIPSIS"/>

or even in css:

.label {
    -fx-text-overrun: leading-ellipsis ;
}

(The latter will set the text overrun for all labels; you can use an id as the selector if you want to apply it to just one label.)

like image 188
James_D Avatar answered Jan 11 '23 22:01

James_D