Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Understanding GridBagLayout

  1. #1
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Understanding GridBagLayout

    Hi,

    I came across this useful class but couldn't understand how exactly it determines how to divide the frame, because in all the examples I've seen there's never been any declaration for how many cells or what size each row or column should get.

    Is there another useful Layout class I should know about?

    Thanks :)

  2. #2
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: Understanding GridBagLayout

    There are many layouts to choose from. I personally use a layout of my own design, but it all depends on what you need it for. I hold a grudge against gridbaglayout, but I have one question before going further. Have you tried this tutorial?

    How to Use GridBagLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
    My API:
    Java Code:
    cat > a.out || cat > main.class

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: Understanding GridBagLayout

    To partly answer your question: GBL allots horizontal/vertical space of each occupied column/row, based on the gridx/y/width/height fields of the GBC with which a component was added to the layout.

    Any remaining space is allocated in proportion of the largest weightx/y associated with a component exclusively occupying the column/row (not components that span more than one column/row).

    Other properties like anchor, insets, padding and fill are pretty much self-explanatory, but the only way to learn GBL is to use it and play around with the constraints.

    An interesting but little noticed feature is that you can retrieve and mutate the GBC for a particular component at runtime.

    That said, for anyone who isn't planning to make a living as a GUI designer, a better option is to nest containers, each with their own layout, to achieve a complex layout. And if you do go professional, you'll end up investing the almost absurd amount of time and effort it takes to master a visual designer, and never look at layout code again.

    db
    Mapisto likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: Understanding GridBagLayout

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: Understanding GridBagLayout

    Yea, I did see this guide, but only now I get the idea that it gets sized by the components inserted to it.

    Maybe I'll take a second look at it and try playing with it once again until I get it.

    Thanks.

  6. #6
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: Understanding GridBagLayout

    By the way,

    I've never really worked at programming or did anything proffesional, but you guys want to tell me that any programmer that isn't really a designer uses nested panels instead of layouts? :| that's a shock.

    I mean, it doesn't look proffesional using nested panels, because sometimes you'll have to nest a lot of panels. Doesn't it take too much memory and increases the loading time significantly?

    Thanks!

  7. #7
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: Understanding GridBagLayout

    Quote Originally Posted by Mapisto View Post
    By the way,

    I've never really worked at programming or did anything proffesional, but you guys want to tell me that any programmer that isn't really a designer uses nested panels instead of layouts? :| that's a shock.

    I mean, it doesn't look proffesional using nested panels, because sometimes you'll have to nest a lot of panels. Doesn't it take too much memory and increases the loading time significantly?

    Thanks!

    If you are nesting a lot of panels, maybe there is a method you could use that requires less panels and still reaches the same effect. The layout you choose does not make a GUI look professional, its the programmer who molds it who makes it look professional. I have used every single standard layout (except the one that eclipses uses), for my applications, and I come to find that it is not that one look more or less professional but that they all tools meant for different problems. If you have a complex problem, combining them or even nesting them can help you achieve your goal.

    To answer your question about load time, it depends on how big the program is as a whole. I have a game with a huge amount of nested JPanels, and it takes 3-5 seconds to load up. To me that is a long time, but I have not even began to optimize the game. If your GUI is as big as my game, then a loading time of a few seconds (in my opinion) is not that bad of a thing. If it does become a problem, see if there are better methods to laying out your components in your GUI.
    My API:
    Java Code:
    cat > a.out || cat > main.class

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: Understanding GridBagLayout

    Quote Originally Posted by kammce View Post
    I have used every single standard layout
    OT: Even the one the Swing layout tutorial doesn't even mention? I've never seen that used, and the description doesn't encourage me to think it's highly usable.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: Understanding GridBagLayout

    To add one more 'feature' of GBL: if unable to provide enough horizontal/vertical space for the preferred width/height, it directly shrinks the component to its minimum width/height.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

Similar Threads

  1. GridBagLayout vs me: 1 - 0
    By JosAH in forum AWT / Swing
    Replies: 9
    Last Post: 08-19-2011, 09:18 PM
  2. GridBagLayout Help
    By Zman3359 in forum AWT / Swing
    Replies: 0
    Last Post: 02-21-2011, 08:39 PM
  3. GridBagLayout
    By carderne in forum New To Java
    Replies: 8
    Last Post: 01-25-2009, 02:06 PM
  4. GridBagLayout
    By MuslimCoder in forum New To Java
    Replies: 1
    Last Post: 01-15-2009, 08:54 PM
  5. abt gridbaglayout
    By pinky in forum AWT / Swing
    Replies: 1
    Last Post: 12-15-2008, 08:35 AM

Posting Permissions

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