如何在Jung 2中听取顶点选择的变化?

时间:2012-01-26 15:18:38

标签: java jung vertex

如何在Jung 2中听取顶点选择的变化?我一直在尝试使用PropertyChangeListener和ChangeListener。

1 个答案:

答案 0 :(得分:7)

http://kahdev.wordpress.com/2010/02/20/detecting-selection-of-vertices-in-jung/

很好地解释了这一点

只需在VisualizationViewer的PickedVertexState上添加一个侦听器:

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