title: How to check 'selected' of an item in JCheckBoxTree after pressing the button
body: <p>I have a customised JCheckBoxTree which its code is taken from this <a href="https://github.com/aterai/java-swing-tips/blob/master/FileSystemTreeWithCheckBox/src/java/example/MainPanel.java" rel="nofollow">source</a>.
  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 <code>DefaultMutableTreeNode</code> and if the node file is equal to the specified file/string then change its state to <code>SELECTED</code>. However I am not sure if it is the right way of doing such a thing and also could not achieve it.</p>
  
  <p>This is what I tried, (doesn't work):</p>
  
  <pre><code>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);
              }
          }
  }
  </code></pre>
  
  <p>The following lines are related codes from my main file:</p>
  
  <pre><code>        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);
  </code></pre>
  
  <p>Here is the class that I think is related to my issue:</p>
  
  <pre><code>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();
          }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>So is there any idea about how to change the status of a tree's item programatically ?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <pre><code>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);
          }
      }
  </code></pre>
  
  <p>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. (<code>Video1.avi</code>) in this example. So I am sure <code>check.status = JTreeModification.Status.SELECTED;</code> can do the job. But there are two problems still, first it does not loop all over the nodes. and secondly, although it assigns <code>Selected</code> 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 <code>Video1.avi</code>. So it needs to loop all over the nodes and update the UI.</p>