0

I have a customised JCheckBoxTree which its code is taken from this source. So I want to have access to the status of each CheckBox of this tree. For example, I want to set an item of the tree as 'SELECTED' by pressing a button. I think I need to make a loop to go through the DefaultMutableTreeNode and if the node file is equal to the specified file/string then change its state to SELECTED. However I am not sure if it is the right way of doing such a thing and also could not achieve it.

This is what I tried, (doesn't work):

public void finder(DefaultMutableTreeNode root){
        JTreeModification treeClass = new JTreeModification();
        Enumeration en = root.depthFirstEnumeration();
        //if(){}
        while (en.hasMoreElements()) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) en.nextElement();
            if(node.equals("file4.json")){
               treeClass.new CheckBoxNode(new File(node.getParent(), node), JTreeModification.Status.SELECTED);
            }
        }
}

The following lines are related codes from my main file:

        JTreeModification treeClass = new JTreeModification();
        File myTest = new File("D:\\Documents\\A X");
        DefaultMutableTreeNode root = new DefaultMutableTreeNode();
        final DefaultTreeModel treeModel = new DefaultTreeModel(root);

        for (File fileSystemRoot: myTest.listFiles()) {
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(treeClass.new CheckBoxNode(fileSystemRoot, JTreeModification.Status.DESELECTED));
            root.add(node);
        }
        treeModel.addTreeModelListener(treeClass.new CheckBoxStatusUpdateListener());
        final JTree tree = new JTree(treeModel) {
            @Override public void updateUI() {
                setCellRenderer(null);
                setCellEditor(null);
                super.updateUI();
                //???#1: JDK 1.6.0 bug??? Nimbus LnF
                setCellRenderer(treeClass.new FileTreeCellRenderer());
                setCellEditor(treeClass.new CheckBoxNodeEditor());
            }
        };
        tree.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
        tree.setRootVisible(false);
        tree.addTreeSelectionListener(treeClass.new FolderSelectionListener());

        tree.setEditable(true);
        tree.expandRow(0);

        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        panel.add(new JScrollPane(tree), BorderLayout.CENTER);

Here is the class that I think is related to my issue:

public enum Status { SELECTED, DESELECTED, INDETERMINATE }

class CheckBoxNode {
        public final File file;
        public Status status;
        public CheckBoxNode(File file) {
            this.file = file;
            status = Status.INDETERMINATE;
        }
        public CheckBoxNode(File file, Status status) {
            this.file = file;
            this.status = status;
        }
        @Override public String toString() {
            return file.getName();
        }
}

In the provided GitHub link, there is method starting from line 72, that does something similar to what I want, but I could not merge it to my method.

So is there any idea about how to change the status of a tree's item programatically ?

EDIT:

public void finder(DefaultMutableTreeNode root){
        JTreeModification treeClass = new JTreeModification();
        Object o = root.getLastChild();
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) o;
        o = node.getUserObject();
        CheckBoxNode check = (CheckBoxNode) o;
        System.out.println(check);
        if(check.toString().equals("Video1.avi")){
            check.status = JTreeModification.Status.SELECTED; 
            //treeModel.addTreeModelListener(treeClass.new CheckBoxStatusUpdateListener());
        }
        System.out.println(check.status);

        Enumeration e = node.children();
            while (e.hasMoreElements()) {
                finder(root);
        }
    }

I just noticed the above code can assign "SELECTED" to the first file of the iteration if its name is same as the one in the if statement. (Video1.avi) in this example. So I am sure check.status = JTreeModification.Status.SELECTED; can do the job. But there are two problems still, first it does not loop all over the nodes. and secondly, although it assigns Selected to the file and I could recognise that in the console print, but it does not show it in the UI until I check or press another item in the tree then it updates the ui and set Video1.avi. So it needs to loop all over the nodes and update the UI.

Alex
  • 255
  • 2
  • 5
  • 10
  • How are you assessing the nodes? You could iterate over the tree model through the use of a recursive algorithm and return all the selected items... – MadProgrammer Mar 05 '15 at 23:14
  • @MadProgrammer Do you mean assigning files to the DefaultMutableTreeNode? If so, `for (File fileSystemRoot: myTest.listFiles()) { DefaultMutableTreeNode node = new DefaultMutableTreeNode(treeClass.new CheckBoxNode(fileSystemRoot, JTreeModification.Status.DESELECTED)); root.add(node); }` does this. As it seems! – Alex Mar 06 '15 at 01:28
  • I more curious about how you were iterating through the tree nodes... – MadProgrammer Mar 06 '15 at 01:32
  • @MadProgrammer Well I am not sure if I get you correctly, but the first piece of code in my question is what I meant by iterating the tree model, but obviously it is not functioning, since CheckBoxNode takes a file in its constructor. (If that is the only problem). – Alex Mar 06 '15 at 02:12
  • 1
    The question would then be, how could `DefaultMutableTreeNode` ever be equal to `file4.json`? – MadProgrammer Mar 06 '15 at 02:34
  • @MadProgrammer well, I printed node in the loop and it was printing the name of files and folders. so I thought it can easily find the file that is equal to `file4.json`. – Alex Mar 06 '15 at 02:57
  • 1
    `DefaultMutableTreeNode` `toString` (from memory) is using the `userObject` properties `toString` result... – MadProgrammer Mar 06 '15 at 03:00
  • @MadProgrammer I made an update in my post. Please have a look. I think I am getting closer to the solution. – Alex Mar 06 '15 at 03:05

0 Answers0