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);
}

2 comments
Comment actions Permalink

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, I am trying to add a checkbox tree with checked tree nodes. For which i need to have a customize renderer

>

However all the implementations in the source code seems to be a bit overkill as I just need to display some options and get the checked data.
My code is (Only check boxes are visible and no text) How to do this?

>

CheckboxTree.CheckboxTreeCellRenderer renderer = new CheckboxTree.CheckboxTreeCellRenderer() {

>

     @Override

>

     public void customizeRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {

>

         //BreakpointsTreeCellRenderer.customizeRenderer(myProject, value, selected, expanded, getTextRenderer());
         JLabel label =null;
         if(value instanceof JLabel){

>

             label = (JLabel)value;
             label.setVisible(true);
             System.out.println(label.getText);
         }
     }

>

};

>

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);
}

>
>
>

---
Original message URL: https://devnet.jetbrains.com/message/5560277#5560277

>

0
Comment actions Permalink

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.

0

Please sign in to leave a comment.