This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes question of GridBagLayout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "question of GridBagLayout" Watch "question of GridBagLayout" New topic
Author

question of GridBagLayout

Arun V.K.
Greenhorn

Joined: Jan 23, 2006
Posts: 5
hi

iam really confused with GridBagLayout. after much struggle i managed to come up with this code:-

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;

public class mm extends WindowAdapter implements ActionListener
{
JFrame f;
JPanel p;
JLabel Cname,ItemCode,ItemName,Price,Qty,TotVal,Status;
JButton cal,save;
JComboBox ic;
JTextField in,prc,qty,tv;
GridBagLayout g;
GridBagConstraints gbc;

public mm()
{
f=new JFrame("@ABC Corp Ltd..");
p=new JPanel();
f.getContentPane().add(p);
g=new GridBagLayout();
gbc =new GridBagConstraints();
p.setLayout(g);

Cname =new JLabel("ABC LTD",JLabel.CENTER);
Cname.setFont(new Font("ARIAL",Font.BOLD|Font.ITALIC,20));
gbc.fill=GridBagConstraints.BOTH;
gbc.anchor=GridBagConstraints.CENTER;
gbc.weightx=1.0;
gbc.gridwidth=GridBagConstraints.REMAINDER;
g.setConstraints(Cname,gbc);
p.add(Cname);

ItemCode=new JLabel("ItemCode");
gbc.fill=GridBagConstraints.NONE;
gbc.gridwidth=GridBagConstraints.RELATIVE;
g.setConstraints(ItemCode,gbc);
p.add(ItemCode);

String []str={"Please Select","I001","I002","I003"};
ic=new JComboBox(str);
gbc.gridwidth=GridBagConstraints.REMAINDER;
g.setConstraints(ic,gbc);
p.add(ic);

ItemName=new JLabel("ItemName");
gbc.gridwidth=GridBagConstraints.RELATIVE;
g.setConstraints(ItemName,gbc);
p.add(ItemName);

in=new JTextField(10);
in.setEnabled(false);
gbc.gridwidth=GridBagConstraints.REMAINDER;
g.setConstraints(in,gbc);
p.add(in);

Price=new JLabel("Price");
gbc.gridwidth=GridBagConstraints.RELATIVE;
g.setConstraints(Price,gbc);
p.add(Price);

prc=new JTextField(6);
prc.setEnabled(false);
gbc.gridwidth=GridBagConstraints.REMAINDER;
g.setConstraints(prc,gbc);
p.add(prc);

Qty=new JLabel("Quantity");
gbc.gridwidth=GridBagConstraints.RELATIVE ;
g.setConstraints(Qty,gbc);
p.add(Qty);

qty=new JTextField(2);
gbc.gridwidth=GridBagConstraints.REMAINDER;
g.setConstraints(qty,gbc);
p.add(qty);

cal=new JButton("Calculate");
gbc.weightx=0.0;
gbc.gridwidth=GridBagConstraints.REMAINDER;
g.setConstraints(cal,gbc);
p.add(cal);

TotVal=new JLabel("TotalVaule");
gbc.weightx=1.0;
gbc.gridwidth=GridBagConstraints.RELATIVE;
g.setConstraints(TotVal,gbc);
p.add(TotVal);

tv=new JTextField(10);
gbc.gridwidth=GridBagConstraints.REMAINDER;
g.setConstraints(tv,gbc);
p.add(tv);

save=new JButton("Save");
gbc.weightx=0.0;
gbc.gridwidth=GridBagConstraints.REMAINDER;
g.setConstraints(save,gbc);
p.add(save);

Status = new JLabel("Status: ",JLabel.LEFT);
gbc.weightx=1.0;
gbc.anchor=GridBagConstraints.WEST;
gbc.gridwidth=GridBagConstraints.REMAINDER;
g.setConstraints(Status,gbc);
p.add(Status);

f.setSize(350,300);
f.setVisible(true);

ic.addActionListener(this);
cal.addActionListener(this);
save.addActionListener(this);
f.addWindowListener(this);
}

public static void main(String [] arg)
{
mm m=new mm();
}

public void actionPerformed(ActionEvent ae)
{
if (ae.getSource()==ic)
{
String s= new String();
s=(String)ic.getSelectedItem();
Status.setText("Status: "+s);
if(s=="I001")
{
in.setText("KeyBoard");
prc.setText("20");
}
else if(s=="I002")
{
in.setText("Mouse");
prc.setText("10");
}
else if(s=="I003")
{
in.setText("Monitor");
prc.setText("30");
}
}
else if(ae.getSource()==cal)
{
int q = Integer.parseInt(qty.getText());
int price=Integer.parseInt(prc.getText());
if(q>0)
{
int val=price*q;
tv.setText(Integer.toString(val));
Status.setText("Status: Total Price Calculated");
}
if(q<=0)
Status.setText("Status: Quantity cannot be < = 0");
}
else if((ae.getSource()==save))
{
Status.setText("Status: Data Saved");
JOptionPane.showMessageDialog(f,"ItemCode :"+ic.getSelectedItem()+"\nItemName :" +in.getText()+
"\nPrice :"+prc.getText()+"\nQuantity :\t"+qty.getText(),"Saved Details",JOptionPane.PLAIN_MESSAGE);

}
}
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}

