File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Gridbag span columns problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Gridbag span columns problem" Watch "Gridbag span columns problem" New topic

Gridbag span columns problem

Juhan Voolaid
Ranch Hand

Joined: Nov 18, 2003
Posts: 179
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 ]
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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.
Carsten Hansen

Joined: Sep 04, 2003
Posts: 9
Is this what you are trying to gain

Juhan Voolaid
Ranch Hand

Joined: Nov 18, 2003
Posts: 179
Yes ... the problem was with those weght atributes .. thnx ... it looks ok now.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.

SCJP2. Please Indent your code using UBB Code
I agree. Here's the link:
subject: Gridbag span columns problem
It's not a secret anymore!