Solved

Using GridBagLayout

Posted on 1997-06-02
6
196 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now