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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:04:17+00:00 2026-05-23T19:04:17+00:00

Situation I have a visualisation using JGraph . The graph is updated by a

  • 0

Situation

I have a visualisation using JGraph. The graph is updated by a different thread to that which instantiates the Visualisation.

Expected Behaviour

The graph should be updated by various worker threads. The function that the threads call to update the graph is syncronized, so the worker threads will not causes concurrency issues between themselves.

Actual Behaviour

There is (ocassionally) an exception thrown in the AWT-EventQueue thread when painting. Sometimes it’s a null pointer, sometimes it’s an index out of bounds. Here’s a stack trace:

Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
at com.mxgraph.shape.mxConnectorShape.translatePoint(Unknown Source)
at com.mxgraph.shape.mxConnectorShape.paintShape(Unknown Source)
at com.mxgraph.canvas.mxGraphics2DCanvas.drawCell(Unknown Source)
at com.mxgraph.view.mxGraph.drawState(Unknown Source)
at com.mxgraph.swing.mxGraphComponent$mxGraphControl.drawCell(Unknown Source)
at com.mxgraph.swing.mxGraphComponent$mxGraphControl.drawChildren(Unknown Source)
at com.mxgraph.swing.mxGraphComponent$mxGraphControl.drawCell(Unknown Source)
at com.mxgraph.swing.mxGraphComponent$mxGraphControl.drawChildren(Unknown Source)
at com.mxgraph.swing.mxGraphComponent$mxGraphControl.drawCell(Unknown Source)
at com.mxgraph.swing.mxGraphComponent$mxGraphControl.drawChildren(Unknown Source)
at com.mxgraph.swing.mxGraphComponent$mxGraphControl.drawCell(Unknown Source)
at com.mxgraph.swing.mxGraphComponent$mxGraphControl.drawFromRootCell(Unknown Source)
at com.mxgraph.swing.mxGraphComponent$mxGraphControl.drawGraph(Unknown Source)
at com.mxgraph.swing.mxGraphComponent$mxGraphControl.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(JComponent.java:1029)
at com.mxgraph.swing.mxGraphComponent$mxGraphControl.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(JComponent.java:866)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JViewport.paint(JViewport.java:764)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5138)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:302)
at javax.swing.RepaintManager.paint(RepaintManager.java:1188)
at javax.swing.JComponent._paintImmediately(JComponent.java:5086)
at javax.swing.JComponent.paintImmediately(JComponent.java:4896)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:783)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:735)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
at javax.swing.RepaintManager.access$700(RepaintManager.java:58)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1593)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:226)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:647)
at java.awt.EventQueue.access$000(EventQueue.java:96)
at java.awt.EventQueue$1.run(EventQueue.java:608)
at java.awt.EventQueue$1.run(EventQueue.java:606)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:617)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

It doesn’t seem to adversely affect the running of the application. A new AWT-EventQueue spawns, which will at some stage inevitably encounter the same problem.

Cause

I think it must caused by updating the graph in a separate thread to the one that the JFrame is instantiated in. The things that need to be drawn are being changed whilst the paint method is trying to draw them.

Question

How do I go about working around this problem? Can I somehow synchronize the paint method with the method that updates the graph?

Visualisation Code

package ui;

import javax.swing.JFrame;

import com.mxgraph.layout.mxOrganicLayout;
import com.mxgraph.layout.mxStackLayout;
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGraphModel;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;

import core.Container;
import core.Node;
import core.WarehouseGraph;

public class Visualisation extends JFrame{
    private static final long serialVersionUID = 8356615097419123193L;
    private mxGraph graph = new mxGraph();
    Object parent = graph.getDefaultParent();
    mxOrganicLayout graphlayout = new mxOrganicLayout(graph);
    mxStackLayout containerLayout = new mxStackLayout(graph, true, 10);

