Sagewire Logo

How to make a JTree leaf look like a folder

6 Message(s) by 4 Author(s) originally posted in java help


From: Rita Leon Date:   Thursday, October 18, 2007
Here is my very simple code:

JAVAx.swing.JTree tree;

...

DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode cat = new DefaultMutableTreeNode("Category");
root.add(cat);
DefaultTreeModel dataRoot = new DefaultTreeModel(root);
tree.setModel(dataRoot);The problem is the icon for "cat" shows as the sheet of paper icon.
How do I get JTree to display "cat"'s icon as the folder icon like
that of "root" without adding a child or changing the graphics?

ty


From: Steve W. Jackson Date:   Thursday, October 18, 2007
In article <rt0fh39l363c0ogu01bakoh3b24hf4fmpv@xxxxxxxxxxx>,
wrote in message:

Here is my very simple code:
JAVAx.swing.JTree tree;
...
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode cat = new DefaultMutableTreeNode("Category");
root.add(cat);
DefaultTreeModel dataRoot = new DefaultTreeModel(root);
tree.setModel(dataRoot);
The problem is the icon for "cat" shows as the sheet of paper icon.
How do I get JTree to display "cat"'s icon as the folder icon like
that of "root" without adding a child or changing the graphics?
ty



Depends what you mean by "changing the graphics"...

Take a look at the tree's cell renderer. By default, it uses a
DefaultTreeCellRenderer. As you have seen, adding a child automatically
results in using the folder icon. But if you want to change that
behavior without adding a child, you *must* make some kind of graphic
change. At its simplest, you can use a small class extending the
DefaultTreeCellRenderer and override getTreeCellRendererComponent so
that the renderer uses the folder on a leaf node .

= Steve =
--
Steve W. Jackson
Montgomery, Alabama


From: Daniel Dyer Date:   Thursday, October 18, 2007
On Thu, 18 Oct 2007 18:44:45 +0100, Steve W. Jackson =

wrote in message:

In article <rt0fh39l363c0ogu01bakoh3b24hf4fmpv@xxxxxxxxxxx>,
wrote in message:
Here is my very simple code:

JAVAx.swing.JTree tree;

...

DefaultMutableTreeNode root =3D new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode cat =3D new DefaultMutableTreeNode("Category")=
;
root.add(cat);
DefaultTreeModel dataRoot =3D new DefaultTreeModel(root);
tree.setModel(dataRoot);


The problem is the icon for "cat" shows as the sheet of paper icon.
How do I get JTree to display "cat"'s icon as the folder icon like
that of "root" without adding a child or changing the graphics?

ty
Depends what you mean by "changing the graphics"...
Take a look at the tree's cell renderer. By default, it uses a
DefaultTreeCellRenderer. As you have seen, adding a child automatically=

results in using the folder icon. But if you want to change that
behavior without adding a child, you *must* make some kind of graphic
change. At its simplest, you can use a small class extending the
DefaultTreeCellRenderer and override getTreeCellRendererComponent so
that the renderer uses the folder on a leaf node.
=3D Steve =3D



Alternatively, you can use UIDefaults to change the default icons for a =
=

JTree. If you print out all of the properties you will find the ones you =
=

need, just set the leaf node property to be the same as the non-leaf one=
.

Dan.

-- =

Daniel Dyer
http//www.uncommons.org


From: Steve W. Jackson Date:   Thursday, October 18, 2007
In article <op.t0enb5ul8kxvgr@xxxxxxxxxxx>,
wrote in message:

[ snip ]

Alternatively, you can use UIDefaults to change the default icons for a
JTree. If you print out all of the properties you will find the ones you
need, just set the leaf node property to be the same as the non-leaf one.
Dan.



That's even easier than a renderer -- neat. It'd apply to all JTree
uses throughout an application, I presume. But if that's not an issue,
it's simpler.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama


From: bcd Date:   Thursday, October 18, 2007
In article <rt0fh39l363c0ogu01bakoh3b24hf4fmpv@xxxxxxxxxxx>,
wrote in message:
Here is my very simple code:
JAVAx.swing.JTree tree;
...
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode cat = new DefaultMutableTreeNode("Category");
root.add(cat);
DefaultTreeModel dataRoot = new DefaultTreeModel(root);
tree.setModel(dataRoot);
The problem is the icon for "cat" shows as the sheet of paper icon.
How do I get JTree to display "cat"'s icon as the folder icon like
that of "root" without adding a child or changing the graphics?



The following might work:

DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode cat = new DefaultMutableTreeNode("Category");
root.add(cat);
DefaultTreeModel dataRoot = new DefaultTreeModel(root)
{
public boolean isLeaf(Object node) { return false; }
}
tree.setModel(dataRoot);

Cheers
Bent D
--
Bent Dalager - bcd@xxxxxxxxxxx - http://www.pvv.org/~bcd
powered by emacs


From: bcd Date:   Thursday, October 18, 2007
In article <ff8ch3$2t2$1@xxxxxxxxxxx>,
wrote in message:
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode cat = new DefaultMutableTreeNode("Category");
root.add(cat);
DefaultTreeModel dataRoot = new DefaultTreeModel(root)
{
public boolean isLeaf(Object node) { return false; }
}



Add a semicolon here I suppose.

tree.setModel(dataRoot);



Cheers again,
Bent D.
--
Bent Dalager - bcd@xxxxxxxxxxx - http://www.pvv.org/~bcd
powered by emacs



Next Message: Run VBS within JAVA Application


Blogs related to How to make a JTree leaf look like a folder

click here python untrusted interpreter delicate sound of thunder ...
... baja laboral por embarazo gentiment je te viol tab saveurs du maquis cheese sniper m82a2 yellow slugs importance of knowing etymology lo que realizo manuel avila camacho en la educacion good friday peace treaty juegos en java para ...

Home of the funny and useful products.
beautifully and wonderfully made yahoo.com.uk medway kent police image process irs liss 3with classfication blue native page doghead brewery what does greek oregano look like? malt shovel brewery morah ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional