Solved

Using GridBagLayout

Posted on 1997-06-02
6
185 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:kpetti
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

772 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

9 Experts available now in Live!

Get 1:1 Help Now