Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2004

Tip: Looking for answers? Try searching our database.

Editable JTree & no Icons  on his nodes

Thread view: 
Joana - 19 Mar 2004 17:38 GMT
Hello,

please help... I need it...

I want a tree (instance of JTree) , his rows havent got any icons and
his nodes are editable (after 3 mouseclicks)
I made it so:

DefaultMutableTreeNode root = new DefaultMutableTreeNode("only one
node");
root.add(new DefaultMutableTreeNode("subroot"));
DefaultTreeModel model = new DefaultTreeModel(root);

JTree tree = new JTree(model);
tree.setEditable(true);

DefaultTreeCellRenderer renderer
=(DefaultTreeCellRenderer)tree.getCellRenderer();
renderer.setLeafIcon(null);
renderer.setClosedIcon(null);
renderer.setOpenIcon(null);

JFrame f = new JFrame();
f.getContentPane().add(tree);
f.setSize(300, 400);
f.show();

When I do 3-mouseclick on a node, node runs off.... When I write
renderer.setXXXIcon(null) as comments,
run the programm corectly.

Can you say, what ist the problem ??
thanks a lot!
best regards,
Joana
Christian Kaufhold - 19 Mar 2004 19:08 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.