but the output is not as i want it to be. i want to know how to align the text fields, i mean how do i make it begin at the same x coordinate. i hope iam clear.

any suggestions and tips are welcome. thanks.


Arun V.K.
Brian Nahas
Greenhorn

Joined: Dec 30, 2004
Posts: 6
GridBagLayout is painful to learn and to use. Sorry to hear you're having difficult with it. I've modified you're code below to display the form a little better. I'm having difficult figuring out what you're trying to accomplish, but this looks better and the fields are better aligned.

I'd suggest looking into using another layout manager. For example, using ZoneLayout (http://www.zonelayout.com), your layout code would look like:

ZoneLayout layout = ZoneLayoutFactory.newZoneLayout();
layout.addRow("a.......a");
layout.addRow("b>b2c-~.c", "inputRow");
layout.addRow("6........", "inputRow");
layout.addRow("d.......d", "buttonRow");
layout.addRow("6........", "buttonRow");
layout.addRow("e<......e");


p=new JPanel(layout);
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(p, BorderLayout.CENTER);
Cname =new JLabel("ABC LTD",JLabel.CENTER);
Cname.setFont(new Font("ARIAL",Font.BOLD|Font.ITALIC,20));
ItemCode=new JLabel("ItemCode");
String []str={"Please Select","I001","I002","I003"};
ic=new JComboBox(str);
ItemName=new JLabel("ItemName");
in=new JTextField(10);
in.setEnabled(false);
Price=new JLabel("Price");
prc=new JTextField(6);
Qty=new JLabel("Quantity");
qty=new JTextField(2);
cal=new JButton("Calculate");
TotVal=new JLabel("TotalVaule");
tv=new JTextField(10);
save=new JButton("Save");
Status = new JLabel("Status: ",JLabel.LEFT);

p.add(Cname, "a");
layout.insertTemplate("inputRow");
p.add(ItemCode, "b");
p.add(ic, "c");
layout.insertTemplate("inputRow");
p.add(ItemName, "b");
p.add(in, "c");
layout.insertTemplate("inputRow");
p.add(Price, "b");
p.add(prc, "c");
layout.insertTemplate("inputRow");
p.add(Qty, "b");
p.add(qty, "c");
layout.insertTemplate("buttonRow");
p.add(cal, "d");
layout.insertTemplate("inputRow");
p.add(TotVal, "b");
p.add(tv, "c");
layout.insertTemplate("buttonRow");
p.add(save, "d");
p.add(Status, "e");

Much easier to read and use. You might also want to look into JGoodies' FormLayout or TableLayout.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;

