Using GridBagLayout with JFrame

Discussion in 'Java' started by KingKongBundy, Aug 24, 2004.

  1. Could anyone tell me how I would modify this code (in the
    gridbaglayout if possible) so that when the JFrame pops up, there is
    'white space' between the JLabel and JButton. Currently, when this
    pops up, the JLabel and JButton are on top of one another. When I
    comment the line 'dialog.pack();' out, I get the desired JFrame look
    (in terms of white space), but the entire JLabel message does not
    display without having to re-sizing the window.

    Thanks!!



    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.text.*;

    public class Question
    {
    public static void main(String[] args)
    {
    String greetMessage = "Hello world - Hopefully this will work";
    GreetingMessage dialog = new GreetingMessage(greetMessage);
    dialog.pack();
    dialog.show();
    }
    } //Question

    class GreetingMessage extends JFrame
    {
    public GreetingMessage(String message)
    {
    Toolkit theKit = getToolkit();

    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    Container contentPane = getContentPane();
    contentPane.setLayout(layout);


    //Set constraints for JLabel
    constraints.weightx = 0.0;
    constraints.weighty = 1.0;
    constraints.gridx = 0;
    constraints.gridy = 0;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.anchor = GridBagConstraints.CENTER;

    JLabel newLabel = new JLabel(message);
    layout.setConstraints(newLabel,constraints);

    contentPane.add(newLabel);

    constraints.fill = constraints.NONE;
    constraints.weightx = 2.0;
    constraints.weighty = 2.0;
    constraints.gridx = 0;
    constraints.gridy = 100;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.anchor = GridBagConstraints.CENTER;

    JButton ok = new JButton("Ok");
    layout.setConstraints(ok,constraints);
    contentPane.add(ok);
    setSize(400,400);

    } //GreetingMessage constructor
    } //GreetingMessage
     
    KingKongBundy, Aug 24, 2004
    #1
    1. Advertising

  2. On 24 Aug 2004 09:16:32 -0700, KingKongBundy wrote:

    > Could anyone tell me how I would modify this code (in the
    > gridbaglayout if possible)


    Why? What is the logic of using a GBL in
    the first place? A nested Layout is often
    simpler to construct, and more reliable.

    >..so that when the JFrame pops up, there is
    > 'white space' between the JLabel and JButton. Currently, when this
    > pops up, the JLabel and JButton are on top of one another. When I
    > comment the line 'dialog.pack();' out,


    If removing pack(), 'fixes' a lyout, it has
    serious problems..

    >I get the desired JFrame look
    > (in terms of white space), but the entire JLabel message does not
    > display without having to re-sizing the window.


    Try this non BGL equivalent, note that the
    button traverses the width of the bottom, but
    you could change that by adding it inside a JPanel
    with a FlowLayout (*nested* inside the current
    BorderLayout)

    <sscce>
    import java.awt.*;
    import javax.swing.*;

    public class Question {

    public static void main(String[] args) {
    String greetMessage = "Hello world - Hopefully this will work";
    GreetingMessage dialog = new GreetingMessage(greetMessage);
    dialog.pack();
    // dialog.show(); //deprecated as of 1.5
    dialog.setVisible(true); //future proofing ..
    }
    } //Question

    class GreetingMessage extends JFrame {

    public GreetingMessage(String message)
    {
    //Toolkit theKit = getToolkit();
    //what's that here for? it does nothing

    //GridBagLayout layout = new GridBagLayout();
    // no longer used, this is a layout you should rarely need.

    Container contentPane = getContentPane();
    contentPane.setLayout(new BorderLayout(15,15));

    JLabel newLabel = new JLabel(message);

    contentPane.add(newLabel, BorderLayout.NORTH);

    JButton ok = new JButton("Ok");
    contentPane.add(ok, BorderLayout.SOUTH);
    setSize(400,400);

    } //GreetingMessage constructor
    } //GreetingMessage
    </sscce>

    BTW, for GUI matter, you'd best post to..
    <http://www.physci.org/codes/javafaq.jsp#cljg>

    HTH

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Aug 24, 2004
    #2
    1. Advertising

  3. KingKongBundy

    Sudsy Guest

    KingKongBundy wrote:
    > Could anyone tell me how I would modify this code (in the
    > gridbaglayout if possible) so that when the JFrame pops up, there is
    > 'white space' between the JLabel and JButton. Currently, when this
    > pops up, the JLabel and JButton are on top of one another. When I
    > comment the line 'dialog.pack();' out, I get the desired JFrame look
    > (in terms of white space), but the entire JLabel message does not
    > display without having to re-sizing the window.


    The gridy for the OK button should be 1, not 100. Also, the weightx
    and weighty should be something like 0.5 and should be the same for
    both elements.
    You could clean up your code a lot by only specifying those values
    which are changing, i.e. replace all this:

    constraints.fill = constraints.NONE;
    constraints.weightx = 2.0;
    constraints.weighty = 2.0;
    constraints.gridx = 0;
    constraints.gridy = 100;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;

    with this:

    constraints.fill = constraints.NONE;
    constraints.gridy++;

    Incrementing gridx and gridy instead of setting to absolute values
    (excepting when resetting gridx to 0 to start a new row) allows a
    bit more flexibility for future modifications IMHO.
     
    Sudsy, Aug 24, 2004
    #3
  4. KingKongBundy wrote:

    >Could anyone tell me how I would modify this code (in the
    >gridbaglayout if possible) so that when the JFrame pops up, there is
    >'white space' between the JLabel and JButton. Currently, when this
    >pops up, the JLabel and JButton are on top of one another. When I
    >comment the line 'dialog.pack();' out, I get the desired JFrame look
    >(in terms of white space), but the entire JLabel message does not
    >display without having to re-sizing the window.
    >
    >

    ....

    > //Set constraints for JLabel
    > constraints.weightx = 0.0;
    > constraints.weighty = 1.0;
    > constraints.gridx = 0;
    > constraints.gridy = 0;
    > constraints.gridwidth = 1;
    > constraints.gridheight = 1;
    > constraints.anchor = GridBagConstraints.CENTER;
    >

    constraints.insets = new Insets(15,15,15,15);
    // See at http://java.sun.com/j2se/1.4.2/docs/api/java/awt/GridBagConstraints.html#insets

    > JLabel newLabel = new JLabel(message);
    > layout.setConstraints(newLabel,constraints);
    >
    > contentPane.add(newLabel);
    >
    > constraints.fill = constraints.NONE;
    > constraints.weightx = 2.0;
    > constraints.weighty = 2.0;
    > constraints.gridx = 0;
    > constraints.gridy = 100;
    > constraints.gridwidth = 1;
    > constraints.gridheight = 1;
    > constraints.anchor = GridBagConstraints.CENTER;
    >

    constraints.insets = new Insets(15,15,15,15);

    > JButton ok = new JButton("Ok");
    > layout.setConstraints(ok,constraints);
    > contentPane.add(ok);
    > setSize(400,400);
    >

    // BTW: setSize not necessary here. Size will be set by pack() anyway.

    --
    Thomas<dot>Fritsch<squiggle>ops<dot>de
     
    Thomas Fritsch, Aug 24, 2004
    #4
  5. KingKongBundy

    Madhur Ahuja Guest

    Sudsy wrote:
    > KingKongBundy wrote:


    >
    > Also, the weightx
    > and weighty should be something like 0.5 and should be the same for
    > both elements.


    Hello

    Why!, If the weightx and weighty are same for both elements, will it make a
    difference
    what value it is? After all, they are just proportions.


    --
    Winners dont do different things, they do things differently.

    Madhur Ahuja
    India

    Homepage : http://madhur.netfirms.com
    Email : madhur<underscore>ahuja<at>yahoo<dot>com
     
    Madhur Ahuja, Aug 24, 2004
    #5
  6. KingKongBundy

    Sudsy Guest

    Madhur Ahuja wrote:
    <snip>
    > Why!, If the weightx and weighty are same for both elements, will it make a
    > difference
    > what value it is? After all, they are just proportions.


    From the javadocs:

    > weightx
    >
    > public double weightx
    >
    > Specifies how to distribute extra horizontal space.
    >
    > The grid bag layout manager calculates the weight of a column to be the
    > maximum weightx of all the components in a column. If the resulting

    layout
    > is smaller horizontally than the area it needs to fill, the extra

    space is
    > distributed to each column in proportion to its weight. A column that

    has a
    > weight of zero receives no extra space.
    >
    > If all the weights are zero, all the extra space appears between the grids
    > of the cell and the left and right edges.


    Where it comes into play is if the size of the Container is smaller than
    the Components it contains. Giving priority to one of the Components by
    specifying a higher weightx or weighty will alter the visual rendering.
    In the situation posited by the OP it doesn't seem to make sense to
    prioritize the display, any more than it makes sense to specify a gridy
    of 100 when there are only two Components... :-0
    Having spent a bunch of time with GridBagLayout and GridBagConstraints,
    I'm merely suggesting a beneficial approach. Apparently, YMMV.
     
    Sudsy, Aug 24, 2004
    #6
  7. KingKongBundy

    Cid Guest

    On Wed, 25 Aug 2004 01:43:20 +0530, "Madhur Ahuja" <> wrote:

    >Sudsy wrote:
    >> KingKongBundy wrote:

    >
    >>
    >> Also, the weightx
    >> and weighty should be something like 0.5 and should be the same for
    >> both elements.

    >
    >Hello
    >
    >Why!, If the weightx and weighty are same for both elements, will it make a
    >difference
    >what value it is? After all, they are just proportions.


    If weightx is 0 (which it is by default) then the components won't
    resize at all on that axis (same deal for weighty). If you do set
    values for one or more components on an axis, all those values are
    normalized by Swing internally (range 0..1). So you could set them
    both to .5, 1, or 2534 as long as they're the same.

    If you're going to add more components later it can be conceptually
    simpler to keep track of if you just start out using normalized
    weights to begin with. The Swing tutorial recommends (for this reason)
    that you use weights, by axis, that add up to 1.0. Just makes it
    easier for a reader to visualize.
     
    Cid, Aug 24, 2004
    #7
  8. Thomas Fritsch <> wrote in message news:<>...

    Thanks - the inset statements took care of the problem. I wanted to
    use the GBL for general practice, but the suggestion to use
    BorderLayout above also worked and was a lot simpler. Besten Dank!!
     
    KingKongBundy, Aug 25, 2004
    #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,112
    dhek bhun kho
    Jul 2, 2003
  2. me
    Replies:
    2
    Views:
    4,514
    Sudsy
    Jul 21, 2003
  3. David McCallum

    GridBagLayout Example

    David McCallum, Oct 23, 2003, in forum: Java
    Replies:
    0
    Views:
    691
    David McCallum
    Oct 23, 2003
  4. jakester
    Replies:
    3
    Views:
    1,842
    jakester
    Apr 3, 2007
  5. Replies:
    2
    Views:
    419
    markspace
    Aug 27, 2012
Loading...

Share This Page