my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Can I add a checkbox in front of a tree node? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Can I add a checkbox in front of a tree node?" Watch "Can I add a checkbox in front of a tree node?" New topic
Author

Can I add a checkbox in front of a tree node?

Sanjay Bahrani
Ranch Hand

Joined: Apr 17, 2001
Posts: 38
Hi There,
Can i add a checkbox in front of a tree node if yes plz reply mew as early as possible?
Thankz.
zoe goddard
Ranch Hand

Joined: Dec 12, 2000
Posts: 74
Yes, Somewhere in the code guru website there's an example. I'm not sure where it is, I find it by doing a web search on JTree.
David Lu
Greenhorn

Joined: Aug 01, 2000
Posts: 10
Yes. There may be better ways to do this, but I just created by own renderer for the tree and added a boolean to the node's object (NodeInfo). I also used an icon that looks like a selected checkbox (selected.gif) and one that looks like an unselected checkbox (uinselected.gif). The advantage of doing this is that you can create tri-state checkboxes by simply having another icon and some logic to know when to use it (i.e., several items are highlighted with some being selected as some not).
class MyRenderer extends DefaultTreeCellRenderer
{
public MyRenderer() {}
public Component getTreeCellRendererComponent(
JTree tree,
Object value,
boolean sel,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus)
{
super.getTreeCellRendererComponent(
tree, value, sel,
expanded, leaf, row,
hasFocus);
DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
Object tempNode = node.getUserObject();
if (tempNode instanceof String)
{
return this;
}
NodeInfo nodeInfo = (NodeInfo)(node.getUserObject());
if (nodeInfo.getSelected())
{
setIcon(new ImageIcon("selected.gif"));
}
else
{
setIcon(new ImageIcon("unselected.gif"));
}
return this;
}
}
Sanjay Bahrani
Ranch Hand

Joined: Apr 17, 2001
Posts: 38
Hi there,
Thankz for replying me David, But i want to add a awt/swing component (JCheckBox/Checkbox) near the node of a tree. Can you suggest me some more things about the same.
Regards.
Sanjay Bahrani
Ranch Hand

Joined: Apr 17, 2001
Posts: 38
Originally posted by David Lu:
[B]Yes. There may be better ways to do this, but I just created by own renderer for the tree and added a boolean to the node's object (NodeInfo). I also used an icon that looks like a selected checkbox (selected.gif) and one that looks like an unselected checkbox (uinselected.gif). The advantage of doing this is that you can create tri-state checkboxes by simply having another icon and some logic to know when to use it (i.e., several items are highlighted with some being selected as some not).
class MyRenderer extends DefaultTreeCellRenderer
{
public MyRenderer() {}
public Component getTreeCellRendererComponent(
JTree tree,
Object value,
boolean sel,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus)
{
super.getTreeCellRendererComponent(
tree, value, sel,
expanded, leaf, row,
hasFocus);
DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
Object tempNode = node.getUserObject();
if (tempNode instanceof String)
{
return this;
}
NodeInfo nodeInfo = (NodeInfo)(node.getUserObject());
if (nodeInfo.getSelected())
{
setIcon(new ImageIcon("selected.gif"));
}
else
{
setIcon(new ImageIcon("unselected.gif"));
}
return this;
}
}

Hi David,
As per your code, I have worked on it it adds a image instead of awt/swing component. I am looking for the functionality in such a way that a Checkbox/JCheckBox is there in front of a tree node.
I have a sample code that I am working on it.....
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
public class MyRenderer extends JCheckBox implements TreeCellRenderer{
public Component getTreeCellRendererComponent(JTree tree,Object
value,boolean selected,boolean expanded,boolean leaf,int row,boolean hasFocus) {
setEnabled(tree.isEnabled());
//setSelected(((SelectableItem)value).isSelected());
//setSelected(((Boolean)value).booleanValue());
setText(value.toString());
return this;
}
}
we will call this class by using ...
tree.setCellRenderer(new MyRenderer());
If you have further queries plz. ask me....
Regards.
Craig Snowbarger
Ranch Hand

Joined: Mar 07, 2001
Posts: 38
Sanjay,
I found this page on checkboxes in JTrees that may be helpful to you. http://www2.gol.com/users/tame/swing/examples/JTreeExamples2.html
Regards,
Craig Snowbarger
Sanjay Bahrani
Ranch Hand

Joined: Apr 17, 2001
Posts: 38
Hello Craig,
Thankz for giving me the meaningful Reply, it makes my work easy thats why I love to post the questions on this informative site....
Thankz....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can I add a checkbox in front of a tree node?