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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:10:16+00:00 2026-06-17T12:10:16+00:00

I have a Java SE 7 application that needs to have the JTree nodes

  • 0

I have a Java SE 7 application that needs to have the JTree nodes updated. From the tutorial given by Oracle using this thread, there’s no given hint on how I could update the label (displayed text of the node on the Tree) on code. Currently I am using DefaultTreeModel as the model of my JTree and DefaultMutableTreeNode as the nodes of the said Tree.

To further detail about the application I am working on, I am developing a chat facility having the contact(s) displayed with their availability status (whether Online, Offline, etc.) per account.

The question is, how can I update the displayed text of a particular node without (at most) removing it from it’s parent and adding it on it’s designated index. Like a DefaultMutableTreeNode.setText("<new label>")?


UPDATE : January 20, 2013

Redefined the question for clarifications.

  • 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-17T12:10:18+00:00Added an answer on June 17, 2026 at 12:10 pm

    May this simple and executable program help you to resolve your issue.

    public class JTreeDemo  extends JPanel
        implements Runnable {
    
    private JTree tree;
    private DefaultTreeModel treeModel ;
    private Random rnd = new Random();
    private List<User> userList;
    public JTreeDemo() {
        super( );
    
        //Create the nodes.
        DefaultMutableTreeNode top =
            new DefaultMutableTreeNode("Users");
        treeModel = new DefaultTreeModel(top);
        createNodes(top);
    
        //Create a tree that allows one selection at a time.
        tree = new JTree(treeModel);
        tree.getSelectionModel().setSelectionMode
                (TreeSelectionModel.SINGLE_TREE_SELECTION);
    
        //Create the scroll pane and add the tree to it. 
        JScrollPane treeView = new JScrollPane(tree);
    
    
        //Add the split pane to this panel.
        add(treeView);
    }
    
    public String getRandomStatus() {
        int nextInt = rnd.nextInt(100);
        if( nextInt%2==0) {
            return "Online";
        } else {
            return "Offline";
        }
    }
    @Override
    public void run() {
         while(true) {
            try {   
              Thread.sleep(1000);
              int nextInt = rnd.nextInt(10);
              User user = userList.get(nextInt);
              user.setStatus(getRandomStatus());
              treeModel.nodeChanged(user);
            } catch (InterruptedException ex) {
                // handle it if necessary
            }
         }
    }
    
    private class User extends DefaultMutableTreeNode {
        public String userName;
        public String status;
    
        public User(String name) {
            userName = name;
    
        }
    
        public void setStatus(String status) {
            this.status = status;
        }
    
        public String getStatus() {
            return status;
        }
    
        @Override
        public String toString() {
            String color = status.equals("Online") ? "Green" : "Red";
            return "<html><b color='"+color+"'>"+
                    userName +"-"+status +"</b></html>";
        }
    
    }
    
    
    private void createNodes(DefaultMutableTreeNode top) {
        userList = new ArrayList() ;
        for(int i=0;i<10;i++) {
            User u1 = new User("User " + (i+1));
            u1.setStatus("Online");
             top.add(u1);
             userList.add(u1);
        }
    }
    
    private static void createAndShowGUI() {
    
        JFrame frame = new JFrame("TreeDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Add content to the window.
        JTreeDemo jTreeDemo = new JTreeDemo();
        frame.add(jTreeDemo);
        frame.pack();
        frame.setVisible(true);
        // update status randomly
        Thread thread = new Thread(jTreeDemo);
        thread.start();
    }
    
     public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
     }
    }
    

    I’ve added a Thread to update Status randomly, hope you can modify base on your need.

    Output :
    enter image description here

    Edit:
    1. Based on suggestion I’ve removed reload(node) and added tree model reload.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java desktop application, which needs to be updated with data from
I have a Java application that needs to collect and report a large number
I have a Java application that is managed using Maven. The project involves the
I have a standalone java application that fires up several JMS consumers using the
I am writing a Java Swing application that needs to have a window receive
I am writing this application in Java, where I have a JTree on the
I have a java application that needs to connect to a server. Its rather
I have a classic ASP/VBScript application that needs to call methods in a Java
We have a small java application running on windows that needs to be able
I have created a java application that frequently gathers data from a source and

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.