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

3

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 a strange behavior occurs with the focus.
When I press F2 or a long click to edit a tree node, the editor loses focus. So instead of just starting typing the new value, I have to manually click on the editor and only then can I change the value. I do not know why this happens and how to avoid it. I tried to generate a log of focus 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?

    
asked by anonymous 30.03.2015 / 14:26

1 answer

1

I managed to resolve.

Remembering that there is usually no such problem, it happened to be inside a VBean (Container 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 overwritten 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();
            }
        });
    }
}
    
30.03.2015 / 16:22