当我调用时component.requestFocusInWindow()
,Swing 将异步FOCUS_GAINED
和FOCUS_LOST
事件排入队列,而不是同步转移焦点。作为一种解决方法,似乎DefaultKeyboardFocusManager
正在尝试通过延迟键盘事件的调度直到焦点事件完成调度来模拟同步切换焦点。但这似乎无法正常工作。
问题:有什么方法可以在 Swing 中同步改变焦点?真的是DefaultKeyboardFocusManager
在尝试模拟同步焦点,是否存在严重错误?是否有正确执行此操作的焦点管理器?
动机:我有一个JTextField
当它充满时会自动转移焦点的东西。在使用 编写集成测试java.awt.Robot
时,我需要它的行为是确定性的,而不是依赖于时间的。
这是一个演示。预期行为:当您按住一个键时,每个键JTextField
将包含一个字符。实际行为:焦点变化不够快,因此它们获得了多个字符。
更新:请注意,此行为并非特定于程序更改焦点。在第三个示例中,我取消了自定义焦点调用,而是简单地将文档更新延迟设置为 500 毫秒。然后我输入了1Tab2Tab3Tab4Tab5Tab6Tab7Tab8Tab9Tab0. 如您所见,数字已正确排队,但制表键被丢弃。
import java.awt.*;
import java.awt.event.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.*;
import javax.swing.*;
import javax.swing.GroupLayout.Group;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class AwtEventListenerDemo {
public static final Logger logger = Logger.getLogger(AwtEventListenerDemo.class.getName());
private static String keyEventToString(KeyEvent keyEvent) {
int id = keyEvent.getID();
String eventName =
id == KeyEvent.KEY_PRESSED ? "key_pressed" :
id == KeyEvent.KEY_TYPED ? "key_typed" :
id == KeyEvent.KEY_RELEASED? "key_released" : "unknown " + id;
String what = id == KeyEvent.KEY_TYPED ? "" + keyEvent.getKeyChar() : "#" + keyEvent.getKeyCode();
String componentString = keyEvent.getComponent().getName();
if (componentString == null) componentString = keyEvent.getComponent().toString();
return String.format("%12s %4s on %s", eventName, what, componentString);
}
private static String focusEventToString(FocusEvent focusEvent) {
int id = focusEvent.getID();
String eventName = id == FocusEvent.FOCUS_GAINED ? "focus_gained" :
id == FocusEvent.FOCUS_LOST ? "focus_lost" :
null;
if (eventName == null) return focusEvent.toString();
String componentString = focusEvent.getComponent().getName();
if (componentString == null) componentString = focusEvent.getComponent().toString();
return String.format("%12s on %s", eventName, componentString);
}
private final AWTEventListener loggingListener = new AWTEventListener() {
@Override public void eventDispatched(AWTEvent event) {
if (event instanceof KeyEvent) {
KeyEvent keyEvent = (KeyEvent) event;
int id = keyEvent.getID();
if (id == KeyEvent.KEY_PRESSED && keyEvent.getComponent() instanceof JTextField && ((JTextField)keyEvent.getComponent()).getDocument().getLength() == 1) {
EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
ArrayList<AWTEvent> inQueue = new ArrayList<AWTEvent>();
int[] interestingIds = new int[] {KeyEvent.KEY_PRESSED, KeyEvent.KEY_TYPED, KeyEvent.KEY_RELEASED, FocusEvent.FOCUS_GAINED, FocusEvent.FOCUS_LOST};
for (int i: interestingIds) {
AWTEvent peek = eventQueue.peekEvent(i);
if (peek != null)
inQueue.add(peek);
}
ArrayList<String> inQueueString = new ArrayList<String>();
for (AWTEvent peek: inQueue) {
if (peek instanceof KeyEvent) {
inQueueString.add(keyEventToString((KeyEvent) peek));
} else if (peek instanceof FocusEvent) {
inQueueString.add(focusEventToString((FocusEvent) peek));
}
}
logger.info(String.format("Still in the queue (in no particular order): %s", inQueueString));
}
logger.info(keyEventToString(keyEvent));
} else {
logger.info(event.toString());
}
}
};
private JFrame jframe;
public void init() {
long mask = AWTEvent.KEY_EVENT_MASK; // AWTEvent.MOUSE_EVENT_MASK |
Toolkit.getDefaultToolkit().addAWTEventListener(loggingListener, mask);
if (jframe == null) jframe = new JFrame(AwtEventListenerDemo.class.getSimpleName());
SwingUtilities.invokeLater(new Runnable() {
@Override public void run() {
initUI();
}
});
}
public void cleanupForRestart() {
Toolkit.getDefaultToolkit().removeAWTEventListener(loggingListener);
}
public void destroy() {
cleanupForRestart();
jframe.setVisible(false);
jframe = null;
}
public void initUI() {
GroupLayout groupLayout = new GroupLayout(jframe.getContentPane());
jframe.getContentPane().removeAll();
jframe.getContentPane().setLayout(groupLayout);
JButton jbutton = new JButton(new AbstractAction("Restart") {
private static final long serialVersionUID = 1L;
@Override public void actionPerformed(ActionEvent e) {
cleanupForRestart();
init();
}
});
groupLayout.setAutoCreateGaps(true);
groupLayout.setAutoCreateContainerGaps(true);
Group verticalGroup = groupLayout.createSequentialGroup()
.addComponent(jbutton);
Group horizontalGroup = groupLayout.createParallelGroup()
.addComponent(jbutton);
groupLayout.setVerticalGroup(verticalGroup);
groupLayout.setHorizontalGroup(horizontalGroup);
for (int i = 0; i < 10; i++) {
final JTextField jtextfield = new JTextField();
jtextfield.setName(String.format("JTextField %d", i));
verticalGroup.addComponent(jtextfield);
horizontalGroup.addComponent(jtextfield);
boolean useDocumentListener = true;
final boolean useFocusRootAncestor = false;
if (useDocumentListener) {
DocumentListener listener = new DocumentListener() {
@Override public void removeUpdate(DocumentEvent e) { }
@Override public void insertUpdate(DocumentEvent e) {
// Simulate a slow event listener. When the listener is
// slow, the problems get worse.
try {
Thread.sleep(50);
} catch (InterruptedException e1) {
logger.warning(e1.toString());
}
if (e.getDocument().getLength() > 0) {
// These two methods of transferring focus appear
// equivalent.
if (useFocusRootAncestor) {
Container focusRoot = jtextfield.getFocusCycleRootAncestor();
FocusTraversalPolicy policy = focusRoot.getFocusTraversalPolicy();
Component nextComponent = policy.getComponentAfter(focusRoot, jtextfield);
nextComponent.requestFocusInWindow();
} else {
KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent(jtextfield);
}
}
}
@Override public void changedUpdate(DocumentEvent e) { }
};
jtextfield.getDocument().addDocumentListener(listener);
}
}
if (!jframe.isVisible()) {
jframe.pack();
jframe.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
jframe.setVisible(true);
}
}
public static void main(String[] argv) {
// Use a single-line console log handler.
LogManager.getLogManager().reset();
Formatter formatter = new Formatter() {
private SimpleDateFormat dateFormat = new SimpleDateFormat("kk:mm:ss.SSS");
@Override
public String format(LogRecord logRecord) {
Date date = new Date(logRecord.getMillis());
DateFormat.getTimeInstance().format(date);
return String.format("%s %s %s %s%n",
dateFormat.format(date),
logRecord.getLoggerName(),
logRecord.getLevel(),
logRecord.getMessage());
}
};
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setFormatter(formatter);
consoleHandler.setLevel(Level.FINEST);
logger.addHandler(consoleHandler);
Logger focusLogger = Logger.getLogger("java.awt.focus.DefaultKeyboardFocusManager");
focusLogger.setLevel(Level.FINEST);
focusLogger.addHandler(consoleHandler);
final AwtEventListenerDemo demo = new AwtEventListenerDemo();
demo.init();
}
}