Solved

Using GridBagLayout

Posted on 1997-06-02
6
205 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

776 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