Cross-posted: https://forums.oracle.com/forums/thread.jspa?threadID=2512538&tstart=0
Hello, everybody.
Is there a way to flush pending AWT events programmatically? I would like to be able to use it in a code like this:
if (comp.requestFocusInWindow())
{
// flush pending AWT events...
KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
if (focusManager.getPermanentFocusOwner() == comp)
{
// do something...
}
}
Thank you.
Marcos
PS: I know I can use a FocusListener in comp, but it is not an option in my case.
UPDATE:
The essence of my problem is this: to focus the actual editor componente of a JTable before the key that starts the editing is dispatched to it. So I came out with this solution:
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;
}
The class TextFieldColumnEditor is the componente I use in a custom cell editor. As you can see the solution is not perfect for a lot of reasons:
- I had to copy the processKeyBinding code from JComponent and change it.
- I don't know if SwingUtilities.notifyAction had success, because it is executed in another block of code and I can't return it from the processKeyBinding method. I assume it had. That's why I needed some kind of synchronous focus handling.
I'm sticking with this solution for the time being. Your comments and suggestions would be appreciated.
Marcos
UPDATE 2
Final (simplified) version, after suggestion by @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;
}
}
Comments and improvements are appreciated.