• 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 ...
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Gridbag span columns problem

Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating a simple gui. Right now i just insert buttons inside the gridbag cells and i have this goal:

(--|--|--)(--) <-button "Start"
(-bigone-)(--) <-button "Stats"
(--|--|--)(--) <-button "Spells"
button "action"
So that schema means that i have:
-big button "action" (0,0) that spans 3 rows and 3 cols
-button "Start" that that is on 4th column and 1st row (0,3)
-button "Stats" (1,3)
-button "Spells" (2,3)
ok, but with the code below i get this result:
(--|--)(--|--) <-Start
(--|--)(--|--) <-Stats
(--|--)(--|--) <-Spells
That means button action only spans 2 cols instead of 3 and
rest of three buttons are allso 2 cols wide instead of 1. But rowspan
is ok.
Here is the code:
import java.awt.*;
import javax.swing.*;
public class hero extends JFrame{

public static void main(String[] args){
hero h = new hero();

public hero(){
JFrame f=new JFrame("Hero ... early development");
f.setLocation(100, 100);
Container container=f.getContentPane();

GridBagLayout GB = new GridBagLayout();
GridBagConstraints c= new GridBagConstraints();
c.weightx = 1;//fill x
c.weighty = 1;//fill y
c.fill=GridBagConstraints.BOTH;//lauotab componendid laiali

Button b1=new Button("Action");
GB.setConstraints(b1, c);


Button b2=new Button("Start");
GB.setConstraints(b2, c);

Button b3=new Button("Stats");
GB.setConstraints(b3, c);

Button b4=new Button("Spells");
GB.setConstraints(b4, c);




please help me ... thnx
[ December 26, 2003: Message edited by: Juhan Voolaid ]
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is directly relevent to our discussion here and is essentially the exact same issue that was causing me grief. All columns within a GridBagLayout are not necessarily the same size, they vary in size based upon the components they contain. Since your components all have the same weight it will try to give them the same amount of space, hence they each take up half the width. It's probably not even using two of those columns, it's making one of the three as big as the right column and the other two columns have no size.
The solution is simple, change the weight of the components so that the right component is given the 1/4 the screen and the left components are given 3/4 the screen. I believe it would be 0.75 and 0.25 for the weights but it MIGHT be 1.0 and 0.25, I'm not -entirely- sure how it calculates weights because it's supposedly based on the largest weight, and if 0.75 is the largest 0.25 would be one third in which case your right component turns out to be too big (1/3 instead of 1/4). It should be one or the other.
Personally I think it would be best if we continued this in the other thread since we were talking about the exact same thing and then we could keep the discussion/questions in one place.
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this what you are trying to gain

Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes ... the problem was with those weght atributes .. thnx ... it looks ok now.
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is the best way to go with GridBagLayout:
A) Draw the buttons on a piece of paper.
B) Draw a vertical line between Action and the rest of the buttons.
C) Draw two horizontal lines; between Star and Stats, and between Stats and Spells
Now we see that Action does not need to expand more than a column, but it needs to expand the three rows.
If you want the Action button to have a bigger width, either set its preferred size or use ipadx.
If you want some component's space to be modified when resizing use weigthx/y.
In steps B and C we have figured out the grid on which the components are laid out.
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!