JTree: speed up drawing 1000+ child nodes from objects retrieved from the database?

When I retrieve 1000 java objects from the database, this is done very quickly. I end up List<Object>matching my query.

The problem is that these objects are on Jtree.

For example, I have the parentID of this node. When this node ( DefaultMutableTreeNode) is double-clicked ( TreeMouseListener.class), it will display the direct children of this node, not all descendants (although this may be required later, if possible, but not now).

The problem is that this jtree drawing operation takes a very long time to complete adding 1000+ children DefaultMutableTreeNodesfor the selected parent node.

ex) 1000 new DefaultMutableTreeNode(Person person);

How can I speed up this drawing process?

I don't use any custom box rendering, and I don't show anything but small bits of text for each node.

+5
source share
3 answers

You will need time where the slowdown occurs, but I doubt that it simply creates a DefaultMutableTreeNodes, which should be faster than loading Person objects from the database. This is unlikely to be a picture (if your Person # toString () is not very slow), since there will not be thousands of nodes on the screen.

, , , , . node , DefaultTreeModel # nodeStructureChanged (node) node.

, SSCCE. , :

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class TestJTree {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                final DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
                final DefaultTreeModel model = new DefaultTreeModel(root);
                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.getContentPane().add(new JScrollPane(new JTree(model)));
                frame.getContentPane().add(new JButton(
                        new AbstractAction("Add thousand children") {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int offset = root.getChildCount() + 1;
                        for (int i = 0; i < 1000; i++) {
                            DefaultMutableTreeNode child = new DefaultMutableTreeNode(
                                    "Person " + (i + offset));
// adding child with event (but isn't much slower really)
//                                model.insertNodeInto(child, root, root.getChildCount());
                            root.add(child);
                        }
                        model.nodeStructureChanged(root);
                    }
                }), BorderLayout.PAGE_END);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}
+5

, 1000+ DefaultMutableTreeNodes, . , DefaultMutableTreeNode. , . TreeModel, , . . 10 000 JTree ( , , , ), TreeModel, .

, , , , , . , , . , , TreeModel.

- . , , node . 1000 , , , , .

+2

JTree, JPanel : P.

, JTree, paint(Graphics g)

-3

All Articles