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.