• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Gridbag span columns problem

 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
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
|
Action
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.setSize(640,480);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
f.setResizable(false);
f.setLocation(100, 100);
Container container=f.getContentPane();

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

c.gridheight=3;
c.gridwidth=3;
c.gridx=0;
c.gridy=0;
Button b1=new Button("Action");
GB.setConstraints(b1, c);
container.add(b1);

c.gridheight=1;
c.gridwidth=1;

c.gridx=3;
c.gridy=0;
Button b2=new Button("Start");
GB.setConstraints(b2, c);
container.add(b2);

c.gridx=3;
c.gridy=1;
Button b3=new Button("Stats");
GB.setConstraints(b3, c);
container.add(b3);

c.gridx=3;
c.gridy=2;
Button b4=new Button("Spells");
GB.setConstraints(b4, c);
container.add(b4);

f.setVisible(true);



}
}

//end

please help me ... thnx
[ December 26, 2003: Message edited by: Juhan Voolaid ]
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this what you are trying to gain
Carsten

 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic