【发布时间】:2013-03-07 17:05:43
【问题描述】:
下面的代码应该在任何时候按下 Enter 键后将焦点转移到下一个控件,事件触发但 .transferFocus 没有转移焦点,可能是什么问题? 谢谢
//JSpinner Creation Code:
private javax.swing.JSpinner edtStockMax;
edtStockMax = new javax.swing.JSpinner();
edtStockMax.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));
//Code to bind the Enter key
JSpinnerField1.getActionMap().put("enter-action", new AbstractAction("enter-action")
{
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("Transfer focus inside JSpinner");
field.transferFocus();
}
});
JSpinnerField1.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
.put(KeyStroke.getKeyStroke("ENTER"), "enter-action");
【问题讨论】:
-
如果微调器未处于编辑器模式,则编辑器不可见,因此不会接收事件。我猜你还需要对 JSpinner 对象进行键绑定。
-
你试过
requestFocusInWindow()(见this例子)vstransferFocus()吗? -
在编辑器模式下,上面的代码除了“transferFocus()”这个过程之外,它运行没有错误,但没有将焦点转移到下一个控件,焦点停留在JSpinner上.
-
我不知道如何使用 requestFocusInWindow(),我需要在 JSpinner 之后获得下一个控件才能做到这一点?如果 transferFocus() 工作起来会容易得多。
标签: java swing events focus jspinner