setWidth(44= of a Container/ContentPane does not work

T

Tom Parson

I would like to set the width of a Container/ContentPane to 400 pixel.
But the following does NOT work:

How do I do it otherwise?

Container contentPane = getContentPane();
GridBagLayout gridbag = new GridBagLayout();
contentPane.setLayout(gridbag);
contentPane.setWidth(400); -->Compiler Err

Tom
 
V

Vova Reznik

Tom said:
I would like to set the width of a Container/ContentPane to 400 pixel.
But the following does NOT work:

How do I do it otherwise?

Container contentPane = getContentPane();
GridBagLayout gridbag = new GridBagLayout();
contentPane.setLayout(gridbag);
contentPane.setWidth(400); -->Compiler Err

Tom

java.awt.Container and its supers don't have method setWidth(int).

java.awt.Component has method setSize(int, int) or setSize(Dimension)

Changing size of the content pane isn't very good idea (but possible).
 
O

Oliver Wong

Tom Parson said:
I would like to set the width of a Container/ContentPane to 400 pixel.
But the following does NOT work:

How do I do it otherwise?

Container contentPane = getContentPane();
GridBagLayout gridbag = new GridBagLayout();
contentPane.setLayout(gridbag);
contentPane.setWidth(400); -->Compiler Err

Assuming you're talking about java.awt.Container, it doesn't have a
setWidth() size. Did you try setSize() instead? Did you try reading the
JavaDocs? http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Container.html

- Oliver
 
A

Andrew Thompson

Tom said:
contentPane.setWidth(400); -->Compiler Err

Unless you understand the complexities behind setting
exact sizes in x-plat (version, PLAF, screen/font size)
environment, you generally shouldn't.

Use layouts (probably less complex than GBL), and allow the
components to arrive at their natural sizes.
 
K

Knute Johnson

Tom said:
I would like to set the width of a Container/ContentPane to 400 pixel.
But the following does NOT work:

How do I do it otherwise?

Container contentPane = getContentPane();
GridBagLayout gridbag = new GridBagLayout();
contentPane.setLayout(gridbag);
contentPane.setWidth(400); -->Compiler Err

Tom

Tom:

That's because there is no setWidth() for Container. There is a
setSize() and a setPreferredSize(). You will have problems though
setting the specific size of a Container when you use a LayoutManager
that resizes components like GridBagLayout.

Maybe if you told us why you wanted it to be 400 pixels exactly we could
give you a better answer. I can tell you though that setting exact
sizes in Java is not the normal way things are done and you will
probably get a lot of flack for suggesting it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top