Solved

Components in wrong places for GridBagLayout?

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where to store the queries for modification of table 4 61
MySQL  on Tomcat 8 45
runtime exception 2 27
Problem to Alipay 10 21
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…
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 third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

776 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