Search code examples
javaswingjung

Java: Adding panel inside another at run time to fit visible area


In my application I want to show generated graph, using JUNG. It produces JPanel object as output. So, i though it would be just a matter of adding a component to panel. But the graph is drawn outside of its parent panel. Screenshot: screenshot How do I restrict the graph to be only inside the visible area? The code I use to add graph to panel is this (the border was for me to see panel borders, though it doesnt show for some reason):

    Layout<Object, String> layout = new CircleLayout<Object, String> (graphProvider.getGraph());
    layout.setSize(panel.getMarketGraphPane().getPreferredSize());
    BasicVisualizationServer<Object,String> graphPanel = new BasicVisualizationServer<Object,String>(layout);
    graphPanel.setBorder(new EtchedBorder());
    graphPanel.setSize(panel.getMarketGraphPane().getPreferredSize());
    panel.getMarketGraphPane().add(graphPanel, BorderLayout.CENTER);
    panel.getMarketGraphPane().revalidate();

Solution

  • use GraphZoomScrollPane to add zoom-able graph scrollpane container for VisualizationViewer object as follows:

    GraphZoomScrollPane pane = new GraphZoomScrollPane(visualizationViewer);
    JPanel panel = new JPanel();
    BorderLayout panelMapLayout = new BorderLayout();
    panel.setLayout(panelMapLayout);
    panel.add(pane, BorderLayout.CENTER);