How to find out which object currently has focus

前端 未结 9 816
囚心锁ツ
囚心锁ツ 2020-12-03 13:38

I have a few TextFields in my Frame. I want to know which TextField currently has focus. How can I find this information?

9条回答
  •  眼角桃花
    2020-12-03 13:58

    Every JComponent has a hasFocus method that you can use to check if it has focus. However, this has been changed, and now you should use isFocusOwner.

    So run over all the text fields in your frame, and check on each of them if it is isFocusOwner by calling that method.

    You could also get the focus owner through the frame.

提交回复
热议问题