0

I have a method that recalculates all nodes of the passed bunch. In some environments, I have an issue with delays (about 15ms) calling methods of the DefaultTreeModel class such as getChildCount, getChild, insertNodeInto. Interesting enough this delay occurs not for every element in the loop. What is the possible reason for such behaviour of DefaultTreeModel? Is this something to do with threads in Swing?


public void refresh(DefaultTreeModel model, MyTreeNode bunchNode,
            Vector<?> objects, int depth, int maxDepth) {

        int numberOfBunchNodes;
        //insert all objects
        for (int i = 0; i < objects.size(); i++) {
            MyTreeNode newNode = null;
            numberOfBunchNodes = model.getChildCount(bunchNode);
            if (i < numberOfBunchNodes) {
                //insert node if not same objects
                MyTreeNode bNode = (MyTreeNode) model.getChild(bunchNode, i);
                if (bNode.getUserObject() != objects.elementAt(i)) {
                    newNode = new MyTreeNode(objects.elementAt(i));
                    model.insertNodeInto(newNode, bunchNode, i);
                }
                else {
                    model.nodeChanged(bNode);
                    newNode = bNode;
                }
            }
            else {
                newNode = new MyTreeNode(objects.elementAt(i));
                model.insertNodeInto(newNode, bunchNode, i);
            }
            if (depth < maxDepth) {
                Vector<?> subTreeItems = getFurtherNodes(newNode);
                if (!subTreeItems.isEmpty()) {
                    refresh(model, newNode, subTreeItems, depth + 1, maxDepth);
                }
            }
        }
        //remove obsolete tree nodes
        numberOfBunchNodes = bunchNode.getChildCount();
        if (numberOfBunchNodes > objects.size()) {
            for (int i = numberOfBunchNodes - 1; i >= objects.size(); i--) {
                model.removeNodeFromParent((MyTreeNode) model.getChild(bunchNode, i));
            }
        }
    }

P.s. Such delay is critical when Jtree contains many elements, for example having 100 elements in model causes the refresh method to take about 1.5 seconds.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
ngranin
  • 181
  • 2
  • 14
  • Post a [mre] that others can test. – Andrew Thompson Oct 27 '21 at 09:30
  • Did you try profiling your code in order to see which part of it takes the most time? – Abra Oct 27 '21 at 10:26
  • Unfortunately, I don't have access to the environment where the slowness happens, that's why I have only the detailed log files – ngranin Oct 27 '21 at 10:35
  • 1
    `JTree` listens for changes to its model. Each time you change the model, the `JTree` needs to perform some processing - which obviously takes time. I think you should try and minimize the number of changes you make to the `DefaultTreeModel` in method `refresh`. As mentioned in the other [comment](https://stackoverflow.com/questions/69735915/defaulttreemodel-methods-are-executing-slowly-in-some-circumstances#comment123265413_69735915), it may help someone answer you if you post a [mcve]. – Abra Oct 27 '21 at 10:36
  • _Unfortunately, I don't have access to the environment where the slowness happens_ Are you saying that you don't experience slowness with the same code? – Abra Oct 27 '21 at 10:38
  • 1
    @Abra, _Are you saying that you don't experience slowness with the same code?_ Yes, I don't have issues with the same data in my development environment. I will try to prepare minimal reproducible example – ngranin Oct 27 '21 at 11:00
  • 3
    [_The Secret Life of a `DefaultTreeModel`_](https://dzone.com/articles/secret-life-defaulttreemodel) expands on @Abra's point. – trashgod Oct 27 '21 at 16:22
  • @ngranin: use an artificially small heap like [this](https://stackoverflow.com/a/15307253/230513) to exaggerate the problem when profiling. – trashgod Oct 27 '21 at 18:29

0 Answers0