Hi Edward,

Remember, you are working on grd bag layout. The same column cannot have
different widths in different rows, so it will adjust  to the width of the
component having max width. For this, it will use the preferred sizes of the
components. In your case, the preferred width of the first button will be
chosen for determining the column width.

        Easy way for this to work is to set the same preferred width for all
the components. However, that may not be the best solution. In general, a
single layout cannot fit for all purpose, so try to use layered panels.
Also, gridbaglayout is a bit complex, and hand coding will consume a lot of
time unless you know it thoroughly. Using a good IDE can eliminate such
issues.

regards,
Krishna

-----Original Message-----
From: Edward King [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, December 18, 2002 1:35 PM
To: Java_Swing
Subject: GridBagLayout question


  I defined a GridBagLayout and put three component in it,I want to get this
goal which I want to every component in the GridBagLayout occupy equal col
size,like follows:

  
   col 
row 0                 1                  2 
 0  _________________________________________________________
 1 |  This is a test1 |                  |                   |
 2 |  test2           |                  |                   |
 3 |                  |                  |                   |
 4 |                  |                  |                   |
 5 |     one          |       two        |      thre         |
    ---------------------------------------------------------

So I design my code like follows:
JLabel label1=new JLabel("This is a test");
JLabel label2=new JLabel("test2");
JButton button1=new JButton("one");
JButton button2=new JButton("two");
JButton button3=new JButton("thre");
GridBagLayout gbLayout=new GridBagLayout();
GridBagConstraints gbConstraints=GridBagConstraints();
Container c=getContentPane();
c.setLayout(gbLayout);

//add label1
gbConstraints.weightx=0;
gbConstraints.weighty=0;
gbConstraints.gridx=0;
gbConstraints.gridy=0;
gbConstraints.gridwidth=1;
gbConstraints.gridheight=1;
gbConstraints.anchor=GridBagConstraints.WEST;
gbLayout.setConstraints(label1,gbConstraints);
c.add(label1);

//add label2
gbConstraints.gridy=1;
gbLayout.setConstraints(label2,gbConstraints);
c.add(label2);

//add button1
gbConstraints.gridx=0;
gbConstraints.gridy=5;
gbLayout.setConstraints(button1,gbConstraints);
c.add(button1);

//add button2
gbConstraints.gridx=1;
gbConstraints.gridy=5;
gbLayout.setConstraints(button2,gbConstraints);
c.add(button2);

//add button3
gbConstraints.gridx=2;
gbConstraints.gridy=5;
gbLayout.setConstraints(button1,gbConstraints);
c.add(button3);
show();

But when I run it,I found I got another result,each JButton occupy different
col size,why?
  col 
row 0                 1        2 
 0  _____________________________________
 1 |  This is a test1 |        |         |
 2 |  test2           |        |         |
 3 |                  |        |         |
 4 |                  |        |         |
 5 |     one          |  two   |  thre   |
    --------------------------------------

I want to make JButton one,two and three looks occupy equal width in the
screen. But JButtone looks bigger than JButton two and JButton three. How to
make three JButtons look equal width size?

Thanks in advance!







      
_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing
_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing

Reply via email to