尝试清除Zest图

时间:2016-06-15 12:51:06

标签: java swt zest

我正在尝试从所有节点和连接中清除Zest Graph,以便我可以使用新节点和连接重绘Graph。要意识到我写了以下方法

     public void clearGraph( Graph graph ) {        
     Object[] objects = graph.getConnections().toArray() ;          
         for (int i = 0 ; i < objects.length; i++){
             GraphConnection graCon = (GraphConnection) objects[i];              
             graCon.dispose();
             //graCon.setVisible(false);
         }           
     objects = graph.getNodes().toArray();      
     for ( int i = 0 ; i < objects.length; i++){
             GraphNode graNode = (GraphNode) objects[i];
             graNode.dispose();
            //graNode.setVisible(false);
     }
}

这会导致我的prorgam崩溃

  

线程“main”中的异常org.eclipse.swt.SWTException:Widget被释放

作为一种解决方法,我尝试将节点和连接设置为不可见,但是不可见的对象似乎弄乱了我的Zest布局,所以如果有办法实际处理节点和连接,我会这样做。

这是错误按摩

Exception in thread "main" org.eclipse.swt.SWTException: Widget is disposed
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.widgets.Widget.error(Unknown Source)
at org.eclipse.swt.widgets.Widget.checkWidget(Unknown Source)
at org.eclipse.swt.widgets.Item.getText(Unknown Source)
at com.mycom.timelineview.views.IndicatorFactorVisualisationView$2.mouseDoubleClick(IndicatorFactorVisualisationView.java:221)
at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at com.mycom.timelineview.views.IndicatorFactorVisualisationView.indicatorFactorWindow(IndicatorFactorVisualisationView.java:249)
at com.mycom.timelineview.views.IndicatorFactorVisualisationView.<init>(IndicatorFactorVisualisationView.java:71)
at com.mycom.timelineview.views.SpiderWebMouseListener.chartMouseClicked(SpiderWebMouseListener.java:102)
at org.jfree.experimental.chart.swt.ChartComposite.mouseDown(ChartComposite.java:1621)
at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at com.mycom.timelineview.views.SpiderWebView.createPartControl1(SpiderWebView.java:622)
at com.mycom.timelineview.views.InformationPlatformAppView2$7.handleEvent(InformationPlatformAppView2.java:628)
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at com.mycom.timelineview.views.InformationPlatformAppView2.main(InformationPlatformAppView2.java:1330)
编辑:感谢巴兹,我发现了自己的错误。鼠标监听器必须在我之前处理的图形节点中搜索文本,因此当然程序必须崩溃。我改变了我的代码以避免它,现在Baz提出的方法运行完美。

1 个答案:

答案 0 :(得分:0)

在致电isDisposed()之前检查dispose()将避免此问题:

public void clearGraph( Graph graph )
{       
    Object[] objects = graph.getConnections().toArray() ;           
    for (int i = 0 ; i < objects.length; i++)
    {
        GraphConnection graCon = (GraphConnection) objects[i];
        if(!graCon.isDisposed())
            graCon.dispose();
    }            

    objects = graph.getNodes().toArray();       
    for (int i = 0; i < objects.length; i++)
    {
        GraphNode graNode = (GraphNode) objects[i];
        if(!graNode.isDisposed())
            graNode.dispose();
    }
}

这是一个测试示例,即使对于选定的节点也可以正常工作:

public static void main(String[] args)
{
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Stackoverflow");
    shell.setLayout(new GridLayout(1, false));

    Graph g = new Graph(shell, SWT.NONE);
    g.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    Random random = new Random(System.currentTimeMillis());

    final List<GraphNode> nodes = new ArrayList<>();
    for (int i = 0; i < 10; i++)
    {
        GraphNode node = new GraphNode(g, SWT.NONE);
        node.setText("TEST");
        node.setLocation(random.nextInt(400), random.nextInt(400));
        nodes.add(node);
    }

    for (int i = 0; i < 50; i++)
    {
        GraphNode source;
        GraphNode target;

        do
        {
            source = nodes.get(random.nextInt(nodes.size()));
            target = nodes.get(random.nextInt(nodes.size()));
        } while (source.equals(target));

        new GraphConnection(g, SWT.NONE, source, target);
    }

    Button clear = new Button(shell, SWT.NONE);
    clear.setText("Clear");
    clear.addListener(SWT.Selection, e -> {
        for(GraphNode node : nodes)
        {
            node.dispose();
        }
        nodes.clear();
    });
    clear.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

    shell.pack();
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}