Java 如何找出当前具有焦点的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/720208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find out which object currently has focus
提问by Talha Bin Shakir
I have a few TextField
s in my Frame
. I want to know which TextField
currently has focus. How can I find this information?
我有几个TextField
在我的Frame
。我想知道哪个TextField
当前有焦点。我怎样才能找到这些信息?
采纳答案by Rob
JFrame.getFocusOwner()
(inherited from Window.getFocusOwner()
) ought to return a reference to the component with focus. getMostRecentFocusOwner()
might also be of interest.
JFrame.getFocusOwner()
(继承自Window.getFocusOwner()
)应该返回对具有焦点的组件的引用。getMostRecentFocusOwner()
可能也有兴趣。
回答by Uri
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.
每个 JComponent 都有一个 hasFocus 方法,您可以使用它来检查它是否具有焦点。但是,这已更改,现在您应该使用 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.
因此,遍历框架中的所有文本字段,并通过调用该方法检查每个文本字段是否为 isFocusOwner。
You could also get the focus owner through the frame.
您还可以通过框架获取焦点所有者。
回答by euphoria83
getFocusOwner()will return the child component which is currently focused.
getFocusOwner()将返回当前聚焦的子组件。
But you have to check to see if it is a JTextField. Other components like buttons might be focused if they exist in your frame as well.
但是您必须检查它是否是 JTextField。如果其他组件(如按钮)也存在于您的框架中,则它们可能会被聚焦。
回答by user85421
Also have a look at the javax.swing.FocusManager
回答by Peter Tseng
KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()
回答by Scouser
KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()
;
wont work across threads. So if your app invokes a new thread and that thread has its own frame/window etc then it wont be able to gain the focus owner from that thread. Instead use: KeyboardFocusManager.getCurrentKeyboardFocusManager().getGlobalFocusOwner();
KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()
;
不会跨线程工作。因此,如果您的应用程序调用一个新线程并且该线程具有自己的框架/窗口等,那么它将无法从该线程获得焦点所有者。而是使用:KeyboardFocusManager.getCurrentKeyboardFocusManager().getGlobalFocusOwner();
回答by predi
You could also listen for the appropriate property change in keyboard focus manager:
您还可以在键盘焦点管理器中监听适当的属性更改:
KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener("focusOwner", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println(evt.getNewValue());
}
});
This outputs focus owner as you interact with Swing components and is useful for debugging focus issues in general.
这会在您与 Swing 组件交互时输出焦点所有者,并且对于调试一般的焦点问题很有用。
回答by Wojciech Wirzbicki
You can get currently focused Component like that:
您可以像这样获得当前聚焦的组件:
Component focusOwner = FocusManager.getCurrentManager().getFocusOwner();
After that You can check if focusOwner
is instance of TextField
之后你可以检查是否focusOwner
是实例TextField