DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2005

    GridBag Layout Question

    I'm trying to get to grips with GridBagLayout, but I'm having a bit of a problem working out how to position components. First, I'm using this method to add components to the container:
    private void newComponent(Component component, int gridx, int gridy, int gridwidth, 
    				int gridheight, int weightx, int weighty, int fill, int anchor) {
    			GridBagConstraints constraints = new GridBagConstraints();
    			constraints.gridx = gridx;
    			constraints.gridy = gridy;
    			constraints.gridwidth = gridwidth;
    			constraints.gridheight = gridheight;
    			constraints.weightx = weightx;
    			constraints.weighty = weighty;
    			constraints.fill = fill;
    			constraints.anchor = anchor;
    			g1.setConstraints(component, constraints);
    Then, adding components is like this:
    newComponent(titleLbl, 0, 0, 2, 100, 10, 100, GridBagConstraints.NONE, GridBagConstraints.CENTER);
    		newComponent(catLbl, 15, 40, 8, 45, 100, 10, GridBagConstraints.HORIZONTAL, GridBagConstraints.EAST);
    What I don't understand is that even though every parameter of the above components is different, the two labels are still being added side by side on the same "line" - the x and y positions seem to make no difference whatsoever. Can anyone help me out with this?


    Edit - what would make this a bit easier is if I could sort of "see" the grid that I'm putting the components into. Is there any way of specifying the number of cells in rows and columns? At least then the results I'm getting might make some sense?
    Last edited by will808; 03-30-2005 at 09:41 AM.

  2. #2
    Join Date
    Jan 2005
    Ah, ok, I got it.

  3. #3
    Join Date
    Jan 2005
    Reisterstowwn, MD
    There are a couple of things that seem to be wrong here:

    First off, your gridX and gridY dont match up at all. Your first component starts at 0,0 with a width of 2 and a height of 100. Then your second component starts at cords (15,40) with a width of 8 and height of 45. This seems a little screwy to me, your placing a component isnisde an already occupied grid. Maybe some people do this but the way i have learned to do grid bag layouts is that each component has its own grid space that it shares with no other components. If you want two components to share the same grid space then you place them in a panel together. Anyways, it doesn't end there. Another thing is i notice your weights are different, i don't know if you meant to do this but i have read it is best to set both of them to 100. Unless you dont want your components to resize then you set to 0. But wait, theres even more i see, for your second component you set it to fill horizontaly. Correct me if i'm wrong, but if your components are inside the same grid space and you tell one of them to fill i think they are going to touch.

    Anyways thats what i see.

    It seems you want to put the components on different "lines," meaning you want one above the other? I would try something like this:

    newComponent(titleLbl, 0, 0, 1, 1, 100, 100, GridBagConstraints.NONE, GridBagConstraints.CENTER);
    newComponent(catLbl, 0, 1, 1, 1, 100, 100, GridBagConstraints.NONE, GridBagConstraints.CENTER);
    Maybe that might be closer to what your looking for, no guarentees though.
    Java has 99 problems but a pointer ain't one

  4. #4
    Join Date
    Jan 2005
    Thanks for the reply - what I was doing with the above was messing about with the parameters in the hope of being able to see something visual, so the code above was pretty meaningless. In the end I used a JLabel starting at 0,0 that covered 2 cells high and 20 cells horizontally.

    This gave me a kind of visual reference to work to. What I find difficult is that there seems to be no way of limiting or setting the amount of cells you want, so either it will look screwy until you've entered every component, or you do what I did and enter a large component first to give some kind of reference point. Still, I got it to work, and I'm dead pleased.

    One question though - what I'd really like is three panels in a North, Center, South BorderLayout, and use a GridBag Layout for the center panel. I've only tinkered with it so far, but is this actually possible? I guess it must be, but my first attempts are a bit messy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.