how can you make a Jtree that uses a DefaultTreeCellEditor stop editing when it loses focus to a component other than the
cell editor's DefaultTextField component ?
i had to use a custom TreeCellEditor and attach my focus listener to the DefaultTextField object and attach it also to the JTree object
DefaultTreeCellEditor celEditor = new DefaultTreeCellEditor(containerTree,renderer)
{
@Override
public Component getTreeCellEditorComponent(JTree tree, Object value,
boolean isSelected,
boolean expanded,
boolean leaf, int row) {
Component comp = super.getTreeCellEditorComponent(tree, value, isSelected, expanded, leaf, row);
boolean found= false;
for(FocusListener f:editingComponent.getFocusListeners())
if(focusListener.equals(f))
{
found=true;
break;
}
if(!found)
editingComponent.addFocusListener(focusListener);
return comp;
}
..
myTree.addFocusListener(focusListener);
in JTable there's a property that you switch on/off which does exactly that, is there anything alike for JTree ?
one more thing,after adding a new TreeNode to the JTree,i'm calling scrollPathToVisible(<path to the new node>),but for all the nodes i add except the first, the user object is not displayed,am i missing something? here's my code
ActionListener actionListener = new ActionListener(){
public void actionPerformed(ActionEvent ae){
DefaultTreeModel model = (DefaultTreeModel)containerTree.getModel();
int nodeCount=containerTree.getModel().getChildCount(root);
if(ae.getSource().equals(menuAdd))
{
DefaultMutableTreeNode newNode=new DefaultMutableTreeNode();
if(currentNode>0 && currentNode+1<nodeCount)
{
model.insertNodeInto(newNode, root, currentNode+1);
newNode.setUserObject("container #"+(currentNode+1));
}
else
{
model.insertNodeInto(newNode, root, root.getChildCount());
newNode.setUserObject("container #"+nodeCount);
}
TreePath path = new TreePath(newNode.getPath());
// only works properly for 1st node,for the rest i have to
// click on the new node to get the proper display
containerTree.scrollPathToVisible(path);
}
else if(nodeCount>=0 && currentNode!=0)
model.removeNodeFromParent((DefaultMutableTreeNode)model.getChild(root, currentNode-1));
}
};