Link to home
Start Free TrialLog in
Avatar of kpetti
kpetti

asked on

Using GridBagLayout

I have a form with five textfields and five labels that correspond to these textfields.  How could I use gridbaglayout to lay them out in this manner:

label1 textfield1
label2 textfield2
...
...
label5 textfield5

button1 button2

--------------------------------------

Any code examples do help answer this question would be very helpful
ASKER CERTIFIED SOLUTION
Avatar of garik
garik

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kpetti
kpetti

ASKER

We don't want all of the textfields to be the same size.  I want to have the Email textfield wider than the FirstName textfield.  The reason for this is mostly for aesthetics.  I am developing applets for an online store and I need it to have an appealing appearance to my customers if I am gonna sell anything.

Thanks, Kevin
Try this:

GridBagLayout m_Bag = new GridBagLayout();
GridBagConstraints m_C = new GridBagConstraints();

protected void addRow(String  labelName, int textLength)
{
  m_C.anchor =  GridBagConstraints.EAST;  
  m_C.gridwidth  =  GridBagConstraints.RELATIVE;  
  Label lab = new Label(labelName);
  m_Bag.setConstraints(lab, m_C);
  add(lab);
  m_C.anchor =  GridBagConstraints.WEST;  
  m_C.gridwidth  =  GridBagConstraints.REMAINDER;  
  TextField txt = new TextField(textLength);
  m_Bag.setConstraints(txt, m_C);
  add(txt);
}

public void init()
{
  setFont(new  Font("Helvetica",  Font.PLAIN,  14));
  setBackground(Color.white);
  setLayout(m_Bag);
  m_C.insets = new Insets(3,5,3,5);

  addRow("First Name:", 10);
  addRow("Last Name:", 20);
  addRow("E-mail:", 30);
}

Avatar of kpetti

ASKER

I have also been having trouble lining up the buttons on the bottom.  I want them lined up right below the columns.  Your code worked great!  Thanks for all your help!!!  If you have any more code for these buttons that would be a huge help.  I just started using Java.

Have you ever used the KeyPressManager control in Visual Cafe Pro for tabbing through components?  We tried to use your code with the KeyPressManager but it does not seem to work right.  Here is our code:

Any ideas???

import java.awt.*;
import java.applet.*;
import symantec.itools.awt.*;

public class GridBagLayoutTest extends Applet
{
    KeyPressManagerPanel tabControl = new KeyPressManagerPanel();

    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();

    protected void addRow(String labelName, int textLength)
    {
        constraints.anchor = GridBagConstraints.EAST;
        constraints.gridwidth = GridBagConstraints.RELATIVE;
        Label lab = new Label(labelName);
        gridbag.setConstraints(lab, constraints);
        add(lab);
        constraints.anchor = GridBagConstraints.WEST;
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        TextField txt = new TextField(textLength);
        gridbag.setConstraints(txt, constraints);
        add(txt);
    }

    public void init()
    {
        //{{INIT_CONTROLS
            setLayout(null);
            addNotify();
            resize(430,270);
            //}}
        setFont(new Font("Helvetica", Font.PLAIN, 14));
        setBackground(Color.white);
        setLayout(gridbag);
        constraints.insets = new Insets(3,5,3,5);

        addRow("First Name:", 20);
        addRow("Last Name:", 20);
        addRow("E-mail:", 30);
        addRow("Address 1:", 30);
        addRow("Address 2:", 30);
        addRow("City:", 20);
        addRow("Postal Code:", 10);
        addRow("PO Box:", 10);
        addRow("State:", 20);
        addRow("Province:", 20);
        addRow("Country:", 20);
        addRow("Phone:", 13);
        addRow("Fax:", 13);


    }
          //{{DECLARE_CONTROLS
      //}}
}


To add buttons:

m_C.insets = new Insets(10,5,3,5);
m_C.anchor =  GridBagConstraints.EAST;  
m_C.gridwidth  =  GridBagConstraints.RELATIVE;  
Button but1 = new Button("First");
m_Bag.setConstraints(but1, m_C);
add(but1);
m_C.anchor =  GridBagConstraints.WEST;  
m_C.gridwidth  =  GridBagConstraints.REMAINDER;  
Button but2 = new Button("Second");
m_Bag.setConstraints(but2, m_C);
add(but2);

Unfortunately, I don't work with Visual Cafe.

To use KeyPressManagerPanel, most likely you have to use its add() method - check with the docs. If it's true, then in addRow() and for the buttons use tabControl.add() instead of just add().