customizeRenderer for checkbox tree
Hi, I am trying to add a checkbox tree with checked tree nodes. For which i need to have a customize renderer
I can get the normal text as in the strings I specefied with the HTML tags. But how to add the JLabels as it is? So that The text wraps around stuff.
CheckboxTree.CheckboxTreeCellRenderer renderer = new CheckboxTree.CheckboxTreeCellRenderer() {
@Override
public void customizeRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
if (!(value instanceof DefaultMutableTreeNode)) return;
value = ((DefaultMutableTreeNode)value).getUserObject();
if (value instanceof JLabel) {
JLabel template = (JLabel)value;
Color fgColor = JBColor.BLUE;
getTextRenderer().append(template.getText(), new SimpleTextAttributes(SimpleTextAttributes.STYLE_PLAIN, fgColor));
String description = "description";
if (StringUtil.isNotEmpty(description)) {
getTextRenderer().append(" (" + description + ")", SimpleTextAttributes.GRAY_ATTRIBUTES);
}
}
}
};
CheckedTreeNode root = new CheckedTreeNode(null);
checkboxTree = new CheckboxTree(renderer,root);
//checkboxTree.getRootPane().add(root);
checkboxTree.setRootVisible(true);
DefaultMutableTreeNode parentNode = new DefaultMutableTreeNode("text found");
for(Map.Entry<String,String> myObj : map.entrySet()) {
JCheckBox label = new JCheckBox("<html>"+myObj.getKey()+"</html>");
label.setOpaque(true);
JLabel label2 = new JLabel("<html>"+myObj.getValue()+"</html>");
CheckedTreeNode newChild = new CheckedTreeNode(label);
CheckedTreeNode another = new CheckedTreeNode(label2);
newChild.setChecked(true);
another.setChecked(true);
newChild.setEnabled(false);
parentNode.add(newChild);
parentNode.add(another);
root.add(parentNode);
}
Please sign in to leave a comment.
I am not sure I completely understood your question, but why avoiding
wrapping doen't work for you ?
Btw what is the point of using Component instances as user data object
of the tree ?
On 10/9/2015 4:38 PM, preetam batchu wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
Hi, thanks for the reply.
Using the code, I am able to use the text renderer and add normal plain text to the tree. So the text is left unwrapped when the Frame is resized and I want to add some swing components to the tree. Is there a way to do that? adding JLabel to the checkbox tree?
Is there an implementation for word wrapping seperately?
Since I have to update the frame on button click, I used the components that way.
Thanks.