Problem aligning components on a panel using GridBagLayout and GBC

I am developing a GUI where I will be having several rows of label/textfield pairs ( each row will have at the most 3 pairs ). I just started with 2 rows. The first row has 2 label/textfield pairs and the second row has 3 label/textfield pairs. I am running of out ideas on how to make them aligned properly. Any help is appreciated.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

publicclass TryWithGBCextends JFrame{

private Object SCROLLBARS_VERTICAL_ONLY;

private Font font;

private JFrame dataFrame;

private Container dataPane;

private GridBagLayout GBL;

private GridBagConstraints GBC;

private JPanel allCompPanel;

private GridBagConstraints allCompGBC;

public TryWithGBC(){

dataFrame =new JFrame();

dataFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Font font =new Font("Courier", Font.BOLD, 11);

GBL =new GridBagLayout();

GBC =new GridBagConstraints();

dataPane = dataFrame.getContentPane();

dataPane.setLayout( GBL );

GBC.fill = GridBagConstraints.NONE;

allCompGBC =new GridBagConstraints();

allCompPanel =new JPanel(new GridBagLayout() );

dataPane.add( allCompPanel, GBC );

createFirstRow( allCompPanel, allCompGBC );

createSecondRow( allCompPanel, allCompGBC );

dataFrame.setSize( 700, 300 );

dataFrame.setVisible(true );

}

privatevoid createSecondRow( JPanel allCompPanel, GridBagConstraints allCompGBC )

{

JLabel l21 =new JLabel("CARD ID : ", JLabel.RIGHT);

JTextField shortDescTxt =new JTextField(20);

shortDescTxt.setMinimumSize(shortDescTxt.getPreferredSize());

JLabel l22 =new JLabel("SERVICE ID : ", JLabel.RIGHT );

JTextField t5 =new JTextField(15);

t5.setMinimumSize(t5.getPreferredSize());

JLabel l23 =new JLabel("SER REF NUM : ", JLabel.RIGHT );

JTextField t6 =new JTextField(9);

t6.setMinimumSize(t6.getPreferredSize());

l21.setFont( font );

addComponent( allCompPanel, l21, allCompGBC, 0, 1, 1, 1 );

addComponent( allCompPanel, shortDescTxt, allCompGBC, 1, 1, 1, 1 );

l22.setFont( font );

addComponent( allCompPanel, l22, allCompGBC, 2, 1, 1, 1 );

addComponent( allCompPanel, t5, allCompGBC, 3, 1, 1, 1 );

l23.setFont( font );

addComponent( allCompPanel, l23, allCompGBC, 4, 1, 1, 1 );

addComponent( allCompPanel, t6, allCompGBC, 5, 1, 1, 1 );

return;

}

privatevoid createFirstRow( JPanel allCompPanel, GridBagConstraints allCompGBC ){

JLabel l11 =new JLabel("ID : ",JLabel.RIGHT);

JTextField procNameTxt =new JTextField(15);

procNameTxt.setMinimumSize(procNameTxt.getPreferredSize());

JLabel l12 =new JLabel("Cntrl Num : ",JLabel.RIGHT);

JTextField idNumTxt =new JTextField(40);

idNumTxt.setMinimumSize(idNumTxt.getPreferredSize());

l11.setFont( font );

addComponent( allCompPanel, l11, allCompGBC, 0, 0, 1, 1 );

addComponent( allCompPanel, procNameTxt, allCompGBC, 1, 0, 1, 1 );

l12.setFont( font );

addComponent( allCompPanel, l12, allCompGBC, 2, 0, 1, 1 );

addComponent( allCompPanel, idNumTxt, allCompGBC, 3, 0, 1, 1 );

return;

}

privatevoid addComponent( JPanel panelToHold, Component compToAdd, GridBagConstraints constraints,int x,int y,int w,int h ){

constraints.gridx = x;

constraints.gridy = y;

constraints.gridwidth = w;

constraints.gridheight = h;

panelToHold.add( compToAdd, constraints );

}

publicstaticvoid main (String args[]){

TryWithGBC f =new TryWithGBC();

//f.setVisible(true);

f.pack();

f.addWindowListener (new WindowAdapter(){

publicvoid windowClosing (WindowEvent e){

System.exit(0);

}

});

}

}

[6286 byte] By [scshekhara] at [2007-11-27 11:51:56]
# 1

> I am running of out ideas on how to make them aligned properly.

Well, I have no idea what "aligned properly" means to you. It is alligned properly according to the rules of the layout manager.

camickra at 2007-7-29 18:41:20 > top of Java-index,Desktop,Core GUI APIs...
# 2

I want the label/textfield to be paired as shown below.

firstRowLabel1 : TextField1 firstRowLabel2 : TextField2

secRowLabel3 : TextField3secRowLabel4 : TextField4 secRowLabel5 : TextField5

Like this I have to generate few rows of label/text field pairs.

scshekhara at 2007-7-29 18:41:20 > top of Java-index,Desktop,Core GUI APIs...
# 3

You need to play with the "anchor" property of the constraints. For example try your code with the following to see the difference:

allCompGBC = new GridBagConstraints();

allCompGBC.anchor = GBC.EAST; // added

Your labels would be anchored to the EAST and the text fields to the WEST.

camickra at 2007-7-29 18:41:20 > top of Java-index,Desktop,Core GUI APIs...