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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:41:21+00:00 2026-06-16T02:41:21+00:00

I am creating a Jtree with a rootNode and than create another thread that

  • 0

I am creating a Jtree with a rootNode and than create another thread that update the root node asynchronously.

It works fantastic if i run this Jtree independently in some JPanel, it was even working at some place in the project, but i was asked to have this Jtree in some new swing Component.

In new Swing Panel, it doesnot populate fully, it only populate the nodes that were inserted (at start for few milliseconds) before the Jtree was rendered on the Screen. Once the Jtree is rendered it doesnt get updated.
Now the interesting part i also made a mouse listener on the node so that i can create a new node by right click create node function, and with that new node is created and is added on the Jtree root node.

Important thing to add i was using newThread(){void run}).start() method to create a thread to add node on the Jtree, becuase i never felt the need of SwingUtilities.invokeLater method before. but now if i used SwingUtilities.invokeLater method than the main window also doesnot open it just halts during the startup, i just checked that SwingUtilities.invokeLater also works fine with the old component and ofcourse works fine independently.

And i do call model.nodeStructureChanged(changedNode); after adding the node thats why it was working fine before.

Kindly asist,
code is difficult to extract and Jtree code was working fine before, may be some component block the containing widgets to refresh itself async?

EDIT
Update to include some code, i am using Temp class as provided by Nick:-

    public BasicGraphEditor(String appTitle, mxGraphComponent component)
{
    // Stores and updates the frame title
    this.appTitle = appTitle;

    // Stores a reference to the graph and creates the command history
    graphComponent = component;
    final mxGraph graph = graphComponent.getGraph();
    undoManager = createUndoManager();

    // Do not change the scale and translation after files have been loaded
    graph.setResetViewOnRootChange(false);

    // Updates the modified flag if the graph model changes
    graph.getModel().addListener(mxEvent.CHANGE, changeTracker);

    // Adds the command history to the model and view
    graph.getModel().addListener(mxEvent.UNDO, undoHandler);
    graph.getView().addListener(mxEvent.UNDO, undoHandler);

    // Keeps the selection in sync with the command history
    mxIEventListener undoHandler = new mxIEventListener()
    {
        @Override
        public void invoke(Object source, mxEventObject evt)
        {
            List<mxUndoableChange> changes = ((mxUndoableEdit) evt
                    .getProperty("edit")).getChanges();
            graph.setSelectionCells(graph
                    .getSelectionCellsForChanges(changes));
        }
    };

    undoManager.addListener(mxEvent.UNDO, undoHandler);
    undoManager.addListener(mxEvent.REDO, undoHandler);

    // Creates the graph outline component
    graphOutline = new mxGraphOutline(graphComponent);

    // Creates the library pane that contains the tabs with the palettes
    libraryPane = new JTabbedPane();

            /////////////////////////////////////////////////
            // Only change i have done here: start
            ////////////////////////////////////////////////
    Temp tempExplorer = new Temp();

    libraryPane.add("new Explorere", tempExplorer);

            /////////////////////////////////////////////////
            // Only change i have done here: End
            ////////////////////////////////////////////////

    // Creates the inner split pane that contains the library with the
    // palettes and the graph outline on the left side of the window
    JSplitPane inner = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
            libraryPane, graphOutline);
    inner.setDividerLocation(320);
    inner.setResizeWeight(1);
    inner.setDividerSize(6);
    inner.setBorder(null);

    // Creates the outer split pane that contains the inner split pane and
    // the graph component on the right side of the window
    JSplitPane outer = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, inner,
            graphComponent);
    outer.setOneTouchExpandable(true);
    outer.setDividerLocation(200);
    outer.setDividerSize(6);
    outer.setBorder(null);

    // Creates the status bar
    statusBar = createStatusBar();

    // Display some useful information about repaint events
    installRepaintListener();

    // Puts everything together
    setLayout(new BorderLayout());
    add(outer, BorderLayout.CENTER);
    add(statusBar, BorderLayout.SOUTH);
    installToolBar();

    // Installs rubberband selection and handling for some special
    // keystrokes such as F2, Control-C, -V, X, A etc.
    installHandlers();
    installListeners();
    updateTitle();
}

