Jtree: Why does she lose focus when I edit a knot?

Asked

Viewed 58 times

3

I am developing a PJC (Oracle Forms) component. I’m making a bean with a jtree inside it.
In a standard java application, it works well, but in PJC there is a strange behavior with the focus.
When I press F2 or a click long 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 it happens and how to avoid.
I tried to generate a log of the events of focus:

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?

1 answer

1

I managed to solve.

Remembering that normally there is this problem, happened to be 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 overwrote the prepareForEditing method, forcing you to take the 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();
            }
        });
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.