Place components at arbitrary (x,y) coordinates

vlad-ardelean picture vlad-ardelean · Jan 6, 2012 · Viewed 12k times · Source

I want to place some buttons in a JPanel at random positions (x,y), and these layout classes are annoying.

Is this even possible in Swing?

Answer

Stephan picture Stephan · Jan 6, 2012

You can set the coordinates if you use a null layout:

panel.setLayout(null);
Button b = new Button(....);
panel.add(b);
b.setSize(width, height);
b.setLocation(x,y);

But it is strongly recommended to use layouts. Layout classes are not "annoying", they are your friend if you understand them properly.

I propose reading a tutorial about GridBagLayout, it is easy to understand (kinda html tables) and very powerful.