Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > gridbag layout

Reply
Thread Tools

gridbag layout

 
 
asit
Guest
Posts: n/a
 
      07-17-2009
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 ???
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      07-17-2009
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
 
Reply With Quote
 
 
 
 
John B. Matthews
Guest
Posts: n/a
 
      07-17-2009
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>
 
Reply With Quote
 
Angel Velazquez
Guest
Posts: n/a
 
      07-23-2009
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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-23-2009
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Borlang JBuilder gridbag layout madness nukleus Java 2 01-31-2007 06:11 AM
Choosing Layout: Css-Layout or Table-Layout hpourfard@gmail.com ASP .Net 1 06-19-2006 10:06 AM
DataList inside a Grid Layout Panel (<DIV>) item layout problem Rick Spiewak ASP .Net 3 08-26-2003 04:22 AM
Re: GridBag Expansion OS X - PLEASE HELP!!! Loki Java 1 06-30-2003 02:10 AM
Re: GridBag Expansion OS X - PLEASE HELP!!! Loki Java 1 06-29-2003 04:42 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57