public class mm extends WindowAdapter implements ActionListener
{
JFrame f;
JPanel p;
JLabel Cname,ItemCode,ItemName,Price,Qty,TotVal,Status;
JButton cal,save;
JComboBox ic;
JTextField in,prc,qty,tv;
GridBagLayout g;
GridBagConstraints gbc;

public mm()
{
f=new JFrame("@ABC Corp Ltd..");
p=new JPanel();
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(p, BorderLayout.CENTER);
g=new GridBagLayout();
gbc =new GridBagConstraints();
p.setLayout(g);

Cname =new JLabel("ABC LTD",JLabel.CENTER);
Cname.setFont(new Font("ARIAL",Font.BOLD|Font.ITALIC,20));
gbc.gridwidth=GridBagConstraints.REMAINDER;
p.add(Cname, gbc);

ItemCode=new JLabel("ItemCode");
gbc.gridwidth=GridBagConstraints.RELATIVE;
gbc.anchor = GridBagConstraints.EAST;
gbc.fill=GridBagConstraints.NONE;
gbc.weightx = 0.0;
p.add(ItemCode, gbc);

String []str={"Please Select","I001","I002","I003"};
ic=new JComboBox(str);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
p.add(ic, gbc);

ItemName=new JLabel("ItemName");
gbc.gridwidth=GridBagConstraints.RELATIVE;
gbc.anchor = GridBagConstraints.EAST;
gbc.fill=GridBagConstraints.NONE;
gbc.weightx = 0.0;
p.add(ItemName, gbc);

in=new JTextField(10);
in.setEnabled(false);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
p.add(in, gbc);

Price=new JLabel("Price");
gbc.gridwidth=GridBagConstraints.RELATIVE;
gbc.anchor = GridBagConstraints.EAST;
gbc.fill=GridBagConstraints.NONE;
gbc.weightx = 0.0;
p.add(Price, gbc);

prc=new JTextField(6);
prc.setEnabled(false);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
p.add(prc, gbc);

Qty=new JLabel("Quantity");
gbc.gridwidth=GridBagConstraints.RELATIVE ;
gbc.anchor = GridBagConstraints.EAST;
gbc.fill=GridBagConstraints.NONE;
gbc.weightx = 0.0;
p.add(Qty, gbc);

qty=new JTextField(2);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
p.add(qty, gbc);

cal=new JButton("Calculate");
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill=GridBagConstraints.NONE;
gbc.weightx = 0.0;
p.add(cal, gbc);

TotVal=new JLabel("TotalVaule");
gbc.gridwidth=GridBagConstraints.RELATIVE;
gbc.anchor = GridBagConstraints.EAST;
gbc.fill=GridBagConstraints.NONE;
gbc.weightx = 0.0;
p.add(TotVal, gbc);

tv=new JTextField(10);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
p.add(tv, gbc);

save=new JButton("Save");
gbc.weightx=0.0;
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill=GridBagConstraints.NONE;
gbc.weightx = 0.0;
p.add(save, gbc);

Status = new JLabel("Status: ",JLabel.LEFT);
gbc.gridwidth=GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.WEST;
p.add(Status, gbc);

f.setSize(350,300);
f.setVisible(true);

ic.addActionListener(this);
cal.addActionListener(this);
save.addActionListener(this);
f.addWindowListener(this);
}

public static void main(String [] arg)
{
mm m=new mm();
}

public void actionPerformed(ActionEvent ae)
{
if (ae.getSource()==ic)
{
String s= new String();
s=(String)ic.getSelectedItem();
Status.setText("Status: "+s);
if(s=="I001")
{
in.setText("KeyBoard");
prc.setText("20");
}
else if(s=="I002")
{
in.setText("Mouse");
prc.setText("10");
}
else if(s=="I003")
{
in.setText("Monitor");
prc.setText("30");
}
}
else if(ae.getSource()==cal)
{
int q = Integer.parseInt(qty.getText());
int price=Integer.parseInt(prc.getText());
if(q>0)
{
int val=price*q;
tv.setText(Integer.toString(val));
Status.setText("Status: Total Price Calculated");
}
if(q<=0)
Status.setText("Status: Quantity cannot be < = 0");
}
else if((ae.getSource()==save))
{
Status.setText("Status: Data Saved");
JOptionPane.showMessageDialog(f,"ItemCode :"+ic.getSelectedItem()+"\nItemName :" +in.getText()+
"\nPrice :"+prc.getText()+"\nQuantity :\t"+qty.getText(),"Saved Details",JOptionPane.PLAIN_MESSAGE);

}
}
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
David Irwin
Ranch Hand

Joined: Mar 25, 2004
Posts: 82
I might suggest that you take a look at the JGoodies forms layout library. Karsten has included a very helpful whitepaper that explains how to use the forms layout manager. I've found that it's very easy to use.

Dave
Jerret Halter
Ranch Hand

Joined: Feb 03, 2006
Posts: 62
Even though this does not answer your question about GridBagLayout your code is break sytle crime, which would piss off any employer or instructor/professor or whoever you are turing it into so read this guide Bleeding at the Keyboard


Jerret Halter<br /> <br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.<hr></blockquote>
 
Don't get me started about those stupid light bulbs.
 
subject: question of GridBagLayout
 
Similar Threads
Creating and opening a window if the button is clicked in an applet
JTextArea with varied lengths
Best Layout for Simple Forms?
Problem with GridBagLayout
Help in gridbaglayout