CheckboxTree

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

Postby hemna » Wed Jul 29, 2009 10:45 am

Is there a way to have the tree not show the checkboxes on the leaf nodes and the root node?
I have a 2 level dataset that I want to show in a checkbox tree, but only have checkboxes on the top level.

I don't want a checkbox on the root or level 2 nodes.

Code: Select all
* root
--Level 1
   -- level 2
 --Level 1a
   -- level 2a


Also, when the tree has Level 1 nodes that are unchecked, is there a way to show that level 1 and all child level 2 nodes as greyed or disabled?
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckboxTree

Postby JIDE Support » Wed Jul 29, 2009 11:07 am

Please try override CheckBoxTree#isCheckBoxVisible(TreePath) like the sample code to set the visibility of each path.
Code: Select all
                return path.getPathCount() == 2;


Please set your own cell renderer to set the rows to gray.
Code: Select all
        DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer() {
            @Override
            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
                Component rendererComponent = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
                TreePath path = tree.getPathForRow(row);
                if (path != null && path.getPathCount() != 2) {
                    rendererComponent.setForeground(Color.gray);
                }
                return rendererComponent;
            }
        };
        _tree.setCellRenderer(renderer);


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

Re: CheckboxTree

Postby hemna » Wed Jul 29, 2009 1:17 pm

Wow thanks for the quick reply. I tried both items and they work great.

I just need to re-enable the leaf nodes when the checkbox is checked again.
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckboxTree

Postby JIDE Support » Wed Jul 29, 2009 1:21 pm

Please try use CheckBoxTreeSelectionModel#isPathSelected(TreePath, boolean) to check the selection state of the path.

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

Re: CheckboxTree

Postby hemna » Wed Jul 29, 2009 1:54 pm

the docs state that CheckBoxTreeSelectionModel#isPathSelected tells weither a path is selected, but not if it's checked.
If the level 1 node is checked, then I need to enable all level 2 (leaf) nodes. If the path is unchecked, then I need to disable all leaf nodes.
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckboxTree

Postby hemna » Wed Jul 29, 2009 1:59 pm

Also, where do I add code to detect when the checkbox has been checked/unchecked. So I need to add another special listener for the checkbox itself?
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckboxTree

Postby JIDE Support » Wed Jul 29, 2009 2:24 pm

Please try the following code.

Code: Select all
        DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer() {
            @Override
            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
                Component rendererComponent = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
                TreePath path = tree.getPathForRow(row);
                if (path != null && path.getPathCount() != 2 && !((CheckBoxTree) tree).getCheckBoxTreeSelectionModel().isPathSelected(path, true)) {
                    rendererComponent.setForeground(Color.gray);
                }
                return rendererComponent;
            }
        };
        _tree.setCellRenderer(renderer);


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

Re: CheckboxTree

Postby hemna » Wed Jul 29, 2009 3:12 pm

ok I figured out how to add a listener to pickup checkbox changes. Now I need to grey out/disable all leaf nodes from a path once I detect a checkbox has been unchcked.

Here is my checkbox code
Code: Select all
         
categoryTree.getCheckBoxTreeSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
      public void valueChanged(TreeSelectionEvent e) {
         boolean checked = false;
         // your code here.
         TreePath checkedPath = e.getPath();
         if (checkedPath != null) {
            if (categoryTree.getCheckBoxTreeSelectionModel().isPathSelected(checkedPath)) {
               checked = true;
            } else {
               checked = false;
            }
         }
      }
});
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckboxTree

Postby JIDE Support » Wed Jul 29, 2009 3:22 pm

Can you please give the code in my previous post a try? In that way, you don't even have to listen to the event.

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

Re: CheckboxTree

Postby hemna » Wed Jul 29, 2009 4:02 pm

Your previous example works great. I have one small difference is that I'm disabling the node instead of marking it grey with the hopes that you can't select it.

Code: Select all
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        Component rendererComponent = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
        TreePath path = tree.getPathForRow(row);
        if (path != null && path.getPathCount() != 2 && !((CheckBoxTree) tree).getCheckBoxTreeSelectionModel().isPathSelected(path, true)) {
            //rendererComponent.setForeground(Color.gray);
            rendererComponent.setEnabled(false);
        }
        return rendererComponent;
    }


The node does get disabled, but I have 2 issues
1) when it's disabled, the node doesn't render the file icon to the left of the node. When the checkbox is checked, and the nodes are enabled again, they get a file icon on the left.
Shouldn't the file icon be there in both cases?
2) when the node is disabled, I can still select the node, but the selection looks quite odd...
Attachments
checkboxtreeselection.png
checkboxtreeselection.png (7.29 KiB) Viewed 49039 times
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckboxTree

