1

交叉发布:https ://forums.oracle.com/forums/thread.jspa?threadID=2512538&tstart=0

大家好。

有没有办法以编程方式刷新挂起的 AWT 事件?我希望能够在这样的代码中使用它:

if (comp.requestFocusInWindow())
{
    // flush pending AWT events...
    KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    if (focusManager.getPermanentFocusOwner() == comp)
    {
        // do something...
    }
}

谢谢你。

马科斯

PS:我知道我可以在 comp 中使用 FocusListener,但在我的情况下它不是一个选项。

更新:

我的问题的本质是:在开始编辑的键被分派给它之前集中 JTable 的实际编辑器组件。所以我想出了这个解决方案:

private class TextFieldColumnEditor extends MyCustomTextField
{
    // TODO
    private static final long serialVersionUID = 1L;

    private FocusListener _keyDispatcher;

    @Override
    protected boolean processKeyBinding(final KeyStroke ks, final KeyEvent e, int condition, boolean pressed)
    {
        InputMap bindings = getInputMap(condition);
        ActionMap actions = getActionMap();
        if (bindings != null && actions != null && isEnabled())
        {
            Object binding = bindings.get(ks);
            final Action action = binding == null ? null : actions.get(binding);
            if (action != null)
            {
                if (!isFocusOwner() && requestFocusInWindow())
                {
                    // In case something went wrong last time and the
                    // listener wasn't unregistered.
                    removeFocusListener(_keyDispatcher);

                    _keyDispatcher =
                         new FocusListener()
                         {
                             @Override
                             public void focusGained(FocusEvent evt)
                             {
                                 removeFocusListener(this);
                                 SwingUtilities.invokeLater(
                                     new Runnable()
                                     {
                                         @Override
                                         public void run()
                                         {
                                             SwingUtilities.notifyAction(action, ks, e, CampoTextoDadosEditorColuna.this, e.getModifiers());
                                         }
                                     }
                                 );
                             }

                             @Override
                             public void focusLost(FocusEvent e)
                             {
                             }
                         };

                    addFocusListener(_keyDispatcher);

                    return true;
                }
                else
                {
                    return SwingUtilities.notifyAction(action, ks, e, this, e.getModifiers());
                }
            }
        }
    return false;
}

TextFieldColumnEditor 类是我在自定义单元格编辑器中使用的组件。如您所见,由于很多原因,该解决方案并不完美:

  • 我不得不从 JComponent 复制 processKeyBinding 代码并进行更改。
  • 我不知道 SwingUtilities.notifyAction 是否成功,因为它是在另一个代码块中执行的,我无法从 processKeyBinding 方法返回它。我想它有。这就是为什么我需要某种同步焦点处理。

我暂时坚持使用这个解决方案。您的意见和建议将不胜感激。

马科斯

更新 2

在@VGR 建议后的最终(简化)版本:

private class CellEditorTextField extends JTextField
{
    @Override
    protected boolean processKeyBinding(final KeyStroke ks, final KeyEvent e, int condition, boolean pressed)
    {
        InputMap bindings = getInputMap(condition);
        ActionMap actions = getActionMap();
        if (bindings != null && actions != null && isEnabled())
        {
            Object binding = bindings.get(ks);
            final Action action = binding == null ? null : actions.get(binding);
            if (action != null)
            {
                if (e.getSource() == YourJTable.this && action.isEnabled() && requestFocusInWindow())
                {
                    SwingUtilities.invokeLater(
                        new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                SwingUtilities.notifyAction(action, ks, e, CellEditorTextField.this, e.getModifiers());
                            }
                        }
                    );
                    return true;
                }
                else
                {
                    return SwingUtilities.notifyAction(action, ks, e, this, e.getModifiers());
                }
            }
        }
        return false;
    }
}

评论和改进表示赞赏。

4

0 回答 0