I've created an application in Java, and the selecting of node's is very very slow when there are thousands of nodes that need to be ticked. I guess I'm not doing it the correct way, so I thought I would post on here and see if people have a better solution.
Basically, I have used the JIDE community library and I create a treemodel and add each node in turn as I read through a database of accounts, manufacturers, products, etc. with each table (accounts, manufacturers, products) displayed under their respective parent node. This all works fine and although a little slow, it's fast enough for the needs. This is built by doing the following;
- Code: Select all
accountNode.removeAllChildren();
productNode.removeAllChilder();
rootNode.removeAllChildren();
rootNode.add(accountNode);
rootNode.add(productNode);
ResetSet rs = select code,description from products;
while (rs.next()) {
number = rs.getString("code");
name = rs.getString("description");
if (search == true) { // If I'm looking for a specific product
if (name.toString().toLowerCase().contains(tmpSearch.toLowerCase())) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(description);
productsNode.add( node);
}
else {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(description);
productsNode.add( node);
}
}
This works fine and builds my tree with all the correct data.
I also have a Map of any products selected, which is built from the listener and is also working fine, its just a list of the product codes and I add or remove products based on the listener events.
Now, I haven't found a way to add a tick at the time of building the tree (the above code), so what I am currently doing is, once the above has run I am doing;
- Code: Select all
treeModel = new DefaultTreeModel(rootNode);
checkboxTree.setModel(treeModel);
checkboxTree.setRootVisible(true);
checkboxTree.getCheckBoxTreeSelectionModel().setDigIn(true);
and then I run a routine called populateCheckBoxes, which does this;
- Code: Select all
e = productsNode.preorderEnumeration();
while(e.hasMoreElements()){
DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) e.nextElement();
TreeNode [] ptr = dmtn.getPath();
TreePath path = new TreePath(ptr);
tmpNum = Utils.getBracketedNumber(path.toString());
if (tmpNum >= 0) {
if (consolProductsMap.containsKey(tmpNum)) {
checkboxTree.getCheckBoxTreeSelectionModel().addSelectionPath(path);
}
}
}
This final bit of code is really slow, taking up to 6 minutes if there are over 8000 products to be selected.
This is the bit I need help with, is there a way to actually set the node as selected while I am building the nodes (the first bit of code), or is it better to build an array of TreePaths and use setSelectionPaths to set the whole list of selected paths, or am I doing this completely wrong and should be doing this another way?
Any help, pointers or comments (pref. constructive) are welcome

Thanks