Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8240221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:24:52+00:00 2026-06-07T20:24:52+00:00

When I start my Java swing application, I read an XML file into a

  • 0

When I start my Java swing application, I read an XML file into a DefaultTreeModel, create a new JTree with the model, add the jtree to a JScrollPane, and Life is Good. Everything works:

// Program initialization:
jarFilesDB = new JarFilesDB ();
jarFilesDB.load(JarFilesDB.JAR_DB_FILENAME);
jtree = new JTree(jarFilesDB.getRootNode ());
JScrollPane scrollPane = new JScrollPane (jtree);
contentPane.add(scrollPane, BorderLayout.CENTER);

Later on, I might want to create a new XML file, and completely rebuild my model. When I do this, I can’t get the jtree to redisplay the new model. Either I get a blank pane, or the old model still displays:

// Completely regenerate XML file and Model
jarFilesDB.mkXmlFile (sPath);                // Generate new XML file
jarFilesDB.load(JarFilesDB.JAR_DB_FILENAME); // Generate new Model

Per Andrew Thompson’s request, I wrote a “Short (as possible), Self-Contained, Correct (not! or I wouldn’t be asking the question) Example):

package com.rgb;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTree;
import javax.swing.JScrollPane;
import javax.swing.tree.*;

public class FrmMain extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    FrmMain frame = new FrmMain();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public FrmMain() {
        setTitle("HelloJTree3");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JButton btnNewButton = new JButton("Click Me");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                doClick ();
            }
        });
        contentPane.add(btnNewButton, BorderLayout.NORTH);
        jarFilesDB = new JarFilesDB ();
        jarFilesDB.load(isAlpha);
        isAlpha = !isAlpha;
        jtree = new JTree(jarFilesDB.getRootNode ());
        JScrollPane scrollPane = new JScrollPane (jtree);
        contentPane.add(scrollPane, BorderLayout.CENTER);   
    }

    private void doClick () {
        jarFilesDB.load(isAlpha);
        isAlpha = !isAlpha;
    }

    private JarFilesDB jarFilesDB;
    private JTree jtree;
    private boolean isAlpha = false;
}

class JarFilesDB extends DefaultTreeModel {

    public JarFilesDB () {
        super (null);
    }

    public void load (boolean isAlpha) {
        if (rootNode == null)
            rootNode =  new DefaultMutableTreeNode("root");
        else
            rootNode.removeAllChildren ();

        String[] values;
        if (isAlpha)
            values = new String[] {"A", "A1", "A2", "B", "B1", "B2"};
        else
            values = new String[] {"10", "11", "12", "20", "21", "22"};

        DefaultMutableTreeNode childNode = new DefaultMutableTreeNode (values[0]);
        DefaultMutableTreeNode leafNode = new DefaultMutableTreeNode (values[1]);
        childNode.add(leafNode);
        leafNode = new DefaultMutableTreeNode (values[2]);
        childNode.add(leafNode);
        rootNode.add(childNode);

        childNode = new DefaultMutableTreeNode (values[3]);
        leafNode = new DefaultMutableTreeNode (values[4]);
        childNode.add(leafNode);
        leafNode = new DefaultMutableTreeNode (values[5]);
        childNode.add(leafNode);
        rootNode.add(childNode);
    }

    public TreeNode getRootNode () {
        return rootNode;
    }

    private DefaultMutableTreeNode rootNode = null;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:24:55+00:00Added an answer on June 7, 2026 at 8:24 pm

    add() methods don’t fire updates. It is best to use insertNodeInto. To fix, add the following:

    nodeStructureChanged(rootNode);
    

    At the end of load() method.

    And use this to initialize the tree:

    jtree = new JTree(jarFilesDB);
    

    Here’s the updated SSCCE, not sure what is the intent for rootNode, so I left it alone:

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JTree;
    import javax.swing.JScrollPane;
    import javax.swing.tree.*;
    
    public class FrmMain extends JFrame {
    
        private JPanel contentPane;
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        FrmMain frame = new FrmMain();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the frame.
         */
        public FrmMain() {
            setTitle("HelloJTree3");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 300);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            contentPane.setLayout(new BorderLayout(0, 0));
            setContentPane(contentPane);
    
            JButton btnNewButton = new JButton("Click Me");
            btnNewButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    doClick ();
                }
            });
            contentPane.add(btnNewButton, BorderLayout.NORTH);
            jarFilesDB = new JarFilesDB ();
            jarFilesDB.load(isAlpha);
            isAlpha = !isAlpha;
            //jtree = new JTree(jarFilesDB.getRootNode ());
            jtree = new JTree(jarFilesDB);
            JScrollPane scrollPane = new JScrollPane (jtree);
            contentPane.add(scrollPane, BorderLayout.CENTER); 
        }
    
        private void doClick () {
            jarFilesDB.load(isAlpha);
            isAlpha = !isAlpha;
        }
    
        private JarFilesDB jarFilesDB;
        private JTree jtree;
        private boolean isAlpha = false;
    }
    
    class JarFilesDB extends DefaultTreeModel {
    
        public JarFilesDB () {
            super (new DefaultMutableTreeNode("root"));
            rootNode = (DefaultMutableTreeNode) getRoot(); 
        }
    
        public void load (boolean isAlpha) {
            rootNode.removeAllChildren ();
    
            String[] values;
            if (isAlpha)
                values = new String[] {"A", "A1", "A2", "B", "B1", "B2"};
            else
                values = new String[] {"10", "11", "12", "20", "21", "22"};
    
            DefaultMutableTreeNode childNode = new DefaultMutableTreeNode (values[0]);
            DefaultMutableTreeNode leafNode = new DefaultMutableTreeNode (values[1]);
            childNode.add(leafNode);
            leafNode = new DefaultMutableTreeNode (values[2]);
            childNode.add(leafNode);
    
            rootNode.add(childNode);
    
            childNode = new DefaultMutableTreeNode (values[3]);
            leafNode = new DefaultMutableTreeNode (values[4]);
            childNode.add(leafNode);
            leafNode = new DefaultMutableTreeNode (values[5]);
            childNode.add(leafNode);
            rootNode.add(childNode);
    
            nodeStructureChanged(rootNode);
        }
    
        public TreeNode getRootNode () {
            return rootNode;
        }
    
        private DefaultMutableTreeNode rootNode = null;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Java Swing application for the Mac using Java 1.6. I've read
Is there possible to code the Java Swing application to run when Windows start
I'm writing a new Java 6 Swing application and want to have a Show
I have a Java application which is used to start a Swing user interface.
In my java swing application I load log4j properties from a properties file stored
How can i start a java swing application by clicking a button on a
I use a batch to launch my java application like this start /min java
As per Java web start where does the jar file get downloaded on a
I've never done a Java Web start application before. I wrote my app's JNLP
I have a Java Web Start application. I specify the resource (jars, images, etc)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.