gridbag layout

A

asit

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 ???
 
K

Knute Johnson

asit said:
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);


}

}
 
J

John B. Matthews

"Peter Duniho said:
[...]
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>
 
A

Angel Velazquez

John said:
Peter Duniho said:
[...]
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.
 
R

Roedy Green

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top