Postby hemna » Wed Jul 29, 2009 4:39 pm

ok for now, I'm going back to just marking each node as grey.


How do I mark which nodes are checked and unchecked when I'm building the tree of nodes prior to adding them to the model?
Code: Select all
//get the list of categories.
String[] categories = myclass.getList();
for (int x=0; x<categories.length; x++) {
   DefaultMutableTreeNode categoryNode = new DefaultMutableTreeNode(categories[x]);
         
   if (myclass.isSelected(categories[x])) {
      //how do I mark this node as CHECKED?
      //HERE
   }

   //get the groups for each category
   String[] groups = myclass.getSubList(categories[x]);
   for (int y=0; y<groups.length; y++) {
      DefaultMutableTreeNode groupNode = new DefaultMutableTreeNode(groups[y]);
      categoryNode.add(groupNode);            
   }
   rootNode.add(categoryNode);
}

DefaultTreeModel treeModel = new DefaultTreeModel(root);
categoryTree  = new CategoryCheckBoxTree();
categoryTree.setModel(treeModel);

hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckboxTree

Postby JIDE Support » Wed Jul 29, 2009 5:24 pm

1, Regarding the enable/disable issue, I cannot reproduce the icon losing behavior. Please use our CheckBoxTreeDemo plus the following code and see if you can get the behavior you want.
Code: Select all
        DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer() {
            @Override
            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
                Component rendererComponent = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
                TreePath path = tree.getPathForRow(row);
                if (path != null && path.getPathCount() != 2 && !((CheckBoxTree) tree).getCheckBoxTreeSelectionModel().isPathSelected(path, true)) {
//                    rendererComponent.setForeground(Color.gray);
                    rendererComponent.setEnabled(false);
                }
                return rendererComponent;
            }
        };
        renderer.setLeafIcon(IconsFactory.getImageIcon(QuickFilterTreeDemo.class, "/icons/song.png"));
        renderer.setClosedIcon(IconsFactory.getImageIcon(QuickFilterTreeDemo.class, "/icons/album.png"));
        renderer.setOpenIcon(IconsFactory.getImageIcon(QuickFilterTreeDemo.class, "/icons/album.png"));
        _tree.setCellRenderer(renderer);


2, Please try use CheckBoxTree#getCheckBoxTreeSelectionModel()#addSelectionPaths(TreePath[]) to add the selection after you create the CheckBoxTree.

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

Re: CheckboxTree

Postby hemna » Thu Jul 30, 2009 3:02 pm

Ok so I mostly have this working. I can initially set the list of checked items. But I need to reset the list of checked items behinds the scenes due to other user actions. I've found that if I do the following, that all of my selections are removed, but the new selections aren't set for some reason.

Code: Select all
categoryTree.getCheckBoxTreeSelectionModel().removeSelectionPaths(
               categoryTree.getCheckBoxTreeSelectionModel().getSelectionPaths());
categoryTree.getCheckBoxTreeSelectionModel().addSelectionPaths(checkedCategoryPaths);


I have verified that the checkedCategoryPaths (which is a new TreePath[] list that is non empty) doesn't seem to work. Is this the proper way to reset a different list of checked nodes?
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckboxTree

Postby JIDE Support » Thu Jul 30, 2009 3:21 pm

It works for me. I tried to add the following line to our CheckBoxTreeDemo and it works fine. Would you please share with us a test case to demonstrate the issue?
Code: Select all
        _tree.getCheckBoxTreeSelectionModel().addSelectionPaths(new TreePath[] {_tree.getPathForRow(1)});

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

Re: CheckboxTree

Postby hemna » Thu Jul 30, 2009 3:47 pm

does addSelectionPaths add new selections/checked checkboxes or does it replace the existing ones?
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckboxTree

Postby JIDE Support » Thu Jul 30, 2009 3:50 pm

It adds new selections without clearing existing ones. However, if in digIn mode, it could combine some selections internally. In both cases, visually it should just add new selections you passed into.

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

Re: CheckboxTree

Postby hemna » Thu Jul 30, 2009 4:37 pm

ok thanks. So I should be removing all the current ones first, then adding new ones, like I have in my previous posting?
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckboxTree

Postby JIDE Support » Thu Jul 30, 2009 4:41 pm

Yes. And if you always want to clear the previous selections, you can also try setSelectionPaths().

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

