dynamically changing data model for JTree

Your problem is in method update() of class TestTree. You keep adding another JScrollPane to the TestTree. You only want one JScrollPane.

You correctly update the model of the JTree and that’s all you need to do. The JTree will automatically update itself whenever its model changes.

Also, don’t set the size of the JFrame. Set the size of its components and call method pack() which will make the JFrame big enough to contain all its components.

In the below code I have commented out the lines you need to remove from your code.

Class TestTree

package test;

import java.awt.Dimension;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

public class TestTree extends JPanel {
    final List<String> objects;

    private JTree tree;
    private final DefaultMutableTreeNode root;

    public TestTree(final List<String> theObjects) {
        objects = theObjects;
        root = new DefaultMutableTreeNode("root");
        tree = new JTree(root);
        JScrollPane scrollPane = new JScrollPane(tree);
        scrollPane.setPreferredSize(new Dimension(100, 490));

    private void updateTree() {
        final DefaultMutableTreeNode result = new DefaultMutableTreeNode(objects.size() + " objects");
        for (String object : objects) {
            final DefaultMutableTreeNode objectNode = new DefaultMutableTreeNode(object);

    public void update() {
//        tree = new JTree(root);
//        add(new JScrollPane(tree));

Class TestGUI

package test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestGUI extends JFrame {

    private final List<String> objects = new ArrayList<>();
    private TestTree testTree;

    final JPanel canvas = new JPanel() {
        public void paintComponent(final Graphics g) {
            g.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
            for (final String object : objects) {
                final String[] parts = object.split("--");
                g.drawOval(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), 5, 5);

    public TestGUI() {
        final Container cp = getContentPane();
        cp.setLayout(new BorderLayout());
        cp.add(canvas, BorderLayout.CENTER);
        testTree = new TestTree(objects);
        cp.add(testTree, BorderLayout.EAST);
        canvas.addMouseListener(new MouseListener() {
            public void mouseReleased(MouseEvent me) {}
            public void mousePressed(MouseEvent me) {}
            public void mouseExited(MouseEvent me) {}
            public void mouseEntered(MouseEvent me) {}
            public void mouseClicked(MouseEvent me) {
                objects.add(me.getX() + "--" + me.getY());
        canvas.setPreferredSize(new Dimension(400, 490));
//        setSize(500, 500);

    public static void main(final String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new TestGUI();

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top