JTextField : How to set text on the left of JTextField when text too long

谁说我不能喝 提交于 2019-12-23 08:50:15

问题


I have a long String that I want to display in a JTextField. If the String is too long, it is showing the right-portion of the String, rather than the left portion, even when I use setHorizontalAlignment(JTextField.LEFT)

For example, if my String is "JTextField example , this text is too long", it should show as...

|----------------------|
| JTextField example ..|
|----------------------|

but instead it shows as...

|----------------------|
| this text is too long|
|----------------------|

Could someone please suggest how this can be fixed.


回答1:


the horizontalAlignement works fine when the size of the field is bigger than the number of chars of the string, but if it is smaller it only does the LEFT_ALIGNMENT with the setText of its creation, not with any later setText.

You could force the position of the caret to the begining of the text using:

myTextField.setCaretPosition(0);


来源:https://stackoverflow.com/questions/10309235/jtextfield-how-to-set-text-on-the-left-of-jtextfield-when-text-too-long

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