Re: CheckboxTree

Postby hemna » Thu Jul 30, 2009 5:30 pm

I just don't know what the problem is.
Code: Select all
buildNodesFromCategories(model);
int size=selectedPathsMap.size();

checkedCategoryPaths = selectedPathsMap.values().toArray(new TreePath[size]);
//checkedCategoryPaths = categoryTree.getCheckBoxTreeSelectionModel().getSelectionPaths();
//categoryTree.getCheckBoxTreeSelectionModel().removeSelectionPaths(
    //categoryTree.getCheckBoxTreeSelectionModel().getSelectionPaths());
   
categoryTree.getCheckBoxTreeSelectionModel().setSelectionPaths(checkedCategoryPaths);
TreePath[] test = categoryTree.getCheckBoxTreeSelectionModel().getSelectionPaths();
categoryTree.getCheckBoxTreeSelectionModel().setSelectionPaths(test);



When I populate the test TreePath array and look at it in eclipse debug, it comes back as exactly as I'd suspect. It's correctly populated
[[Root, Foo], [Root, Bar]]

checkedCategoryPaths and test end up with the same values and look ok...but the tree ends up with nothing selected.
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckboxTree

Postby JIDE Support » Thu Jul 30, 2009 6:29 pm

The reason probably is that the TreeNode contained in your TreePath is not the same instance of the TreeNodes inside the Tree although they look similar. Please try populate the TreePath from the current Tree.

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

Re: CheckboxTree

Postby hemna » Thu Jul 30, 2009 6:51 pm

so I have to get all of the nodes in the tree, then get their TreePath objects, shove those in a TreePath[] and then call setSelectionPaths() ?

man...the docs just aren't clear. Do the docs state anywhere that you have to use the same instance of the TreePath objects that are already contained in the Tree?
Can you provide a sample of the "correct" way to reset the selected paths with new selections?

I'm a bit frustrated, but I really do appreciate the help. You guys have been great.
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckboxTree

Postby JIDE Support » Fri Jul 31, 2009 7:52 am

Please try the following code. Since CheckBoxTree is a subclass of JTree, we didn't describe much on the inherited behaviors on our documents. Anyway, we will improve our document here.
Code: Select all
        TreeModel model = _tree.getModel();
        Object root = model.getRoot();
        int childCount = model.getChildCount(root);
        for (int i = 0; i < childCount; i++) {
            Object child = model.getChild(root, i);
            if (child instanceof DefaultMutableTreeNode) {
                if ("1".equals(((DefaultMutableTreeNode) child).getUserObject())) {
                    _tree.getCheckBoxTreeSelectionModel().addSelectionPath(new TreePath(((DefaultMutableTreeNode) child).getPath()));
                }
            }
        }

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

Re: CheckboxTree

Postby hemna » Fri Jul 31, 2009 11:02 am

I'm having something else quite bizarre happening.

when I do
tree = new CheckBoxTree();

The debugger says there are 20 visible nodes. When the tree gets rendered I see these. I have no idea where they are coming from, unless the CheckBoxTree itself
populates it with sample nodes?!?
Code: Select all
JTree
 Colors
  blue
  violet
  red
  yellow
 Sports
  basketball
  soccer
  football
  hockey
 Food
  hot dogs
  pizza
  ravioli
  bananas
Attachments
CheckBoxTree_Debug.jpg
1 line after new CheckBoxTree
CheckBoxTree_Debug.jpg (218.16 KiB) Viewed 49027 times
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Re: CheckboxTree

Postby JIDE Support » Fri Jul 31, 2009 12:10 pm

Yes. It comes from JTree if you don't pass any model into the constructor.

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

Re: CheckboxTree

Postby hemna » Fri Sep 11, 2009 3:17 pm

How can I insert a new node into an existing tree? Say my tree is constrained to 2 levels

Root
Code: Select all
   + Level 1
       + Level2a
   + Level 1a
       + Level2b
       + Level2c



say I want to insert a new node Level2d under Level1a ? (resulting in)
Code: Select all
Root
   + Level 1
       + Level2a
   + Level 1a
       + Level2b
       + Level2c
       + Level2d


or I want to insert a new Level2d node under level1b, which doesn't exist as well.
Code: Select all
   + Level 1
       + Level2a
   + Level 1a
       + Level2b
       + Level2c
   + Level1b
       + Level 2d


Thanks
hemna
 
Posts: 37
Joined: Wed Jul 29, 2009 10:27 am

Next

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

Who is online

Users browsing this forum: No registered users and 14 guests

cron