Remove Focus from Text/Numeric fields on Esc or mouse click outside bounds


I’d like to be able to remove an object’s focus, in particular text fields and numeric text fields, when an operator has clicked ‘Enter’ to write the new value, has clicked outside of the object’s bounds, or presses the ‘Esc’ key.

Is this possible to do natively?
What’s the best way to achieve this if not natively?

Thanks in advance,


I have the same request!!
I really hate this “feature” as it leaves the value “live” allowing the operator to accidentally change it. Not good when you are dealing with a burner control.

Other HMI packages lose focus as soon as you click off of the object.

You can make this work fairly easily - pretty much everything inherits the Java component class, which has requestFocus(). So if you put this on the root container mousePressed() function, it will tell its parent to request focus:
On something like a text field you can put it in the keyTyped and check for the escape or return key to be typed, then do the same thing (except one or more .parent added before requestFocus).

But I agree, it should be a feature without doing this.

@mrogers solution looks like a good way to handle this after entry of text. We make input components lose focus as soon as the mouse is no longer over them with this code in the mouseEntered event of the background (you could put it in a different event to catch a click):

component = “focusOnMe”

focusOnMe is a component outside the viewable area of the screen so it doesn’t cause a highlight to appear on the screen.