- Code: Select all
CheckBoxTree tree = new CheckBoxTree(model);
tree.setCheckBoxEnabled(false);
I was able to work around this by extending CheckBoxTree, and overriding createCellRenderer to create a custom subclass of CheckBoxTreeCellRenderer with an overridden getTreeCellRendererComponent() method (see below). In my override, I skip the call to _checkBox.setEnabled(enabled).
I have two questions:
1) Is there a better way to do this? This seems like it's embedding an awful lot of the implementation of CheckBoxTree in my code.
2) Are there any oddball side effects of not disabling the checkbox that I should look out for? I haven't found any yet.
Thanks!
- Code: Select all
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
_checkBox.setPreferredSize(new Dimension(_protoType.getPreferredSize().width, 0));
_emptyBox.setPreferredSize(new Dimension(_protoType.getPreferredSize().width, 0));
applyComponentOrientation(tree.getComponentOrientation());
TreePath path = tree.getPathForRow(row);
if (path != null && tree instanceof CheckBoxTree) {
CheckBoxTreeSelectionModel selectionModel = ((CheckBoxTree) tree).getCheckBoxTreeSelectionModel();
if (selectionModel != null) {
boolean enabled = tree.isEnabled() && ((CheckBoxTree) tree).isCheckBoxEnabled() && ((CheckBoxTree) tree).isCheckBoxEnabled(path);
if (!enabled && !selected) {
if (getBackground() != null) {
setForeground(getBackground().darker());
}
}
//_checkBox.setEnabled(enabled); //COMMENTING OUT THIS LINE is the significant change
updateCheckBoxState(_checkBox, path, selectionModel);
}
}
if (_actualTreeRenderer != null) {
JComponent treeCellRendererComponent = (JComponent) _actualTreeRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
Border border = treeCellRendererComponent.getBorder();
setBorder(border);
treeCellRendererComponent.setBorder(BorderFactory.createEmptyBorder());
if (path == null || !(tree instanceof CheckBoxTree) || ((CheckBoxTree) tree).isCheckBoxVisible(path)) {
remove(_emptyBox);
add(_checkBox, BorderLayout.BEFORE_LINE_BEGINS);
}
else {
remove(_checkBox);
add(_emptyBox, BorderLayout.AFTER_LINE_ENDS); // expand the tree node size to be the same as the one with check box.
}
add(treeCellRendererComponent);
}
return this;
}