문제

I am using NetBeans to create a SWING application which should be able to display a graph. In a demo app I was able to display the generated JUNG graph on a JFrame with the following code:

UndirectedGraph t = GraphML.CreateGraph("treeAttribute.graphml");
VisualizationViewer<Node, Edge> vv = new VisualizationViewer<Node, Edge>((new FRLayout<Node, Edge>(t)));
vv.getRenderContext().setVertexFillPaintTransformer(new VertexPainter());
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Node>());
vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller<Edge>());

final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.getContentPane().add(vv);
frame.pack();
frame.setVisible(true);

Now I want to make use of the Palette and the design mode so I have created a JFrame and I have inserted a JPanel into it. I want my graph to be displayed inside the JPanel so I have inserted the code for the graph into the initComponents() method. The code looks like the following:

public class Main extends javax.swing.JFrame {


public Main() throws ParserConfigurationException, SAXException, IOException {
    initComponents();

}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 634, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 298, Short.MAX_VALUE)
    );

    jLabel1.setText("Graph A");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel1)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap(45, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(39, 39, 39)
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(42, Short.MAX_VALUE))
    );

    try {

        UndirectedGraph t = GraphML.CreateGraph("treeAttribute.graphml");
        VisualizationViewer<Node, Edge> vv = new VisualizationViewer<Node, Edge>((new FRLayout<Node, Edge>(t)));
        vv.getRenderContext().setVertexFillPaintTransformer(new VertexPainter());
        vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Node>());
        vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller<Edge>());
        jPanel1.add(vv);

    } catch (ParserConfigurationException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SAXException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }

    pack();
}// </editor-fold>                        

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {

    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                new Main().setVisible(true);


            } catch (ParserConfigurationException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SAXException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    });

}

// Variables declaration - do not modify                     
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
// End of variables declaration                   
}

The program runs as normal but nothing gets displayed into the JPanel. I am not receiving any errors or warnings and the graph is there, cuz it prints some information for its vertices and edges. Any thoughts?

올바른 솔루션이 없습니다

다른 팁

usually in Netbeans this is sufficient to display a graph:

// say your graph is called g
layout = new FRLayout<>(g);
((FRLayout<yournode, youredge>)layout).setMaxIterations(1000);
layout.setSize(new Dimension(700, 700));
VisualizationViewer<yournode, youredge> vv = new VisualizationViewer<>(layout);
GraphZoomScrollPane scrollPane = new GraphZoomScrollPane(vv);
JPanel1.add(scrollPane);

but you should be aware that you have to tell Netbeans what is the JPanel layout, so right-click on your widget, then click "Set Layout" and pick the one you like.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top