Article View: borland.public.jbuilder.java.swing
Article #84Re: GridBagLayout
From: Paul Furbacher [TeamB]
Date: Sat, 07 Jan 2006 03:02
Date: Sat, 07 Jan 2006 03:02
159 lines
6370 bytes
6370 bytes
Hiroshi wrote:
> 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. [...] 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.
[Sorry it has taken so long to answer this, but I was
buried in a project with a deadline around the holidays.)
Yes. If I get what you are trying to do, I think you
are being misled into thinking it doesn't work because
of the way you have constructed the exercise. Try
using a long label for "1021", like "1021 and a lot more".
Or, you could simply add some x-padding to the panel
containing label "1021" and set the weightx of the panel
containing label "0441" to 1.0. If you want the last row
to not grow in width when you resize the frame,
add another column to the end with a single cell
spanning all rows. Set the weightx and weighty
to 1.0. Call it "4015". Finally, so that panel "0441"
doesn't grow in height, set its fill to HORIZONTAL
and set the anchor of all cells to NORTHWEST.
Obviously, in cell "4015", you wouldn't add a border
or put any kind of component in it.
Here's a completely cleaned up version of your original,
may I say, hasty-pasty code.
****
package org.pf.view;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* <p>Sender: "Hiroshi" <hiroshinakata@hotmail.com> <br/>
* Subject: GridBagLayout, 12/3/05 8:57 PM <br/>
* borland.public.jbuilder.java.swing <br/>
* </p>
* <p>Copyright (c) 2006</p>
*
* @author parts Hiroshi, parts Paul Furbacher
* @version 1.0
*/
public class GblOddnessFrame
extends JFrame {
protected GridBagLayout gridBagLayout1 = new GridBagLayout();
protected JPanel jPanel1 = new JPanel();
protected GridBagLayout gridBagLayout2 = new GridBagLayout();
public GblOddnessFrame() {
try {
jbInit();
init();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Reworked a bit so as to be more readable.
*
* Note that I added a 4015 panel/label
* combo, and added weight arrays.
* I've also made the label for "1021" longer. Note the commented
* x-padding for panel in which label "1021" is contained.
*/
private void init() {
JPanel pnls[] = {
new JPanel(), 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 and a lot more"),
new JLabel("3011"),
new JLabel("0111"), new JLabel("1113"), new JLabel("2121"),
new JLabel("0211"), new JLabel("2222"), new JLabel("0311"),
new JLabel("0441"), new JLabel("4015")};
int gx[] = { 0, 1, 3, 0, 1, 2, 0, 2, 0, 0, 4}; //gridx
int gy[] = { 0, 0, 0, 1, 1, 1, 2, 2, 3, 4, 0}; //gridy
int gw[] = { 1, 2, 1, 1, 1, 2, 1, 2, 1, 4, 1}; //gridwidth
int gh[] = { 1, 1, 1, 1, 3, 1, 1, 2, 1, 1, 5}; //gridheight
int gxw[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; // xweight
int gxy[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; // yweight
// Next block is revised to instantiate a new GridBagConstraints
// for each panel.
for (int i = 0; i < pnls.length; i++) {
GridBagConstraints gbc = new GridBagConstraints();
// Option: for the troubled 1021 panel,
// just add some x-padding.
if ( i == 1 ) {
; //gbc.ipadx = 200;
}
pnls[i].add(lbls[i]);
gbc.gridx = gx[i];
gbc.gridy = gy[i];
gbc.gridwidth = gw[i];
gbc.gridheight = gh[i];
gbc.weightx = gxw[i];
gbc.weighty = gxy[i];
gbc.anchor = GridBagConstraints.NORTHWEST;
// Manage the fills in a better way.
gbc.fill = (i==4)
? GridBagConstraints.VERTICAL
: (i==9)
? GridBagConstraints.HORIZONTAL
: GridBagConstraints.BOTH;
pnls[i].setBorder(BorderFactory.createEtchedBorder());
this.getContentPane().add(pnls[i], gbc);
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(gridBagLayout1);
setTitle("GridBagLayout Test");
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
this_windowClosing(e);
}
});
}
public static void main(String[] args) {
GblOddnessFrame frame = new GblOddnessFrame();
frame.pack();
frame.setVisible(true);
}
public void this_windowClosing(WindowEvent e) {
System.exit(0);
}
}
****
--
Paul Furbacher (TeamB)
Save time, search the archives:
http://info.borland.com/newsgroups/ngsearch.html
Is it in Joi Ellis's Faq-O-Matic?
http://www.visi.com/~gyles19/fom-serve/cache/1.html
Finally, please send responses to the newsgroup only.
That means, do not send email directly to me.
Thank you.
Technical Details
Message-ID:
<43bf2ecf$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!gatel-ffm!gatel-ffm!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<43925b0a$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,ec28a2fb91b52693 X-Google-Attributes: gid111d45,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Fri, 06 Jan 2006 22:02:47 -0500 From: "Paul Furbacher [TeamB]" <pfurbacher@mac.com> Organization: Reverse Alchemy, Inc. User-Agent: Mozilla Thunderbird 1.0.6 (Macintosh/20050716) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing Subject: Re: GridBagLayout References: <43925b0a$1@newsgroups.borland.com> In-Reply-To: <43925b0a$1@newsgroups.borland.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 69.142.194.228 Message-ID: <43bf2ecf$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1136602831 69.142.194.228 (6 Jan 2006 19:00:31 -0700) Lines: 159 Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!gatel-ffm!gatel-ffm!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail