我有一个 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,我试图在一个独立的示例中重现它,但无法让它始终失败。
我感谢任何想法/提示。
跟进 -
似乎我第一次打开对话框时,它获得了焦点,然后当我关闭并重新打开对话框时,焦点并不总是被设置。
有趣的是,关闭对话框后,如果我在再次打开对话框之前更改父级的焦点,焦点似乎总是被设置。