I have created a JFrame
- now I want to add the 4 JPanel
in that frame at a particular location. How can set the location of panels in the frame?

- 168,117
- 40
- 217
- 433

- 366
- 4
- 13
- 25
-
4Read the swing tutorial about layout managers: http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html – JB Nizet Jun 12 '12 at 07:38
-
2You really should read documentation or make some research efforts. I think you could find the answer by your own. – Jean-Rémy Revy Jun 12 '12 at 07:45
5 Answers
Use (possibly nested1) layouts for the logic. See Laying Out Components Within a Container for details. They can:
- Include default spacing in the constructor (often)
- Calculate how big the GUI needs to be in order to display the components (in whatever PLAF, on whatever system the app. is deployed).
Extra spacing can be organized by adding an EmtpyBorder
to child components.
- See the nested layout example

- 1
- 1

- 168,117
- 40
- 217
- 433
Placing components in a container is quite a complicated subject in Swing. Instead of defining the exact places for your components, you would normally use a layout manager that arranges them in a certain way.
Here is the tutorial you should read to get a (visual) clue about the different layout managers: A Visual Guide to Layout Managers
However, the standard layout managers of Swing can be cumbersome for more complex layouts. Either, you could use nested layouts to get the desired result, or you could use a very powerful third-party library: JGoodies Forms. The downside is of course that you have to learn yet another library. Therefore, I would only recommend it for a bigger project.

- 10,083
- 4
- 55
- 75
-
*"Placing components in a container is quite a complicated subject in.."* ..a multi-platform, multi-PLAF application. – Andrew Thompson Jun 12 '12 at 07:43
If its 4 locations, you can use BorderLayout
,by default its the CENTRE, but it also have EAST, WEST , NORTH, SOUTH locations for the placement of the components. You can also use setLocation
to put the panels in the appropriate locations, if a layout isn't used
.
Its even better to use GroupLayout
developed my NetBeans team in 2005, use Windows Builder Pro, now provided by google for free.

- 33,294
- 6
- 48
- 75
-
+1 for `GroupLayout` -1 for mentioning `setBounds` - net result 0. – Andrew Thompson Jun 12 '12 at 07:45
-
That answer has become better with the edit, but now you removed `setBounds` I notice you were referring to a `BorderLayout` (as clearly stated in the 1st sentence), as opposed to a `null` layout, where `setBounds` might not be ignored. So.. on the matter of the `BorderLayout`, it is the constraints used when adding components (e.g. `CENTER`, `NORTH`/`PAGE_START`) that does the trick, rather than `setLocation`. If you were to make that change I could up-vote the answer.. – Andrew Thompson Jun 12 '12 at 13:44
For me it is good way to set GridbagLayout for the container of the frame. There are several visual swing GUI editors available to do this easily. You can use NetBeans GUI editor or GWT Designer (https://developers.google.com/web-toolkit/tools/gwtdesigner/) for complex GUI designing tasks

- 468
- 2
- 14
- set the layout of the Frame to be null via
setLayout(null)
- create 4 JPanel and set their location using
setLocation
method - add these panels using JFrame's
add
method

- 852
- 7
- 20