Solved

Components in wrong places for GridBagLayout?

Posted on 2003-11-09
5
227 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
[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
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

717 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