    public Visualisation(WarehouseGraph model){
        super("Warehouse Simulator");

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(800, 600);

        graph.getModel().beginUpdate();
        try{
            for(Node node : model.getNodes().values()){
                mxCell cell = (mxCell)graph.insertVertex(parent, node.getId(), node.getId(), 0, 0, 60, 30);

                Object prev = null;
                for(Container container : node.getContainers()){
                    Object newCont = graph.insertVertex(cell, container.getId(), container.getId(), 0, 0, 60, 20);
                    if(prev != null) graph.insertEdge(cell, null, null, prev, newCont);
                    prev = newCont;
                }
            }

            for(Node node : model.getNodes().values()){
                for(Node toNode : node.getDownstreamNodes()){
                    Object fromCell = ((mxGraphModel)graph.getModel()).getCell(node.getId());
                    Object toCell = ((mxGraphModel)graph.getModel()).getCell(toNode.getId());
                    graph.insertEdge(parent, null, null, fromCell, toCell);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            graph.getModel().endUpdate();
        }

        graphlayout.execute(parent);

        Object[] nodes = mxGraphModel.getChildVertices(graph.getModel(), parent);
        for(Object cell : nodes){
            containerLayout.execute(cell);
            graph.updateCellSize(cell);
        }


        mxGraphComponent graphComponent = new mxGraphComponent(graph);
        getContentPane().add(graphComponent);

        this.setVisible(true);
    }

    //CALLED FROM SYNCHRONIZED FUNCTION
    public void moveContainer(Container container, Node to){
        graph.getModel().beginUpdate();
        mxCell toCell = (mxCell)((mxGraphModel)graph.getModel()).getCell(to.getId());
        mxCell containerCell = (mxCell)((mxGraphModel)graph.getModel()).getCell(container.getId());
        mxCell fromCell = (mxCell)containerCell.getParent();

        try{
            Object[] edges = mxGraphModel.getEdges(graph.getModel(), containerCell);
            graph.removeCells(edges);

            containerCell.removeFromParent();

            graph.addCell(containerCell, toCell);

            Object[] containers = mxGraphModel.getChildVertices(graph.getModel(), toCell);
            if(containers.length >= 2)
                graph.insertEdge(toCell, null, null, containerCell, containers[containers.length-2]);
            containerLayout.execute(toCell);
            containerLayout.execute(fromCell);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            graph.getModel().endUpdate();
        }
    }
}

Controller Code

package core;
import serialisation.JsonUtil;
import ui.Visualisation;

public class Controller{
    private WarehouseGraph graph;
    Visualisation viz;

    public static void main(String[] args){
        Controller.getInstance();
    }

    private Controller(){
        graph = JsonUtil.initFromJson(); //graph has many worker threads running which can call containerMoved
        viz = new Visualisation(graph);
    }

    private static class SingletonHolder{
        private static final Controller INSTANCE = new Controller();
    }

    public static Controller getInstance(){
        return SingletonHolder.INSTANCE;
    }

    public synchronized void containerMoved(Container container, Node from, Node to){
        viz.moveContainer(container, to);
    }
}
  • 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-05-23T19:04:18+00:00Added an answer on May 23, 2026 at 7:04 pm

    Not answer to your question, just some notice for your concept.

    1. Can you show us your public static void main(String[] args) {...} methods? I am hoping that there is something that looks like:

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

              public void run() {
                  Visualisation vis = new Visualisation();
              }
          });
      }
      
    2. this.setSize(800, 600); should be this.setpreferredSize(new Dimension(800, 600));

    3. Then add this.pack(); before last line (this.setVisible(true);)

    EDIT:

    1. What type of Components returns mxGraphComponent graphComponent = new mxGraphComponent(graph); because if is custom Components based on JPanel then

    getContentPane().add(graphComponent); should be add(graphComponent);

    1. All input from backGround thread/task must be wrapped into invokeLater(). If it is flickering or freezing, then you have to look for invokeAndWait(). Personally I hate invokeAndWait() exists there, but for this method I can’t give you some correct suggestions. Or:

    2. If you’ll run your task on some periodical bases, then you have to look for Runnable (output must be wrapped into invokeLater) or for SwingWorker.

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

Sidebar

Related Questions

Situation: Have a UITextField instance which is a subview of a UIScrollView. Need: Screen
I have an interesting situation - I have a table row which, currently, shows
I have the following situation: I have a loop which could loop any amount
Situation: I have two tables, say 'master' and 'detail', where 'master' has two columns
I have situation, where I want to read configuration file only one time, when
current situation: I have a service with several typical service methods like create(), remove(),
My Situation I have a list of items(surveys) displayed on my home page. When
The Situation: I have a mobile, HTML5 application. The application works great in any
The situation: I have crafted a scanner, parser and various AST classes for a
I posted a couple of questions but haven't gotten any reply yet. Everything I

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.