Solved

Using GridBagLayout

Posted on 1997-06-02
6
229 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:kpetti
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
garik earned 50 total points
ID: 1221147
I'd suggest to use a simpler GridLayout (unless you have some specific requirements not stated in your question):

public void init() {
    setLayout(new GridLayout(6,2));
    add(new Label("Label1")); add(new TextField("Text1");
    ...
    add(new Label("Label5")); add(new TextField("Text5");
    add(new Button("Left")); add(new Button("Right"));
    validate();
}

Check how it looks, play with alignments, and if it doesn't give you desired layout, let me know and we'll convert it to GridBagLayout.
0
 

Author Comment

by:kpetti
ID: 1221148
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
0
 
LVL 3

Expert Comment

by:garik
ID: 1221149
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);
}

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:kpetti
ID: 1221150
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
      //}}
}


0
 
LVL 3

Expert Comment

by:garik
ID: 1221151
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.

0
 
LVL 3

Expert Comment

by:garik
ID: 1221152
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().
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question