I am a student and have to submit a Mail Program. I have been asked to do the program using GridBagLayout. The program is working fine but for the Layout. I just am not able to manage the layout problems I am facing in this program. Could I get some help to set this right. I don't want to use any other layout. But I just don't believe how weired Layout Problems could get.

I know where the problem lies. Its with the weights Assigned to the components. I just can't am not able to solve this problem. I am facing this alignment issue when I try to resize my Window.

//Set tooltip text

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.UIManager;

class MailClient extends JDialog{

	private JPanel mailPanel = null;
	private JTextArea jtaMessage = null;

	private JButton jbtSend = null;
	private JButton jbtCancel = null;
	private JButton jbtAttachment = null;

	private JLabel jlbTo = null;
	private JLabel jlbCc = null;
	private JLabel jlbBcc = null;
	private JLabel jlbSubject = null;
	private JList jlstAttachedFiles = null;
	private JScrollPane jlstAttachedFilesPane = null;
	private JTextField jtfTo = null;
	private JTextField jtfCc = null;
	private JTextField jtfBcc = null;
	private JTextField jtfSubject = null;
	private JFileChooser jfcOpenChooser = null;
	private JMenuBar jmenubar = null;
	private JMenu jmenuFile = null;
	private JMenu jmenuHelp = null;
	private JMenu jmenuEdit = null;
	private JMenuItem jmenuitemNew = null;
	private JMenuItem jmenuitemCut = null;
	private JMenuItem jmenuitemCopy = null;
	private JMenuItem jmenuitemPaste = null;
	private JMenuItem jmenuitemAttachments = null;
	private JMenuItem jmenuitemHelp = null;
	private JMenuItem jmenuitemExit = null;
	private JButton jbtCut = null;
	private JButton jbtCopy = null;
	private JButton jbtPaste = null;
	private JButton jbtHelp = null;
	private JButton jbtAttachmentsTb = null;
	private JToolBar jtoolbar = null;
	private ImageIcon cutIcon = null;
	private ImageIcon copyIcon = null;
	private ImageIcon helpIcon = null;
	private ImageIcon pasteIcon = null;
	private ImageIcon attachmentsIcon = null;
	private static final String windows  = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";

