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)getCon tentPane() ;
panelobject.setLayout(gbob ject);
labelcustname = new JLabel("Customer Name: ");
labelcustage = new JLabel("Age");
textcustname = new JTextField(10);
textcustage = new JTextField(2);
buttonaccept=new JButton("Accept");
gbc.anchor=GridBagConstrai nts.NORTHW EST;
gbc.gridx=1;
gbc.gridy=5;
gbobject.setConstraints(la belcustnam e,gbc);
panelobject.add(labelcustn ame);
gbc.anchor=GridBagConstrai nts.NORTHW EST;
gbc.gridx=4;
gbc.gridy=5;
gbobject.setConstraints(te xtcustname ,gbc);
panelobject.add(textcustna me);
gbc.anchor=GridBagConstrai nts.NORTHW EST;
gbc.gridx=1;
gbc.gridy=20;
gbobject.setConstraints(la belcustage ,gbc);
panelobject.add(labelcusta ge);
gbc.anchor=GridBagConstrai nts.NORTHW EST;
gbc.gridx=4;
gbc.gridy=20;
gbobject.setConstraints(te xtcustage, gbc);
panelobject.add(textcustag e);
gbc.anchor=GridBagConstrai nts.NORTHW EST;
gbc.gridx=8;
gbc.gridy=5;
gbobject.setConstraints(bu ttonaccept ,gbc);
panelobject.add(buttonacce pt);
ValidateAction validatebutton=new ValidateAction();
buttonaccept.addActionList ener(valid atebutton) ;
}
class ValidateAction implements ActionListener
{
public void actionPerformed(ActionEven t evt)
{
Object obj=evt.getSource();
if(obj==buttonaccept)
{
String customername=textcustname. getText();
if(customername.length()== 0)
{
getAppletContext().showSta tus("Custo mer Name cannot be empty");
return;
}
int customerage=Integer.parseI nt(textcus tage.getTe xt());
if(customerage<=0 || customerage>=100)
{
getAppletContext().showSta tus("Inval id value for age");
return;
}
}
}
}
}
So anything to be changed? Thanks.
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)getCon
panelobject.setLayout(gbob
labelcustname = new JLabel("Customer Name: ");
labelcustage = new JLabel("Age");
textcustname = new JTextField(10);
textcustage = new JTextField(2);
buttonaccept=new JButton("Accept");
gbc.anchor=GridBagConstrai
gbc.gridx=1;
gbc.gridy=5;
gbobject.setConstraints(la
panelobject.add(labelcustn
gbc.anchor=GridBagConstrai
gbc.gridx=4;
gbc.gridy=5;
gbobject.setConstraints(te
panelobject.add(textcustna
gbc.anchor=GridBagConstrai
gbc.gridx=1;
gbc.gridy=20;
gbobject.setConstraints(la
panelobject.add(labelcusta
gbc.anchor=GridBagConstrai
gbc.gridx=4;
gbc.gridy=20;
gbobject.setConstraints(te
panelobject.add(textcustag
gbc.anchor=GridBagConstrai
gbc.gridx=8;
gbc.gridy=5;
gbobject.setConstraints(bu
panelobject.add(buttonacce
ValidateAction validatebutton=new ValidateAction();
buttonaccept.addActionList
}
class ValidateAction implements ActionListener
{
public void actionPerformed(ActionEven
{
Object obj=evt.getSource();
if(obj==buttonaccept)
{
String customername=textcustname.
if(customername.length()==
{
getAppletContext().showSta
return;
}
int customerage=Integer.parseI
if(customerage<=0 || customerage>=100)
{
getAppletContext().showSta
return;
}
}
}
}
}
So anything to be changed? 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? ;)
Can you please make it a bit clearer? ;)
ASKER
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.
Awaiting for reply.Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry for the delay as I was focused on some other topic and totally forget about this.Thanks anyway :-)
ASKER