How do I limit the number of characters visible in a text field at a time to less than the textfield's width?

sacic :

I have included an image in my JavaFX TextField by following this post, but now my problem is that the text can overflow to cover up the image, like this:

Textfield text overlaps on image

What I want to do is, instead of the TextField moving & cutting off the text when I get to the edge of the TextField, it should start cutting off visible text when I get to the left edge of the image. How can I do this?

Zephyr :

Probably the simplest way to do this is to set the padding on the right side of your TextField to account for the image:

-fx-padding: 4px 25px 4px 7px;

This will keep the default padding for the top, bottom, and left, while updating the right value. The 25px here may need to be adjusted based on your image, but here's my example:

textField.setStyle(
        "-fx-background-image: url('resources/play.png');" +
        "-fx-background-repeat: no-repeat;" +
        "-fx-background-position: right center;" +
        "-fx-padding: 4px 25px 4px 7px;"
);

screenshot

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=139020&siteId=1