I think setting your own renderer should do the job just fine. In this
example I use the value as label and get the checkbox selection state
from outside (here isSelected(int) ) - but I guess there are a lot of
ways to do this, depending on your data model.

public class JCheckboxTree extends JTree {

   public JCheckboxTree() {
       setCellRenderer(new TreeCellRenderer(){
           JCheckBox box = new JCheckBox();
           public Component getTreeCellRendererComponent(JTree tree,
Object value, boolean selected, boolean expanded, boolean leaf, int
row, boolean hasFocus) {
               box.setText(value.toString());
               box.setSelected(isSelected(row));
               box.setBackground(selected ? Color.blue : Color.white);
               return box;
           }
       });
   }

   boolean isSelected(int row){
       return row%2==0;
   }

   public static void main(String[] args) {
       JFrame f = new JFrame();
       f.getContentPane().add(new JScrollPane(new JCheckboxTree()));
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       f.pack();
       f.setVisible(true);
   }
}

Hope it helps,

Thasso

On Oct 5, 6:33 am, Shooter <[email protected]> wrote:
> I'm looking for some help writing a JTree that uses.  I've found
> limited examples on the web, but most are copyrighted and all seem to
> use very different implementation approaches.
>
> Does anyone have links or ideas for creating a JTree with nothing more
> than a check box and a label for each node in the tree.  I've been
> able to override the renderer to use a simple JCheckBox(String)
> constructor but I can't get the check boxes to actually select and
> deselect.  Any help at all would be greatly appreciated.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to