javaswingjtreetreecellrenderer

Get JTree node text inside method getTreeCellRendererComponent from the custom renderer


I am customizing a JTree so some nodes have checkboxes, using santhosh tekuri's work as a base.

So the idea is writing a custom TreeCellRenderer, which in this case extends JPanel and implements TreeCellRenderer, and then at the method getTreeCellRendererComponent I have to decide for each node if it is receiving a checkbox or not.

I have seen some other examples for a typical JTree to customize each node icon, but they relay on converting each node to a JLabel and getting its text, while here it is a JPanel.

This is how my renderer looks like:

public class CheckTreeCellRenderer extends JPanel implements TreeCellRenderer{

    private CheckTreeSelectionModel selectionModel;
    private TreeCellRenderer delegate;
    private TristateCheckBox checkBox;
    protected CheckTreeManager.CheckBoxCustomizer checkBoxCustomer;


    public CheckTreeCellRenderer(TreeCellRenderer delegate, CheckTreeSelectionModel selectionModel){
        this.delegate = delegate;
        this.selectionModel = selectionModel;
        this.checkBox = new TristateCheckBox("");
        this.checkBox.setOpaque(false);
        setLayout(new BorderLayout());
        setOpaque(false);
    }


    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus){
        Component renderer = delegate.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);

        // Inside this if clause, those cells which do not require checkbox will be returned
        if({CODE_TO_GET_NODE_TEXT}.startsWith("A")){
            return renderer;
        }

        TreePath path = tree.getPathForRow(row);
        if(path != null){
            if(checkBoxCustomer != null && !checkBoxCustomer.showCheckBox(path)){
                return renderer;
            }
            if(selectionModel.isPathSelected(path, selectionModel.isDigged())){
                checkBox.getTristateModel().setState(TristateState.SELECTED);
            }
            else{
                checkBox.getTristateModel().setState(selectionModel.isDigged() && selectionModel.isPartiallySelected(path) ? TristateState.INDETERMINATE : TristateState.DESELECTED);
            }
        }
        removeAll();
        add(checkBox, BorderLayout.WEST);
        add(renderer, BorderLayout.CENTER);

        return this;
    }
}

In this case I want to avoid setting a Tristate checkbox for those cells whose texts starts with "A" as an example. But I can't find a way to get the text from the value argument.

In case it helps, this is how I create the JTree:

DefaultMutableTreeNode root = new DefaultMutableTreeNode();
JTree tree = new JTree(root);

// Add nodes to the tree
DefaultMutableTreeNode friends_node = new DefaultMutableTreeNode("Friends");
    friends_node.add(new DefaultMutableTreeNode("Anna"));
    friends_node.add(new DefaultMutableTreeNode("Amador"));
    friends_node.add(new DefaultMutableTreeNode("Jonas"));
    friends_node.add(new DefaultMutableTreeNode("Mike"));
    friends_node.add(new DefaultMutableTreeNode("Anthony"));
    friends_node.add(new DefaultMutableTreeNode("Maya"));
    friends_node.add(new DefaultMutableTreeNode("Pepe Vinyuela"));
root.add(friends_node);

tree.setCellRenderer(renderer = new CheckTreeCellRenderer(tree.getCellRenderer(), new CheckTreeSelectionModel(tree.getModel(), dig)));

Any idea?


Solution

  • If you're using String userObject = "Anna"; new DefaultMutableTreeNode(userObject);, then you might be able to use the DefaultMutableTreeNode#getUserObject() method:

    @Override public Component getTreeCellRendererComponent(
        JTree tree, Object value, boolean selected, boolean expanded,
        boolean leaf, int row, boolean hasFocus) {
      Component renderer = delegate.getTreeCellRendererComponent(
        tree, value, selected, expanded, leaf, row, hasFocus);
      if (value instanceof DefaultMutableTreeNode) {
        Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
        // Inside this if clause, those cells which do not require checkbox will be returned
        if(userObject instanceof String && ((String) userObject).startsWith("A")){
          return renderer;
        }
        //...