Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Components in wrong places for GridBagLayout?

Posted on 2003-11-09
5
Medium Priority
?
235 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 120 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

636 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