Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 555
sacic Avatar asked Aug 22 '19 17:08

sacic


People also ask

How do I limit the number of characters in a text box?

Right-click the text box for which you want to limit characters, and then click Text Box Properties on the shortcut menu. Click the Display tab. Under Options, select the Limit text box to check box, and then specify the number of characters that you want.

How to limit the text length in html?

The HTML <input> tag is used to get user input in HTML. To give a limit to the input field, use the min and max attributes, which is to specify a maximum and minimum value for an input field respectively. To limit the number of characters, use the maxlength attribute.

How do you restrict the length of a TextField in flutter?

using maxLength will give you length counter and increases the Field height, so this answer would be the accepted one, this one is working fine. Thank You, It is working fine to me. I used this to restrict the length of AutocompleteTextField.


1 Answers

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

like image 194
Zephyr Avatar answered Oct 05 '22 23:10

Zephyr