GridbagLayout basics

Discussion in 'Java' started by Scott Steiner, Jul 18, 2005.

  1. Hi,

    I want to get the following arrangment of buttons:

    # button 0 #
    # button 1 ## button 2#

    but I'm getting this instead:

    # button 0 #
    # button 1 #

    here's the code, what am I doing wrong?

    pane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;

    JButton button;

    button = new JButton("Button 0");
    c.weightx = 1;
    c.gridwidth = 2;
    c.gridx = 0;
    c.gridy = 0;
    pane.add(button, c);

    button = new JButton("Button 1");
    c.weightx = 0.5;
    c.gridx = 0;
    c.gridy = 1;
    pane.add(button, c);

    button = new JButton("Button 2");
    c.weightx = 0.5;
    c.gridx = 1;
    c.gridy = 1;
    pane.add(button, c);
     
    Scott Steiner, Jul 18, 2005
    #1
    1. Advertising

  2. Scott Steiner wrote:
    > Hi,
    >
    > I want to get the following arrangment of buttons:
    >
    > # button 0 #
    > # button 1 ## button 2#
    >
    > but I'm getting this instead:
    >
    > # button 0 #
    > # button 1 #
    >
    > here's the code, what am I doing wrong?


    > button = new JButton("Button 0");
    > c.weightx = 1;
    > c.gridwidth = 2;
    > c.gridx = 0;
    > c.gridy = 0;
    > pane.add(button, c);
    >
    > button = new JButton("Button 1");
    > c.weightx = 0.5;

    c.gridwidth = 1;
    > c.gridx = 0;
    > c.gridy = 1;
    > pane.add(button, c);
    > [...]


    Tom Hawtin
    --
    Unemployed English Java programmer
     
    Thomas Hawtin, Jul 18, 2005
    #2
    1. Advertising

  3. Scott Steiner

    Carl Guest

    Scott Steiner wrote:
    > Hi,
    >
    > I want to get the following arrangment of buttons:
    >
    > # button 0 #
    > # button 1 ## button 2#
    >
    > but I'm getting this instead:
    >
    > # button 0 #
    > # button 1 #
    >
    > here's the code, what am I doing wrong?
    >
    > pane.setLayout(new GridBagLayout());
    > GridBagConstraints c = new GridBagConstraints();
    > c.fill = GridBagConstraints.HORIZONTAL;
    >
    > JButton button;
    >
    > button = new JButton("Button 0");
    > c.weightx = 1;
    > c.gridwidth = 2;
    > c.gridx = 0;
    > c.gridy = 0;
    > pane.add(button, c);
    >
    > button = new JButton("Button 1");
    > c.weightx = 0.5;
    > c.gridx = 0;
    > c.gridy = 1;
    > pane.add(button, c);
    >
    > button = new JButton("Button 2");
    > c.weightx = 0.5;
    > c.gridx = 1;
    > c.gridy = 1;
    > pane.add(button, c);


    One problem I see is that you set the gridwidth to "2", but never set it
    back to 1 for the buttons you want to appear side by side. I believe
    that will solve your problem.

    Carl.
     
    Carl, Jul 18, 2005
    #3
  4. Scott Steiner

    Roedy Green Guest

    On Mon, 18 Jul 2005 21:13:06 +0200, Scott Steiner
    <> wrote or quoted :

    >button = new JButton("Button 0");
    >c.weightx = 1;
    >c.gridwidth = 2;
    >c.gridx = 0;
    >c.gridy = 0;
    >pane.add(button, c);
    >
    >button = new JButton("Button 1");
    >c.weightx = 0.5;
    >c.gridx = 0;
    >c.gridy = 1;
    >pane.add(button, c);
    >
    >button = new JButton("Button 2");
    >c.weightx = 0.5;
    >c.gridx = 1;
    >c.gridy = 1;
    >pane.add(button, c);


    see
    http://mindprod.com/jgloss/gridbaglayout.html


    You should not be reusing your constraint object. It leads too easily
    to errors like this where you set gridwidth=2 for all your buttons.
    You need to set it back to 1.

    --
    Bush crime family lost/embezzled $3 trillion from Pentagon.
    Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
    http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
     
    Roedy Green, Jul 18, 2005
    #4
  5. Scott Steiner

    David Segall Guest

    Roedy Green <> wrote:

    Welcome back Roedy. I have missed your advice.
     
    David Segall, Jul 19, 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. Passero

    grid or gridbaglayout

    Passero, Jul 2, 2003, in forum: Java
    Replies:
    3
    Views:
    3,109
    dhek bhun kho
    Jul 2, 2003
  2. me
    Replies:
    2
    Views:
    4,510
    Sudsy
    Jul 21, 2003
  3. David McCallum

    GridBagLayout Example

    David McCallum, Oct 23, 2003, in forum: Java
    Replies:
    0
    Views:
    690
    David McCallum
    Oct 23, 2003
  4. John Davison

    Viewing GridBagLayout's Grid Lines?

    John Davison, Dec 9, 2003, in forum: Java
    Replies:
    6
    Views:
    4,345
    Todd Corley
    Dec 10, 2003
  5. AJ
    Replies:
    6
    Views:
    714
Loading...

Share This Page