我有一个 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 文档实际上说明了
因为这个方法的焦点行为是平台相关的, 强烈建议开发人员在以下情况下使用 requestFocusInWindow 可能。
和
这个组件必须是可显示的、可聚焦的、可见的,并且它的所有 祖先(顶级窗口除外)必须可见 请求被批准
为了使一个组件变得可聚焦,该组件及其所有祖先必须是有效的(可显示的)。我经常看到的一个常见错误是人们在创建新窗口时使用
requestFocus
或 requestFocusInWindow
,但在该窗口实际显示在屏幕上之前(setVisible
不保证窗口会立即显示)可见,只是在未来的某个时间它会变得可见)。
在这种情况下,最好的方法是使用
WindowListener
并监视 windowOpened
事件。那么,我很想使用 SwingUtilities#invokeLater
来确保窗口实际上可以显示在屏幕上。
另一个问题是依赖
isDisplayable
。即使组件所在的窗口不在(显示在屏幕上),此方法也可能返回true
。
一个组件在连接到本地屏幕资源时变得可显示。当它的祖先窗口被打包或可见时,就会发生这种情况……事实上,我发现很难准确确定何时会发生这种情况。
更新
我还应该补充一点,
requestFocus
就是这样,一个“请求”。焦点管理子系统可能会否决请求,因为另一个字段拒绝放弃焦点(例如当字段InputVerifier#shouldYieldFocus
返回false
时)
找到问题的答案。
MadProgrammer的分析是正确的,但是问题有点不相关,setVisible返回dialog后有一个线程sleep了2秒。
消除这种睡眠使问题停止发生。
现在...至于为什么延迟 2 秒(在 setVisible 返回后在父面板中)会很重要有点神秘,但至少我知道解决方案
我为 requestFocus() 创建了一个扩展函数
fun EditText.showKeyboardOnLayout() {
val mEditText = this
viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
viewTreeObserver.removeOnGlobalLayoutListener(this)
mEditText.apply {
if (isShown && isFocusable && isEnabled && requestFocus()) {
// The EditText is visible, focusable, enabled, and has received focus
showKeyboardWithRetry()
}
}
}
private fun showKeyboardWithRetry() {
val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT)
val isKeyboardShown = inputMethodManager.isActive(mEditText)
Timber.d("keyboard is shown: $isKeyboardShown")
if (!isKeyboardShown) {
postDelayed({
inputMethodManager.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT)
}, 100)
}
}
})
}
像这样使用:
binding.phoneEditText.showKeyboardOnLayout()