Sign in to follow this  
omgh4x0rz

java container question

Recommended Posts

omgh4x0rz    122
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 this post


Link to post
Share on other sites
Aldacron    4544
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 this post


Link to post
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 corner
button1.setBounds(100, 30);




See for yourself:

Swing tutorial

Not using a Layout Manager

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this