我有一个 jDialog,其中包含一些需要聚焦的字段。
我看到一些奇怪的行为,有时聚焦会失败,如果您按下 Tab 键,您可以在下面的底层父窗口中看到焦点发生变化,很明显焦点没有转移。
我读了一篇关于聚焦的有趣文章(由 camickr 撰写): http://tips4java.wordpress.com/2010/03/14/dialog-focus/ 但这并没有解决问题。
虽然使用那个监听器,我可以轻松地添加调试以尝试查看发生了什么...
public class RequestFocusListener implements AncestorListener
{
private boolean removeListener;
protected static org.slf4j.Logger logger = LoggerFactory.getLogger(RequestFocusListener.class);
/*
* Convenience constructor. The listener is only used once and then it is
* removed from the component.
*/
public RequestFocusListener() {
this(true);
}
/*
* Constructor that controls whether this listen can be used once or
* multiple times.
*
* @param removeListener when true this listener is only invoked once
* otherwise it can be invoked multiple times.
*/
public RequestFocusListener(boolean removeListener) {
logger.debug("creating RequestFocusListener, removeListener = " + removeListener);
this.removeListener = removeListener;
}
@Override
public void ancestorAdded(AncestorEvent e)
{
logger.debug("ancestorAdded detected");
JComponent component = e.getComponent();
logger.debug("requesting focus");
boolean success = component.requestFocusInWindow();
logger.debug("request focus in window result was: " + success);
if (!success) {
logger.debug("KeyboardFocusManager says focus failed.\nfocus owner is " + KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());
logger.debug("displayable="+component.isDisplayable());
logger.debug("lightweight="+component.isLightweight());
logger.debug("enabled="+component.isEnabled());
logger.debug("focusable="+component.isFocusable());
logger.debug("showing="+component.isShowing());
logger.debug("isRequestFocusEnabled="+component.isRequestFocusEnabled());
} else {
logger.debug("KeyboardFocusManager says we got focus. focus owner is " + KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());
}
if (removeListener) {
component.removeAncestorListener( this );
}
}
@Override
public void ancestorMoved(AncestorEvent e) {
}
@Override
public void ancestorRemoved(AncestorEvent e) {
}
然后我将监听器添加到 JDialog 主面板中的一个组件
radioButton.addAncestorListener(new RequestFocusAncestorListener());
我得到的输出显示:
displayable=true
lightweight=true
enabled=true
focusable=true
showing=true
isRequestFocusEnabled=true
单步执行代码以查看导致请求失败的原因,我看到它在 Component.requestFocusHelper 中停止:
boolean success = peer.requestFocus(this, temporary, focusedWindowChangeAllowed, time, cause);
我读到组件必须是可显示的/可见的/可聚焦的)但调试显示没问题。
谁能阐明其他可能导致 requestFocus 失败的原因? (并将焦点留在调用父面板中,在本例中是在 jtable 中)
很抱歉没有提供完整的 SSCCE,我已经尝试在一个独立的示例中重现它,但无法让它一直失败。
我很感激任何想法/提示。
跟进 -
似乎我第一次打开对话框时,它获得了焦点,然后当我关闭并重新打开对话框时,焦点并不总是被设置。
有趣的是,关闭对话框后,如果我在再次打开对话框之前更改父项中的焦点,焦点似乎总是得到设置。
最佳答案
焦点请求失败的可能原因有很多。
首先,Component#requestFocus
的 Java 文档实际上指出
Because the focus behavior of this method is platform-dependent, developers are strongly encouraged to use requestFocusInWindow when possible.
和
This component must be displayable, focusable, visible and all of its ancestors (with the exception of the top-level Window) must be visible for the request to be granted
为了使组件变得可聚焦,组件及其所有祖先必须有效(可显示)。我经常看到的一个常见错误是人们在创建新窗口时使用 requestFocus
或 requestFocusInWindow
,但在该窗口实际显示在屏幕上之前(setVisible
不保证窗口会立即可见,只是在未来某个时间它会变得可见。
在这种情况下最好的方法是使用 WindowListener
并监视 windowOpened
事件。然后,我很想使用 SwingUtilities#invokeLater
来确保窗口实际上可以显示在屏幕上。
另一个问题是依赖isDisplayable
。即使组件所在的窗口未(显示在屏幕上),此方法也可能返回 true
。
当组件连接到本地屏幕资源时,它就可以显示了。当它的祖先窗口被打包或可见时,就会发生这种情况……事实上,我发现很难准确确定何时会发生这种情况。
更新
我还应该补充一点,requestFocus
就是一个“请求”。焦点管理子系统可能会否决请求,因为另一个字段拒绝放弃焦点(例如当字段 InputVerifier#shouldYieldFocus
返回 false
时)
关于java - 是什么导致组件 requestFocus 有时失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13810777/