Reputation: 1079
I am attempting to create a JFrame where the top is a JPanel of a JGraph and the bottom is JPanel of a bar that allows the user to select how many nodes will appear in the Graph, and the Graph to update after this happens. For this reason I put the Graph in a separate class where later I will be building an update method.
My problem is that the Panel that contains the Graph is the size of the frame, but my Graph is only as large as the max vertex value. How do I get change my JPanel to be a certain size, and have the Graph fill up this space?
Also is the way I'm doing it the best way to achieve this goal or is there a better way?
public class MyGraph extends JFrame{
private static final long serialVersionUID = 1L;
private JPanel Gpanel;
public MyGraph(){
super("Graph");
JPanel Gpanel = new NewPanel();
getContentPane().add(Gpanel);
}
public static void main(String args[]){
MyGraph frame = new MyGraph();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
class NewPanel extends JPanel{
private static final long serialVersionUID = 1L;
mxGraph graph;
Object parent;
NewPanel(){
this.graph = new mxGraph();
this.parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try{
Object v1 = graph.insertVertex(parent, null, "Hello", 20, 20, 80, 30);
Object v2 = graph.insertVertex(parent, null, "World!", 300, 150, 80, 30);
graph.insertEdge(parent, null, "Edge", v1, v2);
}
finally{
graph.getModel().endUpdate();
}
mxGraphComponent graphComponent = new mxGraphComponent(graph);
this.add(graphComponent);
}
public void Update(){
}
}
}
Upvotes: 2
Views: 1590
Reputation: 41
always set a layout to the panels
public MyGraph(){
...
getContentPane().setLayout(new BorderLayout());
getContentPane().add(Gpanel, BorderLayout.CENTER);
}
class NewPanel extends JPanel{
...
NewPanel(){
...
this.setLayout(new BorderLayout());
this.add(graphComponent, BorderLayout.CENTER);
}
}
Upvotes: 1