help on gridBagLayout

Discussion in 'Java' started by Mr. X., May 14, 2008.

  1. Mr. X.

    Mr. X. Guest

    Helo,
    I think that gridBagLayout is the most complicated of the standard layouts.

    I try to understand the following (see my thoughts after the asterisks ...)
    :

    // (The code is a class that implements the JPanel)
    protected void makebutton(String name,

    GridBagLayout gridbag,

    GridBagConstraints c) {

    JButton button = new JButton(name);

    gridbag.setConstraints(button, c);

    add(button);

    button.addActionListener(this);

    }


    public void initX() {

    GridBagLayout gridbag = new GridBagLayout();

    GridBagConstraints c = new GridBagConstraints();


    setFont(new Font("SansSerif", Font.PLAIN, 14));

    setLayout(gridbag);


    setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

    c.anchor = GridBagConstraints.LINE_END;

    c.weightx = 1;

    c.weighty = 1;

    c.fill = GridBagConstraints.VERTICAL;

    c.anchor = GridBagConstraints.EAST;

    makebutton("ButtonX", gridbag, c);


    c.weightx = 1;

    c.weighty = 1;

    c.gridwidth = GridBagConstraints.REMAINDER;

    c.fill = GridBagConstraints.BOTH;

    c.anchor = GridBagConstraints.EAST;

    makebutton("ButtonY", gridbag, c);

    // *********************************

    // *********************************

    // *********************************

    // ...

    All I want to do is that : ButtonX will be at the east side of the panel,
    and ButtonY will be at the rest of the panel.



    What I get is that buttonX is OK, but buttonY is from left to a place where
    the button seems as a square (what ever I try to do ...)

    Since I am new to gridbaglayout , I need help of the above code, please.

    I would like a good tutorial (that explains with some examples, every method
    & field of GridBagConstraint), please.

    What's wrong with my code ?

    Thanks :)
    Mr. X., May 14, 2008
    #1
    1. Advertising

  2. Mr. X.

    Mr. X. Guest

    ....
    I want that both buttonX and buttonY will fill the whole JPanel (the
    container that they are on).

    Thanks :)
    Mr. X., May 14, 2008
    #2
    1. Advertising

  3. Mr. X.

    Mr. X. Guest

    Thanks ...

    This doesn't much help, since I have problem on the above code, because
    ButtonY doesn't fill the rest of the area of it's container.

    (If I am not wrong, HTML table has width = *, which does the task, which is
    simplier).
    Mr. X., May 14, 2008
    #3
  4. Mr. X. wrote:
    > Helo,
    > I think that gridBagLayout is the most complicated of the standard layouts.


    True. That's one reason why I prefer ....
    - other Layout Managers where appropriate
    - nested layouts
    - MigLayout
    I JGoodies FormLayout is also better in many respects

    >
    > [code snipped]
    >
    > All I want to do is that : ButtonX will be at the east side of the panel,
    > and ButtonY will be at the rest of the panel.
    >
    >
    >
    > What I get is that buttonX is OK, but buttonY is from left to a place where
    > the button seems as a square (what ever I try to do ...)
    >
    > Since I am new to gridbaglayout , I need help of the above code, please.
    >


    Try playing with Knute Johnson's teaching applet
    http://rabbitbrush.frazmtn.com/gridbagtester.html


    > I would like a good tutorial (that explains with some examples, every method
    > & field of GridBagConstraint), please.


    http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html





    --
    RGB
    RedGrittyBrick, May 14, 2008
    #4
  5. Mr. X.

    Roedy Green Guest

    On Wed, 14 May 2008 15:52:29 +0300, "Mr. X."
    <no_spam_please@nospam_please.com> wrote, quoted or indirectly quoted
    someone who said :

    >I think that gridBagLayout is the most complicated of the standard layouts


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

    for some hints on how to make it jump through hoops.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, May 14, 2008
    #5
  6. Mr. X.

    Roedy Green Guest

    On Wed, 14 May 2008 15:52:29 +0300, "Mr. X."
    <no_spam_please@nospam_please.com> wrote, quoted or indirectly quoted
    someone who said :

    >
    >All I want to do is that : ButtonX will be at the east side of the panel,
    >and ButtonY will be at the rest of the panel.


    GridBagConstraints.BOTH
    weightx 100%
    ipadx
    are your tools to make a cell wider
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, May 14, 2008
    #6
  7. Mr. X.

    Mr. X. Guest

    Great !
    That's work.

    A thing I don't understand is,
    why when for both "ButtonX" and "ButtonY", when the weightx = 1,
    then I have the problem I have described (the buttons don't both fill their
    container),
    and when ButtonX's weight=0, and ButtonY=1, everything works fine.

    Thanks :)
    Mr. X., May 14, 2008
    #7
  8. Mr. X. wrote:
    > Great !
    > That's work.
    >
    > A thing I don't understand is,
    > why when for both "ButtonX" and "ButtonY", when the weightx = 1,
    > then I have the problem I have described (the buttons don't both fill their
    > container),
    > and when ButtonX's weight=0, and ButtonY=1, everything works fine.
    >
    > Thanks :)
    >
    >


    Is the container bigger than the buttons? GBL will only take more space
    if there is more space to take.

    --

    Knute Johnson
    email s/nospam/linux/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, May 15, 2008
    #8
    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,086
    dhek bhun kho
    Jul 2, 2003
  2. me
    Replies:
    2
    Views:
    4,476
    Sudsy
    Jul 21, 2003
  3. David McCallum

    GridBagLayout Example

    David McCallum, Oct 23, 2003, in forum: Java
    Replies:
    0
    Views:
    674
    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,254
    Todd Corley
    Dec 10, 2003
  5. AJ
    Replies:
    6
    Views:
    694
Loading...

Share This Page