	public MailClient(Frame frame, String title, boolean modal) {
		super(frame, title, modal);
	public MailClient() {
		this((Frame)null, "", false);
	private void init(){
		try {
		} catch (Exception e1) {
		this.setSize(400, 400);
		//Toolbar in seperate panel and the other components in the seperate panel
		JPanel toolbarPanel = new JPanel();
	 	toolbarPanel.setLayout(new GridBagLayout());
		GridBagConstraints toolbarCons = new GridBagConstraints();
		toolbarCons = new GridBagConstraints(0,0,1,1,0.1,0,
				GridBagConstraints.WEST,GridBagConstraints.NONE, new Insets(5,5,0,5),0,0);  
		GridBagConstraints mailPanelCons = new GridBagConstraints();
		mailPanelCons = new GridBagConstraints(0,1,1,1,0.1,0,
				GridBagConstraints.WEST,GridBagConstraints.NONE, new Insets(5,5,0,5),0,0);  
		jlbTo = new JLabel("To");
		jlbCc = new JLabel("CC");
		jlbBcc = new JLabel("BCC");
		jlbSubject = new JLabel("Subject");
		jlstAttachedFiles = new JList();
		jlstAttachedFilesPane = new JScrollPane(jlstAttachedFiles);
		jlstAttachedFilesPane.setPreferredSize(new Dimension(150, 75));
		jbtAttachment = new JButton("Attachments");
		jbtSend = new JButton("Send");
		jbtCancel = new JButton("Cancel");
		jtfTo = new JTextField() ;
		jtfCc = new JTextField();
		jtfBcc = new JTextField();
		jtfSubject = new JTextField();
		jtaMessage = new JTextArea();
		jtaMessage.setPreferredSize(new java.awt.Dimension(300, 200));
		jtaMessage.setMinimumSize(new java.awt.Dimension(100, 50));
		JScrollPane mailMessage = new JScrollPane(jtaMessage);
		jfcOpenChooser = new JFileChooser();
		jtoolbar = new JToolBar();
		cutIcon = new ImageIcon("cut.jpg");
		copyIcon = new ImageIcon("copy.jpg");
		helpIcon = new ImageIcon("help.gif");
		pasteIcon = new ImageIcon("paste.jpg");
		attachmentsIcon = new ImageIcon("attachments.jpg");
		jmenubar = new JMenuBar();
		jmenuFile = new JMenu("File");
		jmenuHelp = new JMenu("Help");
		jmenuEdit = new JMenu("Edit");
		jmenuitemNew = new JMenuItem("New Mail");
		jmenuitemCut = new JMenuItem("Cut", cutIcon);
		jmenuitemCopy = new JMenuItem("Copy", copyIcon);
		jmenuitemPaste = new JMenuItem("Paste", pasteIcon);
		jmenuitemAttachments = new JMenuItem("Attachments", attachmentsIcon);
		jmenuitemHelp = new JMenuItem("Help", helpIcon);
		jmenuitemExit = new JMenuItem("Exit");
		jbtCut = new JButton(cutIcon);
		jbtCopy = new JButton(copyIcon);
		jbtPaste = new JButton(pasteIcon);
		jbtHelp = new JButton(helpIcon);
		jbtAttachmentsTb = new JButton(attachmentsIcon);
		mailPanel = new JPanel();
		mailPanel.setLayout(new GridBagLayout());
		GridBagConstraints cons = new GridBagConstraints();
		cons = new GridBagConstraints(0,0,1,1,0.2,0.1,
				GridBagConstraints.WEST,GridBagConstraints.NONE, new Insets(5,5,0,5),0,0);  
		mailPanel.add(jlbTo, cons);
		cons = new GridBagConstraints(1,0,4,1,0.8,0.1,
				GridBagConstraints.WEST,GridBagConstraints.BOTH, new Insets(5,5,0,5),0,0); 
		mailPanel.add(jtfTo, cons);
		cons = new GridBagConstraints(0,1,1,1,0.2,0.1,
				GridBagConstraints.WEST,GridBagConstraints.NONE, new Insets(5,5,0,5),0,0); 
		mailPanel.add(jlbCc, cons);
		cons = new GridBagConstraints(1,1,4,1,0.8,0.1,
				GridBagConstraints.WEST,GridBagConstraints.BOTH, new Insets(5,5,0,5),0,0); 
		mailPanel.add(jtfCc, cons);
		cons = new GridBagConstraints(0,2,1,1,0.2,0.1,
				GridBagConstraints.WEST,GridBagConstraints.NONE, new Insets(5,5,0,5),0,0); 
		mailPanel.add(jlbBcc, cons);
		cons = new GridBagConstraints(1,2,4,1,0.8,0.1,
				GridBagConstraints.WEST,GridBagConstraints.BOTH, new Insets(5,5,0,5),0,0); 
		mailPanel.add(jtfBcc, cons);
		cons = new GridBagConstraints(0,3,1,1,0.2,0.1,
				GridBagConstraints.WEST,GridBagConstraints.NONE, new Insets(5,5,0,5),0,0); 
		mailPanel.add(jlbSubject, cons);
		cons = new GridBagConstraints(1,3,4,1,0.8,0.1,
				GridBagConstraints.WEST,GridBagConstraints.BOTH, new Insets(5,5,0,5),0,0); 
		mailPanel.add(jtfSubject, cons);
		cons = new GridBagConstraints(0,4,1,1,0.2,0.1,
				GridBagConstraints.WEST,GridBagConstraints.NONE, new Insets(5,5,0,5),0,0); 
		mailPanel.add(jbtAttachment, cons);
		cons = new GridBagConstraints(1,4,4,1,0.8,0.1,
				GridBagConstraints.WEST,GridBagConstraints.NONE, new Insets(5,5,0,5),0,0); 
		mailPanel.add(jlstAttachedFilesPane, cons);
		cons = new GridBagConstraints(1,5,4,4,0.8,0.4,
				GridBagConstraints.WEST,GridBagConstraints.BOTH, new Insets(5,5,0,5),0,0); 
		mailPanel.add(mailMessage, cons);
		cons = new GridBagConstraints(0,9,1,1,0.2,0.1,
				GridBagConstraints.WEST,GridBagConstraints.NONE, new Insets(5,5,0,5),0,0); 
		mailPanel.add(jbtSend, cons);
		cons = new GridBagConstraints(1,9,1,1,0.2,0.1,
				GridBagConstraints.WEST,GridBagConstraints.NONE, new Insets(5,5,0,5),0,0); 
		mailPanel.add(jbtCancel, cons);
		jbtSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
		jbtCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
		jbtAttachment.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
		toolbarPanel.add(jtoolbar, toolbarCons);
		this.getContentPane().setLayout(new GridBagLayout());
		this.getContentPane().add(toolbarPanel, toolbarCons);
		this.getContentPane().add(mailPanel, mailPanelCons);

	private void jbtOK_actionPerformed(ActionEvent e) {
		//	Business Logic

	private void jbtCancel_actionPerformed(ActionEvent e) {

	private void jbtAttachment_actionPerformed(ActionEvent e) {
		File[] fileArray = jfcOpenChooser.getSelectedFiles();
		String[] fileNames = new String[fileArray.length];
		for(int i=0; i < fileArray.length; i++){
			File file = fileArray[i];
			fileNames[i] = file.getPath().trim();
//			fileNames[i] = file.getName().trim();

	public class Invoker{
		private static final String windows  = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
		public static void main(String args[]){
			try {
			} catch (Exception e1) {
			MailClient mc = new MailClient(null, "Mail Client", true);
9 Years
Discussion Span
Last Post by Ezzaral

Well, for one thing, don't set all of those constraint properties in the constructor. How are you going to read and make sense of that parameter string as you try to make adjustments?

Set the constraint properties individually. You can use a single GridBagContstraint object and vary the properties only as much as you need to for each placement. So if you are placing a component that only needs to change its gridx property, change only that property before you submit it with add().


Which part of the behavior specifically do you want to change? The fact that things float a bit, that certain components disappear if sized too small, should the message box be the only part that gets larger to fill the area? If you can be more specific it would help.


Which part of the behavior specifically do you want to change? The fact that things float a bit, that certain components disappear if sized too small, should the message box be the only part that gets larger to fill the area? If you can be more specific it would help.


Se basically I used GrdiBag since it was as per the requirement. Secondly what I am looking for is when I resize the dialog I want even the components to resize (become smaller or larger) when the dialog is resized (smaller or larger) with the relative proportional sizes maintained. What is happening now is all the components get centered and the whole layout gets disturbed. When I resize I don't want the tool bar to float down, but stay where it is.


The weightx and weighty properties control how much each grid cell can grow or shrink in the x and y directions as the container is resized. If you want all cells to resize equally, you need to set that property to the same non-zero value, such as 1.0. The fill property will allow you to specify whether the component in that cell fills the horizontal or vertical space alloted, so in conjunction with the weight properties you can control how each component resizes within its grid cell.

You often don't necessarily want everything to grow the same amount. You may want text fields to get longer but not taller when the form is made larger. You may want all text fields to keep their size and have only the message or comment text area to grow to fill the larger space. Getting that behavior is a matter of balancing the weightx and weighty properties to let the components grow in the manner you desire.

Blank labels can be useful as spacers if you need some areas to remain fixed while other areas grow. The blank labels can be assigned a weight of 1.0 and this will cause them to take on all of the new space from a resize.

GridBagLayout is extremely flexible and allows for a very fine degree of control in how your layout behaves, but it does take some time and experimentation to get the behavior you want.

Personally, my recommendation on your form would be to allow the message area to expand to fill the available space in both directions (set fill to BOTH and weightx=1.0 and weighty=1.0). You may want to let the other text fields grow in the x direction but not the y direction, and anchor everything north or northwest.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.