• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Sometimes you feel like a nut. Sometimes you feel like a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic