0

I have tried several ways, but still havent found the solution. I have a jgraph in a frame and I want to add a Jbutton in that frame also in a specific location. However I only get one of them when i run the program, because they expand to the whole window. Any ideas how to fix this?

Thanks in advance.

public class GUIquery extends JFrame {

    JFrame frame;
    static JGraph jgraph;
    final mxGraph graph = new mxGraph();
    final mxGraphComponent graphComponent = new mxGraphComponent(graph);

    public GUIquery() {
        super("Test");
        GraphD();
        imgbtn();
    }

    public void GraphD() {
        Object parent = graph.getDefaultParent();
        graph.getModel().beginUpdate();
        try {
          ........         
        }catch  {
          ........  
        } finally  {
           graph.getModel().endUpdate();
        }
        getContentPane().add(graphComponent);    
    }    

    public void imgbtn() {
        JPanel jpanel = new JPanel();
        jpanel.setSize(100, 100);
        jpanel.setLocation(1200, 60);
        JButton imgbtn = new JButton("Export as Image");
        imgbtn.setSize(100, 100);
        imgbtn.setLocation(1200, 60);
        jpanel.add(imgbtn);
        add(jpanel);
    }

    public static void main(String[] args) {
        GUIquery frame = new GUIquery();
        frame.setLayout(null);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 320);
        frame.setVisible(true);
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2598911
  • 379
  • 2
  • 6
  • 22

1 Answers1

1

Don't use null layouts. They inevitably result in trouble.

From your code snippet it is impossible to tell where you want them to be relative to each other, the following puts the button below the graph.

The content pane uses BorderLayout by default. For BorderLayout, you need to use place components at different positions:

// the default position, but it does not hurt to be explicit
add(graph, BorderLayout.CENTER);

...

// and the panel
add(jpanel, BorderLayout.SOUTH);

If the positioning is not what you want, take a look at the visual guide to layout managers to pick the layout manager that suits your needs best.

In the button panel the setLocation() and setSize() calls are useless. The layout manager of the panel is responsible for setting the button's bounds. If the default FlowLayout is not what you want for it, use the guide to pick another for the panel too.

kiheru
  • 6,588
  • 25
  • 31
  • Thanks for your reply. This must be it. add(graph..) though wont apply in the method – user2598911 Sep 15 '13 at 17:51
  • I want the graph and button next to each other – user2598911 Sep 15 '13 at 18:11
  • Adding to the frame is the same as adding to the content pane. I didn't mean they'd be in the same method, just somewhere in the code. For placing the button right of the graph you can use `BorderLayout.RIGHT`. See the [BorderLayout tutorial](http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html) for more. Take also a look at the other layout manager images to check if something is closer to what you want. – kiheru Sep 15 '13 at 18:16
  • i add them in separate methods. the add for button in the button method works fine. But the add(graph, BorderLayout.CENTER); in the GraphD method wont be accepted. It says "The method add(String, Component) in the type Container is not applicable for the arguments (mxGraph, String)" – user2598911 Sep 15 '13 at 18:38
  • Ok i got it right. I had to add the graph component and not graph. My problem is though that none of these layouts work for me. I just need a small button, like as if it is inside the graph – user2598911 Sep 15 '13 at 18:47
  • OK, good. The best could be making a new question with an image of the desired layout. The issue has changed from "How to make the components visible?" to "How to get the exact layout I want?". Without seeing an image of the wanted result i would be pure guesswork to give any suggestions. Include an example attempt and preferably an [SSCCE](http://sscce.org/) as usual. – kiheru Sep 15 '13 at 18:55