0

I have many JPanel with GroupLayout and I use addContainerGap() to add the gap on the borders. Also I have a panel with other layout (BorderLayout) and I want to add the same gap with its container as the group layout panels (I will add it through a empty border).

So my question is, how can I get the size of a container gap? Or is it better to use another approach?

I don't find how to get the size of the container gap so the only thing I can think is adding the panel inside a panel with a group layout and add there the containers gap.

PhoneixS
  • 10,574
  • 6
  • 57
  • 73
  • Please the one who had down voted, explain why have done it. How can I improve or fix it if you don't tell me what is wrong? – PhoneixS Mar 23 '15 at 08:36
  • The question was pretty poorly written, and maybe that had something to do with it. I was searching for information about grouplayout container gaps and got sent here, which is completely useless because this question is about how to use BorderLayout. – Erick Robertson Apr 03 '15 at 12:53
  • 1
    Yes, I'm not really good at making question easy to be read. The title now is more exact about what is this question about. But you are wrong about BorderLayout, I know how to use it. It is the context of the question, and the question really is only about GroupLayout. Thank you for you comment and edit. – PhoneixS Apr 24 '15 at 09:29

1 Answers1

6

For BorderLayout use:

  1. new BorderLayout(hgap,vgap) - or a combination of the following two:
  2. BorderLayout.setHgap(int)
  3. BorderLayout.setVgap(int)

Edit

But this doesn't answer how to get the gap size used by GroupLayout..

Look to GroupLayout.getLayoutStyle() then methods like: LayoutStyle.getPreferredGap(JComponent,JComponent,LayoutStyle.ComponentPlacement,int,Container) and of course note that the preferred gap might vary across the components of the layout.

For example you can use the next code to get the gap size for the north border:

LayoutStyle ls = gl.getLayoutStyle();

// Can be null if not already set.
if (ls == null) {
    // If not set, get the default style.
    ls = LayoutStyle.getInstance();
}

// What is the size of north gap if there is a JLabel?
System.out.format("North gap: %d", ls.getContainerGap(new JLabel(), SwingConstants.NORTH, null));
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    But this doesn't answer how to get the gap size used by GroupLayout as I want them to be the same. – PhoneixS Mar 20 '15 at 13:32
  • Thank you, it's what I wanted. I have added an example to your answer so others can see better how can be used. – PhoneixS Mar 23 '15 at 10:18