Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

rowWeights and columnWeights

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your two lines:

Should read:
 
Lori Battey
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I understand it, using either will achieve the same results.
Manfred.
 
You showed up just in time for the waffles! And this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic