gridbag layout

Discussion in 'Java' started by asit, Jul 17, 2009.

  1. asit

    asit Guest

    I was recently studying GridBagLayout. As I have insuffecient material
    ab GridBagLayout, I tried to learn from experiments.

    Here is a code I wrote,

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

    public class Main {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    JFrame frm = new JFrame("Login");
    frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();

    JLabel loginHeading = new JLabel("Login Window");
    JLabel userId = new JLabel("User Id : ");
    JLabel passwd = new JLabel("Password : ");

    JButton login = new JButton("Login");

    JTextField userIdField = new JTextField(10);
    JTextField passwdField = new JTextField(10);

    GridBagLayout gbg = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    frm.setLayout(gbg);

    gbc.anchor = GridBagConstraints.NORTH;
    gbc.insets = new Insets(25,25,0,0);


    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbg.setConstraints(loginHeading, gbc);


    gbc.gridwidth = GridBagConstraints.RELATIVE;
    gbg.setConstraints(userId, gbc);
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbg.setConstraints(userIdField, gbc);

    gbc.gridwidth = GridBagConstraints.RELATIVE;
    gbg.setConstraints(passwd, gbc);
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbg.setConstraints(passwdField, gbc);

    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbg.setConstraints(login, gbc);

    frm.add(loginHeading);
    frm.add(userId);
    frm.add(userIdField);
    frm.add(passwd);
    frm.add(passwdField);
    frm.add(login);

    //frm.pack();
    frm.setSize(400,500);
    frm.setVisible(true);


    }

    }

    It works fine. But how can I add spacing between the heading label and
    the rest of the labels ???
    asit, Jul 17, 2009
    #1
    1. Advertising

  2. asit wrote:
    > I was recently studying GridBagLayout. As I have insuffecient material
    > ab GridBagLayout, I tried to learn from experiments.
    >
    > Here is a code I wrote,
    >
    > import javax.swing.*;
    > import java.awt.*;
    >
    > public class Main {
    >
    > /**
    > * @param args
    > */
    > public static void main(String[] args) {
    > // TODO Auto-generated method stub
    > JFrame frm = new JFrame("Login");
    > frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    >
    > JPanel panel = new JPanel();
    >
    > JLabel loginHeading = new JLabel("Login Window");
    > JLabel userId = new JLabel("User Id : ");
    > JLabel passwd = new JLabel("Password : ");
    >
    > JButton login = new JButton("Login");
    >
    > JTextField userIdField = new JTextField(10);
    > JTextField passwdField = new JTextField(10);
    >
    > GridBagLayout gbg = new GridBagLayout();
    > GridBagConstraints gbc = new GridBagConstraints();
    > frm.setLayout(gbg);
    >
    > gbc.anchor = GridBagConstraints.NORTH;
    > gbc.insets = new Insets(25,25,0,0);
    >
    >
    > gbc.gridwidth = GridBagConstraints.REMAINDER;
    > gbg.setConstraints(loginHeading, gbc);
    >
    >
    > gbc.gridwidth = GridBagConstraints.RELATIVE;
    > gbg.setConstraints(userId, gbc);
    > gbc.gridwidth = GridBagConstraints.REMAINDER;
    > gbg.setConstraints(userIdField, gbc);
    >
    > gbc.gridwidth = GridBagConstraints.RELATIVE;
    > gbg.setConstraints(passwd, gbc);
    > gbc.gridwidth = GridBagConstraints.REMAINDER;
    > gbg.setConstraints(passwdField, gbc);
    >
    > gbc.gridwidth = GridBagConstraints.REMAINDER;
    > gbg.setConstraints(login, gbc);
    >
    > frm.add(loginHeading);
    > frm.add(userId);
    > frm.add(userIdField);
    > frm.add(passwd);
    > frm.add(passwdField);
    > frm.add(login);
    >
    > //frm.pack();
    > frm.setSize(400,500);
    > frm.setVisible(true);
    >
    >
    > }
    >
    > }
    >
    > It works fine. But how can I add spacing between the heading label and
    > the rest of the labels ???


    Here is one way. I wrote a GridBagLayout simulator that you can play
    with. It's not the easiest thing to use but it is handy if you want to
    try some constraints without programming them.

    http://rabbitbrush.frazmtn.com/gridbagtester.html

    Oh, and please try not to post tabs, it makes a mess out of code
    listings for most everybody else that doesn't use them.

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

    public class Main {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    JFrame frm = new JFrame("Login");
    frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();

    JLabel loginHeading = new JLabel("Login Window");
    JLabel userId = new JLabel("User Id : ");
    JLabel passwd = new JLabel("Password : ");

    JButton login = new JButton("Login");

    JTextField userIdField = new JTextField(10);
    JTextField passwdField = new JTextField(10);

    GridBagLayout gbg = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    frm.setLayout(gbg);

    gbc.gridy = 0; gbc.gridwidth = 2;
    frm.add(loginHeading,gbc);
    gbc.insets = new Insets(75,25,0,0);
    ++gbc.gridy; gbc.gridwidth = 1;
    frm.add(userId,gbc);
    frm.add(userIdField,gbc);
    ++gbc.gridy;
    gbc.insets = new Insets(25,25,0,0);
    frm.add(passwd,gbc);
    frm.add(passwdField,gbc);
    ++gbc.gridy; gbc.gridwidth = 2;
    frm.add(login,gbc);

    frm.pack();
    //frm.setSize(400,500);
    frm.setVisible(true);


    }

    }

    --

    Knute Johnson
    email s/nospam/knute2009/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, Jul 17, 2009
    #2
    1. Advertising

  3. In article <>,
    "Peter Duniho" <> wrote:

    > On Fri, 17 Jul 2009 11:22:23 -0700, asit <> wrote:
    >
    > > [...]
    > > It works fine. But how can I add spacing between the heading label and
    > > the rest of the labels ???

    >
    > I don't know if it's the best way, but I have used
    > Box.createHorizontalStrut() and Box.createVerticalStrut() for making
    > "spacers" within a layout. There is also the Box.Filler class.


    GridBag is very powerful, but I like struts and glue from the Box class,
    too. The tutorial has a nice section on the topic under "Using Invisible
    Components as Filler":

    <http://java.sun.com/docs/books/tutorial/uiswing/layout/box.html#filler>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
    John B. Matthews, Jul 17, 2009
    #3
  4. John B. Matthews wrote:
    > In article <>,
    > "Peter Duniho" <> wrote:
    >
    >> On Fri, 17 Jul 2009 11:22:23 -0700, asit <> wrote:
    >>
    >>> [...]
    >>> It works fine. But how can I add spacing between the heading label and
    >>> the rest of the labels ???

    >> I don't know if it's the best way, but I have used
    >> Box.createHorizontalStrut() and Box.createVerticalStrut() for making
    >> "spacers" within a layout. There is also the Box.Filler class.

    >
    > GridBag is very powerful, but I like struts and glue from the Box class,
    > too. The tutorial has a nice section on the topic under "Using Invisible
    > Components as Filler":
    >
    > <http://java.sun.com/docs/books/tutorial/uiswing/layout/box.html#filler>


    Fits most of my needs too. Nested BoxLayouts and Box class components go
    a long way sometimes. But sometimes I do need GridBagLayout, which
    usually means going to the Java Tutorial page on the subject for reference.
    Angel Velazquez, Jul 23, 2009
    #4
  5. asit

    Roedy Green Guest

    On Fri, 17 Jul 2009 11:22:23 -0700 (PDT), asit <>
    wrote, quoted or indirectly quoted someone who said :

    >It works fine. But how can I add spacing between the heading label and
    >the rest of the labels ???


    see http://mindprod.com/jgloss/gridbaglayout.html
    to learn how to persuade GridBagLayout to sit up and beg.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    "The industrial civilisation is based on the consumption of energy resources that are inherently limited in quantity, and that are about to become scarce. When they do, competition for what remains will trigger dramatic economic and geopolitical events; in the end, it may be impossible for even a single nation to sustain industrialism as we have know it in the twentieth century."
    ~ Richard Heinberg, The Party’s Over: Oil, War, and the Fate of Industrial Societies
    Roedy Green, Jul 23, 2009
    #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. Rick Spiewak
    Replies:
    3
    Views:
    3,147
    Rick Spiewak
    Aug 26, 2003
  2. Loki
    Replies:
    1
    Views:
    720
    Thomas G. Marshall
    Jun 29, 2003
  3. Loki
    Replies:
    1
    Views:
    720
  4. Replies:
    1
    Views:
    570
    John Timney \(MVP\)
    Jun 19, 2006
  5. nukleus
    Replies:
    2
    Views:
    370
    nukleus
    Jan 31, 2007
Loading...

Share This Page