java container question

Started by
3 comments, last by omgh4x0rz 16 years, 11 months ago
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
Advertisement
http://www.gamedev.net/community/forums/topic.asp?topic_id=445679
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).
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
Next time I give my advice, I'll buy some bubblegum so I won't your kick ass!
Alright, thanks guys. I'll take try out your suggestions.

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

This topic is closed to new replies.

Advertisement