1

Consider two JPanel instances using DesignGridLayout, like the following:

JFrame demoApplicationFrame = new JFrame();
JPanel singleDesignGridLayout1 = new JPanel();
DesignGridLayout layout1 = new DesignGridLayout(singleDesignGridLayout1);
layout1.row().grid(new JLabel("Short lbl1")).add(new JTextField(""));
layout1.row().grid(new JLabel("Short lbl2")).add(new JTextField(""));

JPanel singleDesignGridLayout2 = new JPanel();
DesignGridLayout layout2 = new DesignGridLayout(singleDesignGridLayout2);
layout2.row().grid(new JLabel("A bit longer label"))
    .add(new JTextField(""));
layout2.row().grid(new JLabel("Another long label here"))
    .add(new JTextField(""));

demoApplicationFrame.getContentPane().add(singleDesignGridLayout1,
     BorderLayout.NORTH);
demoApplicationFrame.getContentPane().add(singleDesignGridLayout2,
    BorderLayout.SOUTH);

Executing that code, will create this window:

Window screenshot

Is it possible to align the column for labels of the two panels?

Paolo Fulgoni
  • 5,208
  • 3
  • 39
  • 55
  • There is a *bad* way. You can set all labels to have equal widht (=max of the labels' widths). Or define relatively big iPadX – StanislavL Mar 06 '14 at 09:03
  • 1
    Do you necessarily need two panels? You could add all the labels and textfields to one single panel with DesignGridLayout. In that way I believe you can see the proper alignment. – Ehsan Khaveh Mar 06 '14 at 09:11
  • @Ehsan: I need different panels for two reasons. 1) They are dynamically generated from different functions. Of course I could just return a list of label-component pairs, but what if a row contains multiple components? 2) I need to put another panel between the two, with completely different content. – Paolo Fulgoni Mar 06 '14 at 09:26
  • 2
    Rather than generating 2 panels in 2 different functions, you could have 2 functions adding content to the same DesignGridLayout (that's the way I do it when I need reusability of similar layouts. – jfpoilpret Mar 06 '14 at 20:18

1 Answers1

3

Is it possible to align the column for labels of the two panels?

No it's not because DesignGridLayout works with grids within the same container. So if your components are placed in different panels there's nothing DesignGridLayout can do to ensure the alignment of cross-panels components.

In this context you can only look for workarounds (if any). Be aware of this: Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? Yes, we should. So setting a fixed labels size is not an option either.


Some thoughts

I was thinking about this matter and it's not a DesignGridLayout limitation but this is about how layout managers work. If you take a look to How Layout Managers work section of Laying Out Components Within a Container lesson, it states this:

  1. Layout managers basically do two things:

    • Calculate the minimum/preferred/maximum sizes for a container.
    • Lay out the container's children.

Layout managers do this based on the provided constraints, the container's properties (such as insets) and on the children's minimum/preferred/maximum sizes. If a child is itself a container then its own layout manger is used to get its minimum/preferred/maximum sizes and to lay it out.

This means if a child itself is a container then it may have its own layout manager to lay out its own children components. This property makes this container independent of its ancestor (about laying out components). If you had a "global" (just to name it somehow) layout manager that aligns components within two separate containers, then it would be breaking the aforementioned contract because children containers wouldn't be independent of their ancestors.

I'm relatively new to this layout managers stuff but I don't know any layout manager that allows you do what you need (or you feel you need), because they don't have to, actually. IMHO you should reconsider if you really need all those panels or you can manage all your components in a single container. Maybe there's a more suitable layout manager to your needs in this list:

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • 1
    I've submitted a [feature request](https://java.net/jira/browse/DESIGNGRIDLAYOUT-56) to the developers, let's see what's their opinion – Paolo Fulgoni Mar 06 '14 at 15:42
  • Great! Added to bookmark to keep myself updated :D @PaoloFulgoni – dic19 Mar 06 '14 at 16:24
  • If you think that this feature could be useful, I think you can vote it up on their issue traking system (you have to create an account) – Paolo Fulgoni Mar 06 '14 at 16:52
  • I didn't need such feature before but I could vote it up for sure. However please see my edit. It's probably more academic discussion than a concrete answer but hopefuly more experienced Swing developers can tell if I'm right (or I'm not). @PaoloFulgoni – dic19 Mar 06 '14 at 18:55
  • 1
    Yes @dic19 you are right: it is not possible for any LayoutManager to deal with cross-panels constraints, this is by design. I have tried to find some workarounds in the past, to no avail :-( One workaround, as you suggested, would be to ensure all your labels have the same preferred width (namely the max preferred width of all labels). – jfpoilpret Mar 06 '14 at 19:22
  • 1
    The real question is more to check if you really need those 2 panels, one under the other. Until now, I have never found a situation where this was needed, but that does not mean that cannot exist, though. – jfpoilpret Mar 06 '14 at 19:25
  • @jfpoilpret agreed: the real question is about those panels. Thanks for the feedback by the way :) – dic19 Mar 06 '14 at 20:11