[Jung] Detecting Selection of Vertices in Jung

In Jung, a PickedState object provides information about which vertices have been selected. To receive notifications when verticesare selected or unselected, define an ItemListener and add it to the PickedState via addItemListener. The listener is notified whenever an item is selected or unselected (or, if you prefer the terms, picked or unpicked).

In the example below, a graph is created and a listener that prints out when vertices are selected or selected is attached.

    Graph<Integer, String> basis = new SparseMultigraph<Integer, String>();
    final Layout<Integer, String> layout = new CircleLayout<Integer, String>(

    layout.setSize(new Dimension(300, 300));
    VisualizationViewer<Integer, String> vv = new VisualizationViewer<Integer, String>(
    final PickedState<Integer> pickedState = vv.getPickedVertexState();

    // Attach the listener that will print when the vertices selection changes.
    pickedState.addItemListener(new ItemListener() {

        public void itemStateChanged(ItemEvent e) {
        Object subject = e.getItem();
            // The graph uses Integers for vertices.
            if (subject instanceof Integer) {
                Integer vertex = (Integer) subject;
                if (pickedState.isPicked(vertex)) {
                    System.out.println("Vertex " + vertex
                        + " is now selected");
                } else {
                    System.out.println("Vertex " + vertex
                        + " no longer selected");

Update 21 May 2011: A working demo is available from my Github Repository.


6 Responses to [Jung] Detecting Selection of Vertices in Jung

  1. Excellent post…helped me a lot…keep up the good work.

  2. Muhammad Asaduzzaman says:

    Thx for the post. I was looking for this.

  3. manish kumar says:

    Superb!!!! I was looking for this for a long time

  4. andreas geo says:

    Could you please give a full example(with two vertices created) ? I add the vertices,i click on them but no response from the listener(there is no system.out.println).


  5. Kifayat says:

    thanks alot for the post, i was searching for this solution badly

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: