相互投稿: 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: コンプで 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;
}
}
コメントと改善を歓迎します。