Graphstream view not loading

2019-08-29 04:48发布

问题:

Well, i have the following schemas:

  • A Java class that extends JFrame instantiating the other class that contains my graph which returns a blank screen.

  • The second a normal class with the method main inside of it, calling the same class that contains my graph which returns a normal graph.

Now, why the class that is a JFrame returns a blank graph?

My code of the graph class.

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
    }

}

Most will say, why did you create a JPANEL here? I didnt find how to set the default size of it, but even removing the jpanel from inside, it didnt work, shows a blank viewer without any exception or other thing.

Here's where i call the class doing stuff i have to do.

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

After the creation of my SwingWorker, the graph was rendered, but the user interaction dissapeared.

Swing worker, here i just gather some information to give the graph class the possibility to proccess information.

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

}

And here, where its called.

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:

First of all class names should start with an upper case character. "grafoComparacao" should be GrafoComparacao.

Now, why the class that is a JFrame returns a blank graph?

I would guess that your GrafoComparacao class is created in some listener code which means the code is executing on the Event Dispatch Thread which is responsible for painting the GUI.

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

That looks like blocking code that is executing on the EDT, and means you are using an infinite loop which means the code never finishes executing so the GUI can never respond to events or repaint itself.

That code should execute on a separate Thread (maybe a SwingWorker) so the EDT is free to respond to events and repaint itself.

Read the section from the Swing tutorial on Concurrency in Swing for more information on the EDT and on a SwingWorker.