Results 1 to 13 of 13
  1. #1
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default GridBagLayout problems and questions

    Well i was searching for a layoutmanager that i could pretty much customize as much as i want... what i wanted to do is something like

    4 boxes(cells with labels with images and some with not inside them ) and i want all of them to have the same size... by using ipadx and ipaxy it just increases the X,Y of size of an existing label i want the cells to have a predefined size that i have set, i want every single Cell to have a different size though ( hence i picked GridBagLayout over GridLayout )
    and so that if put lets say a picture 30/100 the size of the cell wont change but it will stay the same.. ( as defined by me )

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Can you post an image of what you are trying to do? From the sounds of it, the 4 boxes, if they are the same size, should be laid out using GridLayout (since they are the same size), but perhaps the other components could be laid out using GridBagLayout, but I'd know better if I could visualize what you are trying to do.

    Much luck!

  3. #3
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default

    my question is not about that... its about making pre defined cells for gridbaglayout when saying predefined i mean with specific sizes even if it has no data in it i want it to have a specific size how can i do that? by adding an empty label for example or what?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by g123456 View Post
    my question is not about that... its about making pre defined cells for gridbaglayout when saying predefined i mean with specific sizes even if it has no data in it i want it to have a specific size how can i do that? by adding an empty label for example or what?
    Read the API documentation for the Box class; it can build components for you that are not visible themselves but do take up space.

    kind regards,

    Jos

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    If your boxes are all the same size, I believe that you are still much better off using a GridLayout for the container holding the boxes and then using a separate layout(s) for the boxes themselves.

    But regardless, best of luck.

  6. #6
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default

    yeah i doupt if box layout can help me.. what i want to do is..



    http://img163.imageshack.us/img163/1514/gridbaglayout.png



    In other words i need the first 4 boxes to be the same in the 2nd row different size But thats NOT THE MAJOR problem.. the real problem is that i dont know how to PREDEFINE size of a cell(box) in gridbaglayout

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by g123456 View Post
    In other words i need the first 4 boxes to be the same in the 2nd row different size ...
    Perhaps it's me, but this statement above confuses me. Could you describe in further detail?

    But thats NOT THE MAJOR problem.. the real problem is that i dont know how to PREDEFINE size of a cell(box) in gridbaglayout
    Most layouts respect the preferredSize of a component that is held by it, so one possible solution is to call setPreferredSize(BOX_SIZE) on your box component where BOX_SIZE is a constant Dimension that you've defined.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by g123456 View Post
    yeah i doupt if box layout can help me..
    I didn't mention the BoxLayout, you did; I mentioned the Box class for a factory of invisible components that do take up space.

    kind regards,

    Jos

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    A GridLayout example:
    Java Code:
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.Image;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class ShowGrid {
      private static final String[] DUKES = {
        "",
        "http://duke.kenai.com/iconSized/duke.gif", 
        "http://duke.kenai.com/iconSized/jws-dukeonly.gif",
        "http://duke.kenai.com/iconSized/penduke-transparent.gif",
        "http://duke.kenai.com/misc/PensiveDuke.gif", 
        "http://duke.kenai.com/misc/SunSteinSmall.png",
        "http://duke.kenai.com/boxer/boxer.gif",
        "http://duke.kenai.com/thinking/Thinking.jpg"
      };
    
      private static void createAndShowUI() {
        int gap = 10;
        JPanel panel = new JPanel(new GridLayout(2, 0, gap, gap));
        panel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
        panel.setBackground(Color.blue);
        List<String> dukeList = Arrays.asList(DUKES);
        Collections.shuffle(dukeList);
    
        for (String dukeImagePath : dukeList) {
          if (dukeImagePath.trim().isEmpty()) {
            panel.add(new JLabel());
          } else {
            try {
              URL imageUrl = new URL(dukeImagePath);
              Image dukeImage = ImageIO.read(imageUrl);
              Icon dukeIcon = new ImageIcon(dukeImage);
              JLabel label = new JLabel(dukeIcon);
              panel.add(label);
            } catch (MalformedURLException e) {
    
              e.printStackTrace();
            } catch (IOException e) {
    
              e.printStackTrace();
            }
          }
        }
    
        JFrame frame = new JFrame("ShowGrid");
        frame.getContentPane().add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            createAndShowUI();
          }
        });
      }
    }

  10. #10
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default Totally GridBag

    I used the GridBagLayout once and was
    very impressed by its ease and simplicity.

    I did not follow the API.
    I followed a procedure from a book on Java.
    The book is made of paper. I'll research
    the topic tonight and see if it's worth
    presenting here.

    In the mean time, here is a Flash Tutorial
    on GridBag, however, it may not be too
    encouraging.

    Totally Gridbag madbean

    Otherwise, Horstman tries to tame this
    topic at his website:

    Taming the GridBagLayout

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by paul pasciak View Post
    I used the GridBagLayout once and was
    very impressed by its ease and simplicity.
    You're a better man than I!

    I did not follow the API.
    I followed a procedure from a book on Java.
    The book is made of paper. I'll research
    the topic tonight and see if it's worth
    presenting here.
    I'd vote in favor of a small summary, but I'm afraid that it would be too much work to ask of you. Most definitely post information about the book since you found it helpful.

    Otherwise, Horstman tries to tame this
    topic at his website:

    Taming the GridBagLayout
    Many thanks for the useful information!

  12. #12
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default Brief GridBagLayout Guide

    By default, a GridBagLayout will prevent components
    placed within it from resizing.
    The result is a grid with cells sized to fit the largest
    of the dimensions from all the components used in
    the GUI.

    A typical, immutable grid might have cells with these
    boundaries.
    Java Code:
        _______________________________________
       |                 |        |            |
       |                 |        |            |
       |_________________|________|____________|
       |                 |        |            |
       |                 |        |            |
       |                 |        |            |
       |                 |        |            |
       |_________________|________|____________|
       |                 |        |            |
       |                 |        |            |
       |                 |        |            |
       |                 |        |            |
       |                 |        |            |
       |                 |        |            |
       |_________________|________|____________|

    Three instructions are necessary for a basic
    GridBagLayout:

    Java Code:
    setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    add(Component, GridBagConstraint);

    All of the formatting of a cell's appearance and
    resize behaviour is done through several values
    contained in the GridBagConstraint object.

    Only ONE GridBagContraint object is needed to
    format an entire panel.
    In the following table, the GridBagConstraint is
    named "c".
    The GridBagConstraints variables are named:
    gridx, weightx, anchor, fill,
    gridy, weighty.

    Here they are with a description of their purpose.

    Java Code:
    ---------------------------------------------------------------------
    
    c.gridx = 0;   c.gridy = 0;   //  Selects the first cell in the grid.
    
    ---------------------------------------------------------------------
    
    c.weightx = 0;  // Makes the column of the selected cell immutable.
    c.weighty = 0;  // Makes the row of the selected cell immutable.
    c.weightx = 1;  // Makes the column of the selected cell "stretchy".
    c.weighty = 1;  // Makes the row of the selected cell "stretchy".
    
    ( note: Typically 100 is chosen for this value. If several cells are
    given "stretch values", it is helpful if their combined value
    equals 100, because resize spacing amongst several
    components will be a percentage of the sum of their weights.)
    
    ---------------------------------------------------------------------
    
    c.anchor = GridBagConstraint.EAST  // Affixes a small component to
                                       // the EAST side of a large cell.
    
    (All values: NORTH, SOUTH, EAST, WEST, NORTHEAST,
                 NORTHWEST, SOUTHEAST, SOUTHWEST, CENTER. )
    
    ---------------------------------------------------------------------
    
    c.fill = GridBagConstraint.HORIZONTAL  // Makes a component fill its
                                           // cell horizontally.
    c.fill = GridBagConstraint.VERTICAL    // Makes a component fill its
                                           // cell vertically.
    c.fill = GridBagConstraint.BOTH        // Makes a component fill its
                                           // cell.
    
    ---------------------------------------------------------------------
    
    c.gridwidth = 2  // Extends the component's region to occupy 2 cells
                     // horizontally.
    
    c.gridheight = 3  // Extends the component's region to occupy 3 cells
                      // vertically.
    
    ---------------------------------------------------------------------

    Here is some code to copy, compile and experiment with.
    You'll see that very difficult GUI designs can be accomplish
    very easily with the GridBagLayout.

    Here is an example of a 3 x 3 grid that contains only three
    components:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class GB1 extends Panel{
    
      GB1(){
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
    
        c.gridx = 0;  gridy = 0;
        add( new Button("A very wide button"), c);
    
        c.gridx = 1;  gridy = 3;
        add( new Button("A"), c);
    
        c.gridx = 3;  gridy = 2;
        c.weightx = 1;
        c.weighty = 1;
        add( new Button("A"), c);
      }
    
      public static void main(String[] args){
        JFrame f = new JFrame("Brief GridBag Example");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GB1 gb1 = new GB1();
        f.add(gb1);
        f.pack();
        f.setVisible(true);
      }
    }

    Here is a GridBagLayout that uses some fancier
    "shorthand" and "auto-increment" features, which
    I haven't explained here.

    Java Code:
    import java.awt.*;
    import java.awt.*;
    import javax.swing.*;
    
    public class GB2 extends Panel{
    
      GB2(){
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
    
        add( new Button("A very wide button"), c);
        add( new Button("A"), c);
        c.gridwidth = GridBagConstraints.REMAINDER;    //  These three lines act
        add( new Button("A button"), c);               //  like a "\n" character for
        c.gridwidth = 1;                               //  a "new line' of buttons.
    
        add( new Button(), c);
        add( new Button("B"), c);
        c.gridwidth = GridBagConstraints.REMAINDER;    //  These three lines act
        add( new Button("Sea"), c);                    //  like a "\n" character for
        c.gridwidth = 1;                               //  a "new line' of buttons.
    
        add( new Button("D"), c);
        add( new Button("E"), c);
        c.gridwidth = GridBagConstraints.REMAINDER;    //  These three lines act
        add( new Button("F"), c);                      //  like a "\n" character for
        c.gridwidth = 1;                               //  a "new line' of buttons.
      }
    
      public static void main(String[] args){
        JFrame f = new JFrame("Brief GridBag Example");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GB2 gb2 = new GB2();
        f.add(gb2);
        f.pack();
        f.setVisible(true);
      }
    }
    Last edited by paul pasciak; 06-02-2010 at 07:04 PM. Reason: Correct errors in second GridBagLayout example (GB2)

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. GridBagLayout
    By carderne in forum New To Java
    Replies: 8
    Last Post: 01-25-2009, 03:06 PM
  2. GridBagLayout
    By MuslimCoder in forum New To Java
    Replies: 1
    Last Post: 01-15-2009, 09:54 PM
  3. abt gridbaglayout
    By pinky in forum AWT / Swing
    Replies: 1
    Last Post: 12-15-2008, 09:35 AM
  4. Replies: 1
    Last Post: 02-17-2008, 12:43 AM
  5. gridbaglayout
    By newtojava7 in forum New To Java
    Replies: 4
    Last Post: 01-27-2008, 09:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •