Solved

Components in wrong places for GridBagLayout?

Posted on 2003-11-09
5
206 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How Complex Is This Java Course ? 9 63
countPairs challenge 7 58
pairs challenge 5 45
Strange loading of website behaviour 3 23
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…
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…
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 …

746 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

14 Experts available now in Live!

Get 1:1 Help Now