views:

50

answers:

5

I am using the following:

java.awt.Container.add(Component comp, Object constraints)

How do I specificy the constraints object? I need to be able to place a component within the container.

Oh and my class extends JInternalFrame if this helps...

I need to specify coordinates to place the component within the container

A: 

It depends on the layout manager you are using. For example, if you are using a BorderLayout, then you could use values like BorderLayout.CENTER and BorderLayout.NORTH. If you are not using a layout manager, then you need to set the position of the components manually.

Jeff Storey
How do I do this manually? I need to basically give it "coordinates" to specify its position. I just don't know how to construct the "constraint" object.
llm
You can pass `null` for the constraint object and use `setBounds` or `setLocation` on the component to position it.
Carl Smotricz
You don't need to pass any constraints to the add-method if you set bounds and location manually. The layout-manager however should be set to `null`.
aioobe
@aioobe: You're correct, of course. I haven't *not* used LayoutManagers in so long I didn't have it quite right.
Carl Smotricz
A: 

The proper constraints object depends on the current LayoutManager.

If you're using BorderLayout for instance, the constraints object could for instance be BorderLayout.SOUTH.

aioobe
+2  A: 

Look at the tutorials for LayoutManagers! The examples will show you which constraints are used with which layouts, and how.

Carl Smotricz
+1, for referring to the tutorial. This is obviously a beginner and the tutorial is full of examples and can explain what constraints are in more detail than the simple one line sentences in the other answers given. Not only that hopefully the poster will look at the "table of contents" to learn other basic Swing features before posting the next question.
camickr
+1  A: 

The constraints objects depends on which layout manager you are using.

For example with a BorderLayout you will have just some constants: container.add(element, BorderLayout.CENTER)

While if the layout manager of the container is a GridBagLayout you will have a GridBagConstraints object with the specified parameters.

Some layout managers (like FlowLayout or GridLayout) doesn't need any kind of constraint since they actually decide how to place things by themselves.

As a side note, if you need absolute positioning you won't have any layout manager:

container.seLayout(null);
container.add(element1);

Insets insets = pane.getInsets();
element1.setBounds(..); //here you set absolute position
Jack
Thanks. This almost worked. If I am using setBounds to size a button for instance, is there anyway I can get a "default" height and width for the button (i.e. autosized to the size of its display text) so that when i call setBounds I don't have to specify a height and width (just an x,y position)?
llm
You can precompute them once and use for every button that you add, to derive the size according to the font you should use __component.getFontMetrics()__, then you'll have desired methods (like __stringWitdh("foobar")__ ) to obtain the dimensions..
Jack
I actually ended up using "componentName".getMinimumSize().width/height in case anyone runs into this.
llm
That is a lot of work for a simple problem. You should NOT be using a null layout. Layout managers exist for a reason. Do some reading of the tutorial and save yourself some trouble in the long run.
camickr
+1  A: 

From java.awt.Container class'es javadoc:

The constraints are defined by the particular layout manager being used. For example, the BorderLayout class defines five constraints: BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.EAST, BorderLayout.WEST, and BorderLayout.CENTER.

The GridBagLayout class requires a GridBagConstraints object. Failure to pass the correct type of constraints object results in an IllegalArgumentException.

This comment can be found at the protected addImpl method.

Andreas_D