java – JTree: Why does it lose focus when I edit a node?

Question:

I'm developing a PJC (Oracle Forms) component. I'm creating a Bean with a JTree inside it.
In a standard java application, it works fine, but in PJC there is a strange behavior with focus.
When I press F2 or long click to edit a tree node, the editor loses focus. So instead of just starting to type in the new value, I have to manually click on the editor and only then can I change the value.
I don't know why this happens and how to avoid it.
Tried to generate a log of hover events:

Tree focusLost e.getOppositeComponent: org.jdesktop.swingx.tree.DefaultXTreeCellEditor$XEditorContainer
Tree focusLost e.paramString: FOCUS_LOST,permanent,opposite=org.jdesktop.swingx.tree.DefaultXTreeCellEditor$XEditorContainer[,38,160,100x16]
Tree focusLost e.getSource: org.jdesktop.swingx.JXTree

Does anyone know how to prevent the editor from losing focus?

Answer:

I managed to resolve it.

Remembering that this problem usually doesn't exist, it happened because it was inside a VBean (Containter PJC/Oracle Forms).

I needed to create an editor, I used JTextField:

this.textField = new JTextField();
this.textField.setBorder(BorderFactory.createLineBorder(Color.red, 1));
this.cellEditor = new DefaultCellEditor(this.textField);
this.tree.setCellEditor(new treeCellEditor(this.tree, this.cellRenderer, this.cellEditor));        

In my editor class, I overridden the prepareForEditing method, forcing it to take focus.

private class treeCellEditor extends DefaultTreeCellEditor {
    public treeCellEditor(JTree tree, DefaultTreeCellRenderer renderer, TreeCellEditor editor) {
        super(tree, renderer, editor);
    }
    public treeCellEditor(JTree tree, DefaultTreeCellRenderer renderer) {
        super(tree, renderer);
    }
    protected void prepareForEditing() {
        super.prepareForEditing();
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                textField.requestFocusInWindow();
                textField.grabFocus();
                textField.selectAll();
            }
        });
    }
}
Scroll to Top