Graphstream视图未加载

时间:2016-04-27 19:24:20

标签: java swing graphstream

好吧,我有以下架构:

  • 扩展JFrame的Java类,实例化其他类 包含我的图表,它返回一个空白屏幕。

  • 第二个是普通类,其中包含main方法,调用 包含我的图形的同一个类,它返回一个普通图形。

现在,为什么作为JFrame的类会返回一个空白图?

我的图表类代码。

public class grafoComparacao implements ViewerListener {
    private List<Comparados> integralizacoesComparadas;
    private Viewer viewer;
    private Graph graph;
    private View view;
    protected boolean loop = true;

    public grafoComparacao(List<Comparados> listaComparados) throws HeadlessException, InterruptedException {
        System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
        this.integralizacoesComparadas = listaComparados;
        graph = new MultiGraph("clicks");
        graph.addAttribute("ui.stylesheet", GraphsStreamStylesheet.stylesheet);

        adicionaNodes(graph);

        viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
        viewer.enableAutoLayout();
        view = viewer.addDefaultView(false);

        JFrame frame = new JFrame("Comparação de catálogos");
        frame.add((Component) view);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 

        ViewerPipe fromViewer = viewer.newViewerPipe();
        fromViewer.addViewerListener(this);
        fromViewer.addSink(graph);

        frame.setVisible(true);

        while(loop) {
            fromViewer.pump();
        }        
    }

    //this actually works too.
//    public static void main(String[] args) throws HeadlessException, InterruptedException{
//        Control.Controller ctrl = new Controller();
//        JFileChooser escolherAlunos = new JFileChooser();
//        escolherAlunos.setMultiSelectionEnabled(true);
//        escolherAlunos.showOpenDialog(null);
//        File[] integralzacoes = escolherAlunos.getSelectedFiles();
//        List<Comparados> integralizacoesComparadas = ctrl.geraComparacaoIntegralizacoes(integralzacoes);
//        try {
//            grafoComparacao comparacao = new grafoComparacao(integralizacoesComparadas);
//        } catch (HeadlessException ex) {
//            Logger.getLogger(FrmPrincipal.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (InterruptedException ex) {
//            Logger.getLogger(FrmPrincipal.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        new grafoComparacao(integralizacoesComparadas);
//    }

    @Override
    public void viewClosed(String string) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void buttonPushed(String string) {

    }

    @Override
    public void buttonReleased(String string) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private void adicionaNodes(Graph graph) {
       //function that populate my graph
    }

}

大多数人会说,为什么要在这里创建JPANEL?我没有找到如何设置它的默认大小,但即使从内部删除jpanel,它也没有用,显示一个空白的查看器,没有任何异常或其他东西。

在这里,我打电话给班级做我必须做的事情。

  JFileChooser escolherAlunos = new JFileChooser();
    escolherAlunos.setMultiSelectionEnabled(true);
    escolherAlunos.showOpenDialog(null);
    File[] integralzacoes = escolherAlunos.getSelectedFiles();
    List<Comparados> integralizacoesComparadas = ctrl.geraComparacaoIntegralizacoes(integralzacoes);
    try {      
        grafoComparacao comparacao = new grafoComparacao(integralizacoesComparadas); // heres the graph call.
    } catch (HeadlessException ex) {
        Logger.getLogger(FrmPrincipal.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InterruptedException ex) {
        Logger.getLogger(FrmPrincipal.class.getName()).log(Level.SEVERE, null, ex);
    }

在创建SwingWorker之后,图形被渲染,但用户交互消失了。

Swing worker,在这里我只收集一些信息,让图表类可以获取信息。

public class VisualizacaoWorker extends SwingWorker<Void, Void>{
    private List<Comparados> listao;
    public VisualizacaoWorker(List<Comparados> lista){
        this.listao = lista;
    }

    @Override
    protected Void doInBackground() throws Exception {
        GrafoComparacao gf = new GrafoComparacao(this.listao);
        return null;
    }

}

在这里,它被称为。

JFileChooser escolherAlunos = new JFileChooser();
escolherAlunos.setMultiSelectionEnabled(true);
escolherAlunos.showOpenDialog(null);
File[] integralzacoes = escolherAlunos.getSelectedFiles();
List<Comparados> integralizacoesComparadas = ctrl.geraComparacaoIntegralizacoes(integralzacoes);
VisualizacaoWorker visualizacaoWorker = new VisualizacaoWorker(integralizacoesComparadas);
visualizacaoWorker.execute();

1 个答案:

答案 0 :(得分:2)

首先,类名应以大写字母开头。 “grafoComparacao”应为GrafoComparacao

  

现在,为什么作为JFrame的类会返回一个空白图?

我猜你的GrafoComparacao类是在一些监听器代码中创建的,这意味着代码正在Event Dispatch Thread上执行,它负责绘制GUI。

while(loop) 
{
    fromViewer.pump();
} 

这看起来像阻止在EDT上执行的代码,意味着你正在使用无限循环,这意味着代码永远不会完成执行,因此GUI永远不会响应事件或重新绘制自己。

该代码应该在一个单独的线程上执行(可能是SwingWorker),因此EDT可以自由地响应事件并重新绘制自己。

阅读Concurrency in Swing上Swing教程中的部分,了解有关EDT和SwingWorker的更多信息。