The above class is from Jgraph library as https://github.com/jgraph/jgraphx
And i am just adding the jtree component like as above, no other changes.
please help.

  • 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-16T02:41:23+00:00Added an answer on June 16, 2026 at 2:41 am

    Swing isn’t thread safe unless explicitly stated. In the JavaDocs for JTree, it explicitly says this isn’t thread safe. If you update it in a thread outside the EDT, there’s no guarentee anything will work. So if you want to update a JTree from a different Thread, you need to use SwingUtilities.invokeLater(Runnable run); in order to put the request on the EDT.

    I’d recommend having a data structure to store the info of the JTree, and only using the JTree for User Interaction (not data storage).

    EDIT

    Here’s an example of using SwingUtilities.invokeLater() to update a JTree while in the component model. Without you posting any code, this is the best I have to work with. Please try to use this to recreate your problem (add pieces of your code to this example until you have narrowed down what the problem is).

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    
    public class Temp extends JPanel{
        JTree tree = new JTree();
    
        public Temp(){
            JScrollPane jsp = new JScrollPane(tree);
    
            // Creates the library pane that contains the tabs with the palettes
            JTabbedPane libraryPane = new JTabbedPane();
    
            libraryPane.add("new Explorere", jsp);
    
            // Creates the inner split pane that contains the library with the
            // palettes and the graph outline on the left side of the window
            JSplitPane inner = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                    libraryPane, new JPanel());
            inner.setDividerLocation(320);
            inner.setResizeWeight(1);
            inner.setDividerSize(6);
            inner.setBorder(null);
    
            // Creates the outer split pane that contains the inner split pane and
            // the graph component on the right side of the window
            JSplitPane outer = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, inner,
                    new JPanel());
            outer.setOneTouchExpandable(true);
            outer.setDividerLocation(200);
            outer.setDividerSize(6);
            outer.setBorder(null);
    
            // Puts everything together
            setLayout(new BorderLayout());
            add(outer, BorderLayout.CENTER);
        }
    
        public static void main(String[] args) {
            final Temp temp = new Temp();
            SwingUtilities.invokeLater(new Runnable(){
    
                @Override
                public void run() {
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setContentPane(temp);
                    frame.pack();
                    frame.setVisible(true);
                }});
    
            Thread updater = new Thread(temp.new CustomThread());
            updater.start();
        }
    
        public class CustomThread implements Runnable{
    
            @Override
            public void run() {
                for(int i = 0; i < 1000; i++){
                    updateTree("New Item "+ i);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
    
            public void updateTree(final String nodeToAdd){
                SwingUtilities.invokeLater(new Runnable(){
    
                    @Override
                    public void run() {
                        DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
                        DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot();
                        DefaultMutableTreeNode child = new DefaultMutableTreeNode(nodeToAdd);
                        model.insertNodeInto(child, root,root.getChildCount());
                        tree.scrollPathToVisible(new TreePath(child.getPath()));
                    }});
    
            }
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a node by using crrm as below $(#TreeDiv).jstree(create, $(#somenode), inside, {
I am having difficulty creating a JTree that allows the nodes to be reorganized
Creating a patch utility that will update my current website with my patch. when
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
I have specific troubles when I try create node. From context menu everything gona
Creating an application that requires BOTH gesture(swipe) support as well as simple touch events.
I am using the first example mentioned here for creating my JTree but I
I'm creating an app with JTabbedpane and I made a function that adds tabs
I'm creating a JTree and adding some nodes doing something like this: DefaultMutableTreeNode top
Creating a class that implements DynamicObject public class Test : DynamicObject { public override

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.