简体   繁体   中英

How to listen to vertex selection change in Jung 2?

how can I listen to vertex selection change in Jung 2? I've been trying with PropertyChangeListener and ChangeListener.

This is nicely explained on http://kahdev.wordpress.com/2010/02/20/detecting-selection-of-vertices-in-jung/

Just add a listener on the PickedVertexState of your VisualizationViewer:

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

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

final PickedState<Integer> pickedState = vv.getPickedVertexState();

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

    @Override
    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");
            }
        }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM