I have a panel with multiple JTextFields. One field holds a coordinate value and when it gains focus it becomes set as an active text field singleton. This is done so that when the field loses focus due to the user clicking on the map, the field is still updated with coordinates.

I want to make the field stop updating with coordinates once focus has changed to another text field.

Right now, the only way I can achieve this is by disabling the active text field singleton when any other text field gains focus. This doesn't seem too elegant to me since I will have to add a focus listener to every new text field and then disable the active text field singleton.

有帮助吗?

解决方案 2

I was able to come up with a solution using the "getOppositeComponent" method on the FocusEvent. This lets me check the instance of the opposite component. If it is the map, I can keep the active text field singleton, if it is anything else, then I can remove it.

其他提示

With FocusGained and FocusLost you can just do: if (FocusLost from JTextfield) stop updating

(FYI: this is not actual code)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top