CheckBoxTree: SELECTED, DONT_CARE look same when disabled

This is the forum for JIDE Common Layer which is open sourced at https://github.com/jidesoft/jide-oss. Please note, JIDE technical support doesn't monitor this forum as often as other forums. Please consider subscribe for technical support for JIDE Common Layer so that you can use customer only forum to get a timely response.

Moderator: JIDE Support

Forum rules
Community driven forum for open source JIDE Common Layer. JIDE technical support doesn't monitor this forum as often as other forums. If you only use JIDE Common Layer, please consider subscribing for technical support for JIDE Common Layer so that you can use customer only forum to get a timely response.

CheckBoxTree: SELECTED, DONT_CARE look same when disabled

Postby Sbodd3 » Tue Aug 03, 2010 7:01 am

So, I have a CheckBoxTree that's read-only (I want to display heirarchical selections, and allow the user to expand/collapse the tree but not change the checked state of items in the tree). My problem is that, when I do this, the "some-elements-selected" and "all-elements-selected" checkboxes look the same. (Using the default Swing look-and-feel on either Windows or Linux)


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;
    }
Sbodd3
 
Posts: 1
Joined: Mon Aug 02, 2010 11:10 am

Re: CheckBoxTree: SELECTED, DONT_CARE look same when disable

Postby JIDE Support » Tue Aug 03, 2010 9:33 am

Your solution is fine for me. I'm afraid there is no better solution for now. The painting is controlled by the button model of the JCheckBox, armed, pressed, selected. So the difference could only be visually obvious when the check box is enabled.

Thanks,
JIDE Software Technical Support Team
JIDE Support
Site Admin
 
Posts: 37219
Joined: Sun Sep 14, 2003 10:49 am


Return to JIDE Common Layer Open Source Project Discussion (Community Driven)

Who is online

Users browsing this forum: No registered users and 74 guests