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

Discussion in 'Java' started by Tom Parson, Nov 14, 2005.

  1. Tom Parson

    Tom Parson Guest

    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 Parson, Nov 14, 2005
    #1
    1. Advertising

  2. Tom Parson

    Vova Reznik Guest

    Tom Parson wrote:
    > 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).
     
    Vova Reznik, Nov 14, 2005
    #2
    1. Advertising

  3. Tom Parson

    Oliver Wong Guest

    "Tom Parson" <> wrote in message
    news:4378ce45$0$7424$-online.net...
    >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
     
    Oliver Wong, Nov 14, 2005
    #3
  4. Tom Parson wrote:

    > 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.
     
    Andrew Thompson, Nov 14, 2005
    #4
  5. Tom Parson wrote:
    > 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.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Nov 15, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. apchar
    Replies:
    3
    Views:
    1,850
    Andrew Hobbs
    Feb 3, 2004
  2. Scott Steiner

    Setting insets for a contentPane?

    Scott Steiner, Jul 19, 2005, in forum: Java
    Replies:
    2
    Views:
    6,705
    Vova Reznik
    Jul 19, 2005
  3. Vivi Orunitia
    Replies:
    11
    Views:
    4,481
    Martijn Lievaart
    Feb 4, 2004
  4. Vaxius
    Replies:
    3
    Views:
    3,460
  5. Phi!
    Replies:
    1
    Views:
    182
Loading...

Share This Page