Ruben Laguna's blog

May 7, 2009 - 3 minute read - connectionwidget graph illegalstateexception java labelwidget layerwidget library netbeans order visual visualization widget

Painting LabelWidgets on top of the ConnectionWidget - Netbeans Visual Library

I was fiddling with the Netbeans Visual Library again, trying to show a complex graph on screen and I run into a problem: There were so many connection in the graph that some widgets were hard to read because the ConnectionWidget arrows were printed over them.

So I tried to I try to rearrange the order of my LayerWidgets to paint the LabelWidgets on top of ConnectionWidgets. A call to mainLayer.bringToFront() should be enough, but this is what I got when I tried that:

java.lang.IllegalStateException: Widget (org.netbeans.api.visual.widget.LabelWidget@10014f0) was not added into the scene
at org.netbeans.api.visual.anchor.Anchor.getRelatedSceneLocation(
at org.netbeans.modules.visual.anchor.RectangularAnchor.compute(
at org.netbeans.modules.visual.router.DirectRouter.routeConnection(
at org.netbeans.api.visual.widget.ConnectionWidget.calculateRouting(
at org.netbeans.modules.visual.layout.ConnectionWidgetLayout.layout(
at org.netbeans.api.visual.widget.Widget.layout(
at org.netbeans.api.visual.widget.Widget.layout(
at org.netbeans.api.visual.widget.LayerWidget.layout(
at org.netbeans.api.visual.widget.Widget.layout(
at org.netbeans.api.visual.widget.Scene.layoutScene(
at org.netbeans.api.visual.widget.Scene.validate(
at org.netbeans.api.visual.widget.SceneComponent.addNotify(
at java.awt.Container.addImpl(
at java.awt.Container.add(
at com.rubenlaguna.modules.mainmodule.GraphTopComponent.setDotFile(
at com.rubenlaguna.modules.mainmodule.DotOpenSupport.createCloneableTopComponent(
at org.openide.util.Mutex.doEvent(
at org.openide.util.Mutex.writeAccess(
at org.openide.actions.OpenAction.performAction(
at org.openide.util.actions.NodeAction$DelegateAction$
at org.netbeans.modules.openide.util.ActionsBridge.doPerformAction(
at org.openide.util.actions.NodeAction$DelegateAction.actionPerformed(
at org.openide.explorer.view.TreeView$PopupSupport.mouseClicked(
at java.awt.AWTEventMulticaster.mouseClicked(
at java.awt.AWTEventMulticaster.mouseClicked(
at java.awt.AWTEventMulticaster.mouseClicked(
at java.awt.AWTEventMulticaster.mouseClicked(
at java.awt.Component.processMouseEvent(
at javax.swing.JComponent.processMouseEvent(
at java.awt.Component.processEvent(
at java.awt.Container.processEvent(
at java.awt.Component.dispatchEventImpl(
at java.awt.Container.dispatchEventImpl(
at java.awt.Component.dispatchEvent(
at java.awt.LightweightDispatcher.retargetMouseEvent(
at java.awt.LightweightDispatcher.processMouseEvent(
at java.awt.LightweightDispatcher.dispatchEvent(
at java.awt.Container.dispatchEventImpl(
at java.awt.Window.dispatchEventImpl(
at java.awt.Component.dispatchEvent(
[catch] at java.awt.EventQueue.dispatchEvent(
at org.netbeans.core.TimableEventQueue.dispatchEvent(
at java.awt.EventDispatchThread.pumpOneEventForFilters(
at java.awt.EventDispatchThread.pumpEventsForFilter(
at java.awt.EventDispatchThread.pumpEventsForHierarchy(
at java.awt.EventDispatchThread.pumpEvents(
at java.awt.EventDispatchThread.pumpEvents(

So I posted a question on netbeans graph mailing list but the suggestions there didn’t resolved my problem.

After digging a little bit into the Visual Library source code I come up with a really easy solution. Just call mainLayer.bringToFront() AFTER the scene was painted for the first time on the screen. I should have tried that first!. The result shows LabelWidgets painted overt the ConnectionWidgets so the text in the LabelWidgets is always readable.

class GraphTopComponent extends CloneableTopComponent {
  //.... omitted ....
  private showScene() {
    JComponent c = scene.createView();
    GraphLayout graphLayout = GraphLayoutFactory.createHierarchicalGraphLayout(scene, false);
    ForceDirectedLayout forceDirectedGraphLayout = new ForceDirectedLayout(scene);
    SceneLayout sceneGraphLayout = LayoutFactory.createSceneGraphLayout(scene, graphLayout);
    this.forceDirectedSceneLayout = LayoutFactory.createSceneGraphLayout(scene, forceDirectedGraphLayout);
class DotGraphScene extends GraphScene.StringGraph {
  //... omitted ...
  void bringMainLayerToFront() {

Anchor will throw an IllegalStateException if the relatedWidget has no location in the scene. Those widget will not get a position until the LayerWidget that contains them is processed. So you keep the original order of the LayerWidgets until the scene is painted for the first time, by then all widgets have a location in the scene and then it’s posible to change the relative order of the LayerWidgets using LayerWidget.bringToFront().

The only problem with this approach is that is your are using a dynamic layout like me there is a chance that the layout changes the position of the LabelWidget by a noticiable amount and the Anchor will not be updated accordingly until the next iteration of the dynamic layout. Which lead to the artifact marked in red in the previous figure. This is only a real problem if your dynamic layout doesn’t stabilize or if is not very smooth.