Solved

Components in wrong places for GridBagLayout?

Posted on 2003-11-09
5
209 Views
Last Modified: 2010-03-31
The components are getting displayed in positions as opposed to my setting of gridx and gridy values.The code is as follows.....

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Customer extends JApplet
{
JPanel panelobject;
JLabel labelcustname;
JLabel labelcustage;
JTextField textcustname;
JTextField textcustage;
JButton buttonaccept;
GridBagLayout gbobject;
GridBagConstraints gbc;
public void init()
{
gbobject=new GridBagLayout();
gbc=new GridBagConstraints();
panelobject=(JPanel)getContentPane();
panelobject.setLayout(gbobject);
labelcustname = new JLabel("Customer Name: ");
labelcustage = new JLabel("Age");
textcustname = new JTextField(10);
textcustage = new JTextField(2);
buttonaccept=new JButton("Accept");
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=1;
gbc.gridy=5;
gbobject.setConstraints(labelcustname,gbc);
panelobject.add(labelcustname);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=4;
gbc.gridy=5;
gbobject.setConstraints(textcustname,gbc);
panelobject.add(textcustname);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=1;
gbc.gridy=20;
gbobject.setConstraints(labelcustage,gbc);
panelobject.add(labelcustage);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=4;
gbc.gridy=20;
gbobject.setConstraints(textcustage,gbc);
panelobject.add(textcustage);
gbc.anchor=GridBagConstraints.NORTHWEST;
gbc.gridx=8;
gbc.gridy=5;
gbobject.setConstraints(buttonaccept,gbc);
panelobject.add(buttonaccept);
ValidateAction validatebutton=new ValidateAction();
buttonaccept.addActionListener(validatebutton);
}

class ValidateAction implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
Object obj=evt.getSource();
if(obj==buttonaccept)
{
String customername=textcustname.getText();
if(customername.length()==0)
{
getAppletContext().showStatus("Customer Name cannot be empty");
return;
}
int customerage=Integer.parseInt(textcustage.getText());
if(customerage<=0 || customerage>=100)
{
getAppletContext().showStatus("Invalid value for age");
return;
}
}
}
}

}

So anything to be changed? Thanks.
0
Comment
Question by:gopikrish
  • 3
5 Comments
 

Author Comment

by:gopikrish
ID: 9710403
Well no one knows? Please any comment is welcome.Thanks.
0
 
LVL 3

Expert Comment

by:InNoCenT_Ch1ld
ID: 9711907
>>The components are getting displayed in positions as opposed to my setting of gridx and gridy values.

Can you please make it a bit clearer? ;)
0
 

Author Comment

by:gopikrish
ID: 9714016
Well.As you can see from my code gridx and gridy values I wanted labelcustname on 1st row and 5th column and textcustname to be below this.And similarly for labelcustage and textcustage.But on executing the applet its coming side by side.If you can execute yourself the above code you might understand more :)
Awaiting for reply.Thanks.
0
 
LVL 1

Accepted Solution

by:
BuddhaBuddy earned 30 total points
ID: 9719159
You got exactly what you asked for. Like everything else in Java GUI development, x increasing is across from left to right, or columns, and y is down from top to bottom, or rows. When you say "gbc.gridx=1; gbc.gridy=5;", you are telling it to place the component in the second column, sixth row. When you say "gbc.gridx=4; gbc.gridy=5;", you are telling it to place the component in the fifth column, sixth row.

What is the point of skipping all the rows and columns, anyway? GridBag just shrinks them to nothing anyway.
PLEASE use better variable names. An occasional capital letter really does wonders for readability.
Don't forget to do data validation while validating...  :-)  A NumberFormatException can ruin your day.
Lastly, don't forget to clear out the status when successful, or once it works, it looks like it still messed up.

Here is something a little easier to read and understand, which uses GridBag's natural tendencies.

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Customer extends JApplet
{
    JPanel pContent;
    JTextField tfCustName;
    JTextField tfCustAge;
    JButton btAccept;
    public void init()
    {
        GridBagConstraints gbc = new GridBagConstraints();
        pContent = (JPanel) getContentPane();
        pContent.setLayout(new GridBagLayout());
        tfCustName = new JTextField(10);
        tfCustAge = new JTextField(2);
        btAccept = new JButton("Accept");

        gbc.anchor = GridBagConstraints.NORTHWEST;
        pContent.add(new JLabel("Customer Name: "), gbc);
        pContent.add(new JLabel("Age:"), gbc);
        gbc.gridy = 1;
        pContent.add(tfCustName, gbc);
        pContent.add(tfCustAge, gbc);
        gbc.gridy = 2;
        pContent.add(btAccept, gbc);

        ValidateAction validatebutton = new ValidateAction();
        btAccept.addActionListener(validatebutton);
    }

    class ValidateAction implements ActionListener
    {
        public void actionPerformed(ActionEvent evt)
        {
            Object obj = evt.getSource();
            if (obj == btAccept)
            {
                String customername = tfCustName.getText();
                if (customername.length() == 0)
                {
                    getAppletContext().showStatus(
                        "Customer Name cannot be empty");
                    return;
                }
               
                try {
                    int customerage = Integer.parseInt(tfCustAge.getText());
                    if (customerage <= 0 || customerage >= 100)
                    {
                        getAppletContext().showStatus("Invalid value for age");
                        return;
                    }
                } catch (NumberFormatException nfe)
                {
                    getAppletContext().showStatus("Not a valid number");
                    return;
                }
                getAppletContext().showStatus("");
                // Do something meaningful with data
            }
        }
    }
}
0
 

Author Comment

by:gopikrish
ID: 9802506
Sorry for the delay as I was focused on some other topic and totally forget about this.Thanks anyway :-)
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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

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

18 Experts available now in Live!

Get 1:1 Help Now