Java: Adding panel inside another at run time to fit visible area
Question
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: 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);
OTHER TIPS
too little informations for the actual image
1 depends
what
Size
returns (generated graph, by using JUNG), are you set there setSize(int, int) too,if is graph resiziable,
2) remove
layout.setSize(panel.getMarketGraphPane().getPreferredSize());
and
graphPanel.setSize(panel.getMarketGraphPane().getPreferredSize());
you can't to setSize
for Object placed to the BorderLayout.CENTER
, and I think that is possible directly put (generated graph, using JUNG. It produces JPanel object
) to the BorderLayout.CENTER
area, try that maybe your twice setSize
generated some mess
and then you can call only
panel.add(graphPanel, BorderLayout.CENTER);
panel.revalidate();
panel.repaint();
3) another two choises (without clean-up setSize
)
call
pack();
to the Top-Level Container, notice carefully with LCD/LED monitors boundsput that to the JScrollPane