🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.swing
5 messages
5 total messages Started by Hiroshi Sun, 04 Dec 2005 01:57
GridBagLayout
#60
Author: Hiroshi
Date: Sun, 04 Dec 2005 01:57
38 lines
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.
Re: GridBagLayout
#63
Author: Hiroshi
Date: Thu, 08 Dec 2005 08:49
66 lines
3586 bytes
I spent quite some time in visiting Sun MicroSystems web site.
One of their documents: http://jave.sun.com/developer/
technicalArticles/GUI/AWTLayoutMgr/index.htm says something
relating to my issue.

"When you specify a component as the last or next-to-last
component in a row, you can give it a gridheight value greater
than one as the component spans multiple rows. However, in this
case, there is no way to make the component wider than one
column when it is the last or next-to-last component in the
row."  Also it says
"Due to a GridBagLayout bug, you can use gridheight to have a
component span multiple rows only if the component is in Column
0 or if gridx and gridy have positive values."

If above statements are true even today, the layout has a
serious limitation/drawback I must say.

I wonder why such a limitation is needed...

My issue must be quite common because my layout is nothing
unusual, thus, lots of people must have experienced...

Please respond if you have any idea about my issue.

"Hiroshi" <hiroshinakata@hotmail.com> 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.
>
>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.
>
Re: GridBagLayout
#75
Author: Infernoz
Date: Thu, 22 Dec 2005 13:13
50 lines
3054 bytes
I suggest you use packer, a GridBagLayout helper, it is much easier to 
use that GridBagLayout on it's own, see:
https://packer.dev.java.net/

BTW: There is some odd behaviour (IMHO bugs) in GridBagLayout, which I 
found using packer, so it is worth trying extra constraints, even if 
they don't make sense.  BoxLayout maybe useful if you get stuck and 
don't have time to experiment.


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.
> 
> 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.
> 
Re: GridBagLayout
#76
Author: Hiroshi
Date: Fri, 23 Dec 2005 10:49
59 lines
3601 bytes
Thank you very much for your response. Yes, I'd like to try it out sometime.

I gave up GridBagLayout (since I didn't receive any response and since I didn't find anything on the Internet relating to my problem), and am using BoxLayout now as you suggested. Its behaviour is much predictable and logical; using struts and the nested Boxes, we can do what GrigBagLayout premisses, although code becomes longer and messy...)

Thanks again.


Infernoz <auto102767@hushmail.com> wrote:
>
>I suggest you use packer, a GridBagLayout helper, it is much easier to 
>use that GridBagLayout on it's own, see:
>https://packer.dev.java.net/
>
>BTW: There is some odd behaviour (IMHO bugs) in GridBagLayout, which I 
>found using packer, so it is worth trying extra constraints, even if 
>they don't make sense.  BoxLayout maybe useful if you get stuck and 
>don't have time to experiment.
>
>
>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.
>> 
>> 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.
>> 
Re: GridBagLayout
#84
Author: Paul Furbacher [TeamB]
Date: Sat, 07 Jan 2006 03:02
159 lines
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" &lt;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.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads