Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sanjay Bahrani
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic