GraphStream Users

Archives de la liste Aide


RE: Concurrent modification exception with Swing; getNode() method not yet implemented


Chronologique Discussions 
  • From: "Steven E. Noel" <snoel AT gmu.edu>
  • To: "graphstream-users AT litislab.fr" <graphstream-users AT litislab.fr>
  • Subject: RE: Concurrent modification exception with Swing; getNode() method not yet implemented
  • Date: Wed, 25 Sep 2013 18:33:31 +0000
  • Accept-language: en-US

Sorry for the false alarm, I have solved my problem.  I needed to put the GraphStream View inside a JPanel of its own, then put that into the rest of the GUI.  Now it works as expected.  Thanks again, will try to stop bugging everyone now!

 

Steve


From: Steven E. Noel <snoel AT gmu.edu>
Sent: Wednesday, September 25, 2013 2:07 PM
To: graphstream-users AT litislab.fr
Subject: RE: Concurrent modification exception with Swing; getNode() method not yet implemented
 

Thanks a lot Guilhelm, this post was very helpful.  It solved the concurrent modification problem I was having.  But when I put add other Swing widgets, I am losing the handle to the GraphStream view.  I have posted sample code here:  http://snipt.org/Ahfif0 .

 

In my example, I start with your example GUIEventsExample.java (https://gist.github.com/gsavin/6577865).  I then create a JFrame and place the GraphStream View into the JFrame along with a JToggleButton.

 

Without the JToggleButton added to the frame, the GraphStream View works as expected, i.e., the mouse clicks generate events that InternalMouseManager handles and shows the JOptionPane.

 

But when the JToggleButton *is* added to the frame, this no longer works, i.e., mouse clicks are not causing the JOptionPane

popup.

 

Any suggestions?  Thanks again.

 

PS, thanks for all the effort in developing GraphStream, it is a truly remarkable tool.

 

Steve


From: Guilhelm Savin <guilhelm.savin AT gmail.com>
Sent: Tuesday, September 24, 2013 6:36 PM
To: graphstream-users AT litislab.fr; Steven E. Noel
Subject: Re: Concurrent modification exception with Swing; getNode() method not yet implemented
 
Hi Steve,

GraphicGraph should not be directly handled.
You should have a look to this thread first : http://sympa.litislab.fr/sympa/arc/graphstream-users/2013-09/msg00005.html about using graph inside the swing thread.
Then if you still have questions, we will try to help you.

Regards.
Guilhelm

"Steven E. Noel" <snoel AT gmu.edu> a écrit :

I have a question that involves GraphStream within a Swing GUI.  I know that is slightly out of scope of GraphStream per se, but my investigations led to a final question that I think is relevant.

 

I followed the tutorial in integrating a GraphStream viewer in a Swing GUI (http://graphstream-project.org/doc/Tutorials/Graph-Visualisation_1.1/#integrating-the-viewer-in-your-gui) and everything was ok.  My problem was later getting a ConcurrentModificationException while trying to modify a graph in response to a Swing event.

 

My goal is to be able to handle Swing events (buttons, etc.) to dynamically update a GraphStream graph (modify styles, etc.).  I wrote a small test of that.  Here is an excerpt:

 

/* =============================== */

import java.util.Collection;

import org.graphstream.graph.Node;
import org.graphstream.ui.graphicGraph.GraphicGraph;
import org.graphstream.ui.swingViewer.Viewer;


public class ToggleGreen {
    javax.swing.JToggleButton button;
    public javax.swing.JToggleButton getButton() {return button;}
    public void setButton(javax.swing.JToggleButton button) {this.button = button;}

 

    private static GraphicGraph graph = null;
    public GraphicGraph getGraph() {return graph;}
    public void setGraph(GraphicGraph _graph) {graph = _graph;}


    ToggleGreen(Viewer networkViewer) {
        setGraph(networkViewer.getGraphicGraph());  

        button = new javax.swing.JToggleButton("Green",  false);
        button.addItemListener (new java.awt.event.ItemListener() {


            public void itemStateChanged(java.awt.event.ItemEvent event) {
                if (button.isSelected()) {
                    System.out.println("Green ON");
                    /* This throws 
                     *  Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
                     *   at java.util.HashMap$HashIterator.nextEntry(Unknown Source)
                     *   at java.util.HashMap$KeyIterator.next(Unknown Source)
                     *  at org.graphstream.ui.graphicGraph.StyleGroupSet$ElementIterator.next(StyleGroupSet.java:1419)
                     *  at org.graphstream.ui.graphicGraph.StyleGroupSet$ElementIterator.next(StyleGroupSet.java:1403)
                     *  at ToggleGreen$1.itemStateChanged(ToggleGreen.java:33) */
                    for (Node node: getGraph().getEachNode()) {
                        node.addAttribute("ui.style", "fill-color:green;");
                    }

 

                    /* This throws
                     *  Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: not implemented !
                     *  at org.graphstream.ui.graphicGraph.GraphicGraph.getNode(GraphicGraph.java:1223)
                     *  at ToggleGreen$1.itemStateChanged(ToggleGreen.java:42) */
                    //Collection<Node> nodeSet = getGraph().getNodeSet();
                    //for (int i = 0; i < nodeSet.size(); i++) {
                    //    Node node = getGraph().getNode(i);
                    //    node.addAttribute("ui.style", "fill-color:green;");
                    //}
                }
            }
        });
    }
}
/* =============================== */

 

This class creates a JToggleButton, which I added to my GUI, along with a GraphStream viewer.  The instatiated viewer is passed into this class, giving a handle to the graph in the viewer.  The button event then attempts to add a green color to each graph node.

 

With the code above, this throws the concurrent modification exception (after painting a single node green).  I am somewhat new to Swing, but I see that this is a well-known problem with iterating over Java Collections within threads.  I tried various suggestions such as synchronized(), but was never successful in eliminating the concurrent modification exception.  As a sanity check, I tested that

 

    for (Node node: getGraph().getEachNode()) {

        node.addAttribute("ui.style", "fill-color:green;");

        }

 

works fine outside of the Swing thread.

 

Another suggestion I saw was to reference the list/array using its index rather than an iterator, which is commented out in the code example.  That is this:

 

    for (int i = 0; i < nodeSet.size(); i++) {

        Node node = getGraph().getNode(i);

        node.addAttribute("ui.style", "fill-color:green;");

    }

 

For this, I get "java.lang.RuntimeException: not implemented !"  Presumably this is coming from a GraphStream try/catch, indicating that the getNode() method is not yet implemented?

 

Thanks in advance for any suggestions.

 

Regards,

Steve

 




Archives gérées par MHonArc 2.6.16.

Top of page