Article View: borland.public.jbuilder.java.swing
Article #60GridBagLayout
From: Hiroshi
Date: Sun, 04 Dec 2005 01:57
Date: Sun, 04 Dec 2005 01:57
38 lines
2395 bytes
2395 bytes
Hi everyone, I am planning to post a dialog of 10 JPanels
using the layout, but I cannot make them aligned as I want.
Below is the code to illustrate the problem - some cells get
2 cols while asking 1, and other gets 1 cell while asking 2.
JPanel pnls[]={
new JPanel(),new JPanel(),new JPanel(),new JPanel(),
new JPanel(),new JPanel(),new JPanel(),new JPanel(),
new JPanel(),new JPanel()};
JLabel lbls[]={//digits ~ gridx,gridy,gridwidth,and gridheight
new JLabel("0011"),new JLabel("1021"),new JLabel("3011"),
new JLabel("0111"),new JLabel("1113"),new JLabel("2121"),
new JLabel("0211"),new JLabel("2222"),new JLabel("0311"),
new JLabel("0441")};
for (i=0;i<10;i++) pnls[i].add(lbls[i]);
int gx[]={0,1,3,0,1,2,0,2,0,0}, //gridx for i-th cell
gy[]={0,0,0,1,1,1,2,2,3,4}, //gridy "
gw[]={1,2,1,1,1,2,1,2,1,4}, //gridwidth "
gh[]={1,1,1,1,3,1,1,2,1,1}; //gridheight "
gbc.fill=GridBagConstraints.BOTH;
for (i=0;i<10;i++) {
pnls[i].add(lbls[i]); //(to make panels identifyable)
gbc.gridx=gx[i]; gbc.gridy=gy[i]; //Set the position
gbc.gridwidth=gw[i]; gbc.gridheight=gh[i]; //and the size
gbl.setConstraints(pnls[i],gbc); //constraints for all cells.
pnls[i].setBorder(BorderFactory.createEtchedBorder());
dlgCp.add(pnls[i]);
}
super.setTitle("GridBagLayout Test");
setBounds(10,10,200,200);
show();
As you can see, cell "1113" (~gridx=1;gridy=1;gridwidth=1;
gridheight=3) gets 2 cols while asking 1, and cell "2121"
and "2222" get 1 col while asking 2...
Any idea? Thanks.
Technical Details
Message-ID:
<43925b0a$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeed.hal-mli.net!feeder1.hal-mli.net!news-out.newsfeeds.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 111d45,ec28a2fb91b52693,start X-Google-Attributes: gid111d45,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Hiroshi" <hiroshinakata@hotmail.com> Sender: "Hiroshi" <hiroshinakata@hotmail.com> Reply-To: "Hiroshi" <hiroshinakata@hotmail.com> Subject: GridBagLayout Newsgroups: borland.public.jbuilder.java.swing X-User-Info: 69.192.157.16 NNTP-Posting-Host: 207.105.83.66 Message-ID: <43925b0a$1@newsgroups.borland.com> Date: 3 Dec 2005 18:57:14 -0700 X-Trace: newsgroups.borland.com 1133665034 207.105.83.66 (3 Dec 2005 18:57:14 -0700) Lines: 39 Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeed.hal-mli.net!feeder1.hal-mli.net!news-out.newsfeeds.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail