Link to home
Start Free TrialLog in
Avatar of gopikrish
gopikrish

asked on

Components in wrong places for GridBagLayout?

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.
Avatar of gopikrish
gopikrish

ASKER

Well no one knows? Please any comment is welcome.Thanks.
>>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? ;)
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.
ASKER CERTIFIED SOLUTION
Avatar of BuddhaBuddy
BuddhaBuddy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry for the delay as I was focused on some other topic and totally forget about this.Thanks anyway :-)