I rendered a check box node tree.
The renderer renders the parent nodes with a (check box + folder like icon ) and the leaf nodes as (Only check box) .
I have rendered it and now i want to make it editable . (i.e) when i click it , the check boxes must be checked and unchecked .
I tried writing an editor . But i am not clear as to how to write it . Please guide me as to how to accomplish this .
Many thanks in advance .
I have built the tree from a Vector . The vector is called NamedVector and it contains Parent node objects . The parent node object holds the leaf nodes . The leaf nodes are of type CheckBoxNode.
public class CheckBoxNodeRenderer implements TreeCellRenderer{
NonLeafRenderer nonLeafRenderer = new NonLeafRenderer();
protected JCheckBox check;
protected JLabel label;
public JPanel panel;
CheckBoxNode checkNode;
public JCheckBox getLeafRenderer()
{
return leafRenderer;
}
public CheckBoxNodeRenderer()
{
panel = new JPanel();
panel.setLayout(new BorderLayout());
check = new JCheckBox();
label = new JLabel();
Font fontValue;
fontValue = UIManager.getFont("Tree.font");
if (fontValue != null) {
leafRenderer.setFont(fontValue);
}
Boolean booleanValue = (Boolean) UIManager
.get("Tree.drawsFocusBorderAroundIcon");
leafRenderer.setFocusPainted((booleanValue != null)
&& (booleanValue.booleanValue()));
selectionBorderColor = UIManager.getColor("Tree.selectionBorderColor");
selectionForeground = UIManager.getColor("Tree.selectionForeground");
selectionBackground = UIManager.getColor("Tree.selectionBackground");
textForeground = UIManager.getColor("Tree.textForeground");
textBackground = UIManager.getColor("Tree.textBackground");
}
///////////////////
/**
* Approach by returning a panel .
*/
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean isSelected, boolean expanded,
boolean leaf, int row, boolean hasFocus) {
String stringValue = tree.convertValueToText(value, isSelected,
expanded, leaf, row, hasFocus);
panel.setEnabled(true);
if(leaf){
if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
checkNode = (CheckBoxNode)node.getUserObject();
check.setSelected(checkNode.isSelected());
label.setFont(tree.getFont());
label.setText(value.toString());
label.setIcon(null);
panel.removeAll();
panel.add(check,BorderLayout.WEST);
panel.add(label);
panel.setVisible(true);
}
}
else if(!leaf){
if ((value != null) && (value instanceof DefaultMutableTreeNode) ) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
Object parent = (Object)node.getUserObject();
System.err.println(parent.toString());
NamedVector parentNode = (NamedVector) parent;
check.setSelected(parentNode.isSelected());
label.setFont(tree.getFont());
label.setText(parentNode.toString());
label.setIcon(UIManager.getIcon("Tree.openIcon"));
panel.removeAll();
panel.add(check,BorderLayout.WEST);
panel.add(label);
panel.setVisible(true);
}
}
return panel;
}
The issue is that you’re not listening for the events. In your getTreeCellRendererComponent method, create a listener and then make sure to tell the tree model that you’ve changed a node via the nodeChanged method. The following should work (you might have to make some variables final to use them in the inner class though):