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 8679047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:52:29+00:00 2026-06-12T20:52:29+00:00

I have a problem with JTree nodes visibility I use for my JDialog. When

  • 0

I have a problem with JTree nodes visibility I use for my JDialog.
When I want to add new node to the model the Jtree is not refreshed.

Strange is the nodes are updating just as they should if I set setRootVisible(true).

Here is the code. Thanks in advance

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class TestClass {

JTree tree;
DefaultTreeModel dm;
JDialog dialog;

public TestClass(){
    JFrame frame = new JFrame("title");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    JPanel showPane = new JPanel();
    showPane.setLayout(new BorderLayout()); 

    dm = new DefaultTreeModel(new DefaultMutableTreeNode("root"));
    tree = new JTree(dm);
    tree.setRootVisible(false);

    JButton button = new JButton("add node");
    button.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent arg0) {
            DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot();           
            dm.insertNodeInto(new DefaultMutableTreeNode("Node " + (root.getChildCount() + 1)), root, root.getChildCount());

            int c = root.getChildCount();
            System.out.println("child count: " + c);

            for(int i=0; i<c; i++){
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(i);
                System.out.println("has node:" + node.getUserObject().toString());
            }
        }

    });

    showPane.add(tree, BorderLayout.CENTER);
    showPane.add(button, BorderLayout.PAGE_END);

    JComponent[] inputComponents = new JComponent[] {showPane};

    Object[] opButtons = {"OK"};

    JOptionPane optPane = new JOptionPane(inputComponents       
            , JOptionPane.PLAIN_MESSAGE             
            , JOptionPane.CLOSED_OPTION             
            , null                                      
            , opButtons                             
            , opButtons[0]);                            

    optPane.setPreferredSize(new Dimension(400 ,250));

    dialog = optPane.createDialog(null, "Create new Application Node");
    dialog.setLocationRelativeTo(frame);
    dialog.setVisible(true);

    if(optPane.getValue() != null){
        System.exit(0);
    }

}

public static void main(String arg[]){
    TestClass myClass = new TestClass();
}

}

  • 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-12T20:52:30+00:00Added an answer on June 12, 2026 at 8:52 pm

    Make sure to expand the path from the parent of the new node to the root each time you add a new child node:

    DefaultMutableTreeNode newChild = new DefaultMutableTreeNode("Node " + (root.getChildCount() + 1));
    dm.insertNodeInto(newChild, root, root.getChildCount());
    tree.expandPath(new TreePath(dm.getPathToRoot(newChild.getParent())));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with expanding JTree nodes. I start when use selects a
I'm trying to use dynamically JTree component. Under root node I have four nodes
I want to have a JTree in my swing application that does not have
I have problem SIMILAR to preventing form data reposting, but not quite the same
I try to dynamically add nodes to a Java Swing JTree , and the
I have a strange problem with JSTree and Ajax. I generate my tree via
In my code, depending on a condition, a JTree node might or might not
I need dynamic JTree where nodes would be objects and when node is expanded
I have a Jtree with nodes representing images that the user can call up
i have a JTree, and when u click leafs, it changes the JTable model

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.