I am using a GTextField to allow the user to specify values being used in other processes. In the event handler for the GTextField I want to check what’s being typed and make sure that it is a valid value for my program. Below is what I have tried to accomplish this.
When I attempt to modify the text I get a NullPointerException. I have read on otherforums that this is due to the fact that a GTextField can’t be modified while it has focus.
This does stop processing from throwing a NullPointerException, but the text in the text field is never changed.
Does anyone have an idea of a workaround that I could try to implement?
Also, I thought that maybe instead of modifying the user’s input, I could just change the color of the text red to signal that it is an unaccepted value. Is there a way to change to the color of the text in a GTextField programmatically?
Thanks for the advice! I’ll try both of those when I am at home again. I just realized while looking over my code that I am not actually modifying the users string at all in my use of substring. I am just returning the entire original string. That could be (probably is) why I wasn’t seeing any change after modifying the text field.
Hopefully the fix is as simple as correcting that line of code. Either way I’ll reply here to give an update.
Ok, I tried using forceBufferUpdate() as shown below. The issue is that now when the setFocus(false) method is called it jumps to the beginning of the event handler method and creates an infinite loop.
In this thread a similar issue is discussed. I modified my code so the color changes based on if it meets the regex or not. If there is a solution that would allow setText to work while the GTextField has focus I would really like to know about it.