This discussion is archived
4 Replies Latest reply: Oct 28, 2010 11:03 AM by splungebob RSS

do you write GridBagLayout code by hand?

801461 Newbie
Currently Being Moderated
I used to do a lot of Swing development and I became fluent in Swing LayoutManagers -- especially GridBagLayout (which is like a foreign language to most people ;).

I wrote a couple very nice utilities to generate the GridBagLayout code for me based on a drag-and-drop UI and it saved me a lot of time. I am wondering, are there still a lot of people writing such code by hand? I've been working in the JSF world for the past couple years, so I don't know if there's a need for such utilities anymore.

If there is, I might consider spending some time with my utility and packaging it up as a proper application. Or possibly just releasing the source as an open source project?

So, again, does anyone still write GridBagLayout code by hand?
  • 1. Re: do you write GridBagLayout code by hand?
    322169 Newbie
    Currently Being Moderated
    Good grief, I'd probably stab myslef repeatedly with a fork, were I to do it by hand. In days of yore I wrote a wrapper class, named GBC (for faster typing). It would contain a set of constructors, creating underlying GridBagConstraints, and then methods for modifying each constraint, and each method returned the object itself. The ok() method returned the underlying constraints object, So, the actual code looked something like:
    add(new JLabel("test"), new GBC(0, 0).anchor(WEST).fill(BOTH).ok());
    or actually:
    add("test", new GBC(...));,
    since my panel's add methods are always overridden to allow easy adding of labels.

    But since I stumbled upon MigLayout... I have never looked back. The last time I used GridBagLayout was maybe a year ago. And only to implement a change request to some old codebase.

    Edited by: rkr1410 on 2010-10-28 09:58

    Edited by: rkr1410 on 2010-10-28 09:59 - aded code tags
  • 2. Re: do you write GridBagLayout code by hand?
    splungebob Explorer
    Currently Being Moderated
    Like rkr1410 does, I too wrote some utility classes (as I think many/most do) when working with GBL. However, I still consider this "doing it by hand". I always thought doing anything Swing "by hand" was in comparison to using a GUI builder.
    rkr1410 wrote:
    So, the actual code looked something like:
    add(new JLabel("test"), new GBC(0, 0).anchor(WEST).fill(BOTH).ok());
    I like the chaining idea you have here. Right now, I just have some overloaded methods on my helper class with the most commonly used param parings, using default values when a param is not present. Consequently, having every permutation of 11 parameters is not practical.

    I'm curious though, why the ok() method? Isn't the chaining that you have already returning the constraints object each time?
  • 3. Re: do you write GridBagLayout code by hand?
    322169 Newbie
    Currently Being Moderated
    I'm curious though, why the ok() method? Isn't the chaining that you have already returning the constraints object each time?
    That's beacuse the class was a wrapper, not extending GridBagConstraints, but rather storing them as an instance variable. So, the various methods returned the wrapper to perform further operations on, and the ok() method returned the actual GridBagConstraints instance.

    As to why the class wasn't extending GridBagConstraints... well. It's a code so old I seriously can't remember what was my rationalization. Now it seems just a bug, it should extend GridBagConstraints and have no ok() method.


    And once again, I encourage you to look up MigLayout . It's free. It's easy. And it will solve any problem you ever had with GridBagLayout.
  • 4. Re: do you write GridBagLayout code by hand?
    splungebob Explorer
    Currently Being Moderated
    rkr1410 wrote:
    And once again, I encourage you to look up MigLayout . It's free. It's easy.
    Yeah, it's been on my hit list to experiment with non-JDK layouts. Never any time to, it seems.
    And it will solve any problem you ever had with GridBagLayout.
    I never have :-)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points