• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

rowWeights and columnWeights

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how might one go about accessing and changing these fields of the GridBagLayout class? This isn't working!!
import java.awt.*;
public class GBag extends Panel {
public GBag() {
GridBagConstraints c = new GridBagConstraints();
Font f = new Font("Serif", 0, 36);
setFont(f);
GridBagLayout gbl = new GridBagLayout();
setLayout(gbl);
c.gridx = 0;
c.gridy = 0;
add(new Button("TL"), c);
c.gridx = 1;
add(new Button("Top Middle"), c);
c.gridx = 2;
add(new Button("TR"), c);
// weights
gbl.rowWeights[0] = 1; // NULL POINTER EXCEPTION!!
gbl.columnWeights[0] = 1;
}
public static void main(String args[]) {
Frame f = new Frame("GridBag Example");
f.add(new GBag(), BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
}

Any help would be appreciated!!
Lori
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your two lines:

Should read:
 
Lori Battey
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying not to use weightx and weighty per RHE Complete Java Certification (latest edition) on pages 318 and 319. It suggests that rowWeights and columnWeights is a simplified way. I am just not able to find any code examples showing this "simplified" way!
Anyone??
Lori
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lori,
To use rowWeights you need to actually initialize it and fill it in. It is null double array by default.

Since you are only setting one value, this approach is not as efficient as just setting the weightx, and weighty GridBagConstraints fields.
Matthew is incorrect in his code change that he gave you since the fields belong to GridBagConstraints and not GridBagLayout!

Of course you need to set these before your add the components into your panel. Therefore you will need to move the lines above to the correct location.
Regards,
Manfred.
 
Lori Battey
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great information! Thanks!!
This brings up another question though...should you set rowWeights and columnWeights for each row and column? I know when you set them using weightx and weighty you are specifying which row/column should be the "stretchy" one (I guess you can specify how much "stretchiness" as well). When using rowWeights and columnWeights do you have these same options of setting how much "weight" each row/column has?
Sorry to ask so many questions but I'm brand new to gridBagLayout. Got the other ones down!
Thanks for everyones help! I love JavaRanch!!
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I understand it, using either will achieve the same results.
Manfred.
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic