Issue with checkboxtree with "Select All" checkbox

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.

Issue with checkboxtree with "Select All" checkbox

Postby mandar.j » Thu Aug 23, 2012 9:16 am

Hi,

I am trying to use a "Select All" checkbox which should allow to select all checkboxes and similarly unselect all of them on unselecting that checkbox. It should also allow to deselect some of all items. The goal is to add all finally selected checkbox items in a List "selectedItems". But, somehow I am not able to get it working as expected. It is giving unpredictable selections (for example it is adding unselected items to list and not adding some of selected items). Below is snippet of code that I'm trying.

Can you take a look at it and help me to figure out the issue. Please let me know if there is any better way to achieve the desired result.

Code: Select all
List<String> selectedItems = new ArrayList<String>();

DefaultMutableTreeNode root = new DefaultMutableTreeNode("Select All", true);
DefaultMutableTreeNode box1 = new DefaultMutableTreeNode("CheckBox1", false);
DefaultMutableTreeNode box2 = new DefaultMutableTreeNode("CheckBox2", false);
DefaultMutableTreeNode box3 = new DefaultMutableTreeNode("CheckBox3", false);
DefaultMutableTreeNode box4 = new DefaultMutableTreeNode("CheckBox4", false);
root.add(box1);
root.add(box2);
root.add(box3);
root.add(box4);

CheckBoxTree tree = new CheckBoxTree(root);


tree.getCheckBoxTreeSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
   public void valueChanged(TreeSelectionEvent e) {
      TreePath[] paths = e.getPaths();
      for (int i = 0; i < paths.length; i++) {
         TreePath path = e.getPath();
         DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) path.getLastPathComponent();
         if (e.isAddedPath(i)) {
            if (selectedNode.toString().equals("Select All")) {
               for (Enumeration en = selectedNode.children(); en.hasMoreElements();) {
                  if(!selectedItems.contains(en.nextElement().toString())) {
                     selectedItems.add(en.nextElement().toString());
                  }
               }
            }
            if(!selectedItems.contains(selectedNode.toString())) {
               selectedItems.add(selectedNode.toString());
            }
         }
         else {
            if (selectedNode.toString().equals("Select All")) {
               for (Enumeration en = selectedNode.children(); en.hasMoreElements();) {
                  selectedItems.remove(en.nextElement().toString());
               }
            }
            selectedItems.remove(selectedNode.toString());                        
         }
      }
   }
});
mandar.j
 
Posts: 8
Joined: Fri Jul 27, 2012 12:57 am

Re: Issue with checkboxtree with "Select All" checkbox

Postby JIDE Support » Thu Aug 23, 2012 10:33 am

Please try to add a root node and add all first-level nodes as a child to that root node. The root node's default behavior should be able to fulfill your request. As long as you check the root node, all its descendant nodes get selected/deselected.

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

Re: Issue with checkboxtree with "Select All" checkbox

Postby mandar.j » Fri Aug 24, 2012 6:40 am

Thanks. I tried with following code but seems that it works partially. By default, it selects all the nodes. But It doesn't work correctly if I deselect one of the selected nodes. When I deselect one of the default selected nodes, it looks like its mess up the selections and even if deselect a non-root(non "Select All") node, it says deselected root node and it keep only deselected node in the selection and clears all other nodes from tree selection. Any idea what could be issue in that?

Code: Select all
int row = tree.getRowCount();
TreePath[] listOfSelectedNodes = new TreePath[row];
for (int q=0; q < row; q++) {
   listOfSelectedNodes[q] = tree.getPathForRow(q);
}
tree.setSelectionPaths(listOfSelectedNodes);
Object rootModel = tree.getModel().getRoot();
tree.getCheckBoxTreeSelectionModel().addSelectionPath(new TreePath(rootModel));

tree.getCheckBoxTreeSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
   public void valueChanged(TreeSelectionEvent e) {
      TreePath[] paths = e.getPaths();
      for (int i = 0; i < paths.length; i++) {
         TreePath path = paths[i];
         DefaultMutableTreeNode expandedNode = (DefaultMutableTreeNode) path.getLastPathComponent();
         if (e.isAddedPath(path)) {
            //Do Nothing
         }
         else {
            tree.getCheckBoxTreeSelectionModel().removeSelectionPath(path);
         }
      }
   }
});   

tree.setDigIn(true);


Thanks in advance,
Mandar
mandar.j
 
Posts: 8
Joined: Fri Jul 27, 2012 12:57 am

Re: Issue with checkboxtree with "Select All" checkbox

Postby JIDE Support » Fri Aug 24, 2012 10:42 am

TreeSelectionEvent#getPaths() returns only the paths that have been changed ( added or removed ). While CheckBoxTreeSelectionModel#getSelectionPaths() will returns all paths that were selected. Is that you needed? Please feel free to let me know if I made a misunderstand.

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

Re: Issue with checkboxtree with "Select All" checkbox

Postby JIDE Support » Tue Sep 11, 2012 2:13 pm

B.T.W., the behavior you described is as designed when CheckBoxTree#isDigIn() returns true.

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 14 guests

cron