• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Values for weightx and weighty

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have seen conflicting information for the values of weightx and weighty in the GridBagConstraints class. I just consulted the API documentation, but it does not say. Are the values between 0.0 and 1.0 or between 0 and 100? Does anyone know for sure? I've seen it both ways.
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
double weightx
double weighty
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I know, you can give a component a weightx or weighty greater than 1.0 if you want, thought there isn't much point, as if everything else is set to 0.0 it will have the same effect - the weighting is divided proportionally.
 
Tony Alicea
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
weightx, weighty
Specifying weights is an art that can have a significant impact on the appearance of the components a GridBagLayout controls. Weights are used to determine how to distribute space among columns (weightx) and among rows (weighty); this is important for specifying resizing behavior.
Unless you specify at least one nonzero value for weightx or weighty, all the components clump together in the center of their container. This is because when the weight is 0.0 (the default), the GridBagLayout puts any extra space between its grid of cells and the edges of the container.
Generally weights are specified with 0.0 and 1.0 as the extremes, with numbers in between used as necessary. Larger numbers indicate that the component's row or column should get more space. For each column, the weight is related to the highest weightx specified for a component within that column (with each multi-column component's weight being split somehow between the columns the component is in). Similarly, each row's weight is related to the highest weighty specified for a component within that row. Extra space tends to go toward the rightmost column and bottom row.
---
From the Sun AWT 1.1 tutorial which has been archived and available only via download:
http://java.sun.com/docs/books/tutorial/information/download.html#OLDui
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that the same info is still available online as part of the new GUI Tutorial, which emphasizes Swing, but still covers some AWT classes like LayoutManagers because Swing itself still uses AWT LayoutManagers. The GridBagConstraints part is here.
Although the tutorial says generally weight values are from 0.0 to 1.0, the actual limits are somewhat wider. Since weights are represented by doubles, it's possible to put in any value from -Double.MAX_VALUE to +Double.MAX_VALUE (i.e. -1.8E308 to +1.8E308). However, any negative values are treated exactly as if they were zero - it's possible, but completely useless. On the other hand, values greater than 1.0 certainly are possible and have a notable effect - a weight of 2.0 gets twice as much space as a weight of 1.0.
Note also that the API says nothing about 1.0 being any sort of maximum; it's just a convention used by a lot of people. I've never seen any example where 100.0 is the "maximum", but that's no less valid than choosing 1.0 as the "maximum". It doesn't matter whether you use weights of 0.0, 0.1, 0.5 and 1.0, or 0.0, 10.0, 50.0, and 100.0 - or even 0.0, 1E100, 5E100, and 1E101. All that matters is that you make sure the ratios are what you want them to be.
So - I think the best answer is 0.0 to Double.MAX_VALUE. If it's multiple choice and that's not available, go with 0.0 to 1.0, or -Double.MAX_VALUE to Double.MAX_VALUE (depending whether they ask for "possible values" or "typical values").

[This message has been edited by Jim Yingst (edited February 24, 2000).]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic