public void mouseClick(MouseEvent evt)
{
TreePath path=getJTree().getPathForLocation(evt.getX(),evt.getY());
getJTree().stopEditing();
if(path==null)
return;
DefaultMutableTreeNode node=(DefaultMutableTreeNode)path.getLastPathComponent();
if(evt.getClickCount()==2)
{
if(node.equals(root_guanjianci) || node.equals(root_guanxi) || node.equals(root_zitu) || node.equals(root_fujia))
{
getJTree().setEditable(false);
}
else
{
getJTree().setEditable(true);
getJTree().startEditingAtPath(path);
}
}
if(evt.getClickCount()==1)
{
getJTree().stopEditing();
getJTree().setEditable(false);
}
}
你是不是JTree的这个没设?
setInvokesStopCellEditing(true)
给你点代码参考下:ProjectTreeNodeObject 是我自定义的userObject用来保存节点的信息
public boolean stopCellEditing() {
ProjectTreeNodeObject selectObject = ((ProjectTreeNodeObject)selectedNode.getUserObject());
String oldValue = selectObject.getNameWithoutExtension();
String editorValue = getCellEditorValue().toString();
if (selectObject.updateName(editorValue) && super.stopCellEditing()) {
fFileTree.setEditable(false);
selectObject.setAbsolutePath(editorValue);
fFileTree.getModel().valueForPathChanged(new TreePath(selectedNode), selectObject);
return true;
} else {
delegate.setValue(oldValue);
return false;
}
}