Public Group

# java container question

This topic is 4092 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Is there a way to specify the coordinates that a panel or button will appear at in a container. Right now, all I can seem to find is c.add(button1, BorderLayout.SOUTH)// and north, east etc. My question is if you can tell it to appear at specific x,y coords instead of just a general area of the screen. Thanks

##### Share on other sites
http://www.gamedev.net/community/forums/topic.asp?topic_id=445679

##### Share on other sites
Java uses LayoutManager instances to determine how to arrange components. There are several different layout techniques. What you are describing is the BorderLayout class, which is the default for most components.

Generally, when working with AWT or Swing you add things like Buttons and Labels to a Panel, then add the panel instance to the main container. Using that technique with BorderLayout can get you a long way. GridLayout, GridBagLayout, and BoxLayout can help you with more complex layouts.

If you really want, you can set the layout of a Container to null and then place components precisely by position. That's not really a good idea, though. LayoutManagers take care of a lot of things behind the scenes for you, such as making sure the components are positioned properly when the window is resized.

IIRC, NetBeans has a custom layout manager (as part of its GUI designer) that allows you to position components by coordinates, while still taking care of the bookkeeping for you (or it used to, at least).

##### Share on other sites
Hi,

I saw your code and I think you forgot to add getContentPane().setLayout(new BorderLayout()). But that isn't you problem...

You need to set the layout of the container to null and explicitly set the location and size of every component you add. If we take your button1, you should call:

button1.setLocation(10, 10); //10x, 10y from the topleft cornerbutton1.setBounds(100, 30);

See for yourself:

Swing tutorial

Not using a Layout Manager

##### Share on other sites
Alright, thanks guys. I'll take try out your suggestions.

@StarikK
yup, I have getContentPane earlier in my code. Don't worry

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
17
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002297
×