I Have a Text Field component , that has a characters remaining label, and also limits the amount
of characters allowed. i have it working but the problem is that when the limit is reached(20 characters) the Cursor of the text box goes to the first character in the field
This is the Script that i found that works for the limit, but the cursor goes to the first character, I want the text cursor to stay in the last character.
if event.propertyName == 'text':
if len(event.newValue) > 20:
def doLate(evt=event):
evt.source.text = evt.oldValue
system.util.invokeLater(doLate)
in this example i would type two more characterts it would at 1 more character and then the cursor moves to before the M. i need to stay at then end.
if event.propertyName == 'text':
if len(event.newValue) > 20:
def doLate(evt=event):
evt.source.text = evt.oldValue
evt.source.selectionEnd = 20
evt.source.selectionStart = 20
system.util.invokeLater(doLate)
the reason why i used the oldValue, is that this is for a password field, so let’s say if the user is typing and does not look that field, if he types 22 characters, with your solution the character twenty will be replaced and if they approved that password, then they will not know that it was actually changed automatically with the script, so next login they will have incorrect password. i thougth it would be better to keep the old value, that way if the password it’s type next login if they type the same first 20 characters, password will be correct.
I apologize for necroposting, but I think it's worth doing in this case. I would advise being extremely cautious about using an approach like this. If the user were to tab/click/navigate away from the text box during the time window in which those KeyEvents were being processed (which is longer than you might think), the JVM will simulate those key presses wherever the user's focus is at - leading to unexpected, potentially dangerous behavior.
Generally speaking, it is virtually never a good idea to use Java.Robot outside of UI unit testing. Using it to bandaid over a text selection issue in an HMI application? You're just asking for trouble.