> I want a tree (instance of JTree) , his rows havent got any icons and
> his nodes are editable (after 3 mouseclicks)
[quoted text clipped - 4 lines]
> root.add(new DefaultMutableTreeNode("subroot"));
> DefaultTreeModel model = new DefaultTreeModel(root);
It would have been minimal extra work to make this code compilable.
Wrapping lines can make code illegal.
> JTree tree = new JTree(model);
> tree.setEditable(true);
[quoted text clipped - 4 lines]
> renderer.setClosedIcon(null);
> renderer.setOpenIcon(null);
Incorrect. Renderers are assumed stateless, yet you change its settings
after having it installed on the tree. Therefore initially the widths
are calculated incorrectly (because that is done before the changing of
the icons).
DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
renderer.setLeafIcon(null);
renderer.setClosedIcon(null);
renderer.setOpenIcon(null);
tree.setCellRenderer(renderer);
The real problem is a) some profound hackery in DefaultTreeCellEditor
in calculating the preferred size, b) its assumption that isCellEditable
is always called. (This is fixed in 1.4.2, at least).
Fix:
tree.setCellRenderer(new DefaultTreeCellEditor2(tree, renderer));
with
class DefaultTreeCellEditor2
extends DefaultTreeCellEditor
{
public DefaultTreeCellEditor2
(JTree tree, DefaultTreeCellRenderer renderer)
{
super(tree, renderer);
}
public Component getTreeCellEditorComponent
(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row)
{
Component result = super.getTreeCellEditorComponent
(tree, value, selected, expanded, leaf, row);
prepareForEditing();
return result;
}
}
Christian
Joana - 22 Mar 2004 11:26 GMT
hi Christian,
thank you for answer! it helps to understand me something about
TreeCellRenderer.
But the error in my program was.... I used java1.3 and program didnt
work, how I wanted. The solution is java1.4.
regards,
Joana