Margin/padding in GridBagLayout Java
Asked Answered
J

2

22
  1. Is it possible to set an margin/padding in GridBagLayout for the whole row/column? I use the inset on the constraints-object however, using this approach I need to set padding from bottom on every single component.

  2. Is it possible to pad all of the content in the JFrame? Because now every component is aligned with the frame.

  constraints.weightx = 2;
  constraints.weighty = 1;
  constraints.fill = GridBagConstraints.BOTH;
  addComponent(this, layout, constraints, questionPanel = new QuestionPanel(), 0, 0, 1, 1);

  constraints.weightx = 1;
  constraints.weighty = 1;
  constraints.fill = GridBagConstraints.BOTH;
  addComponent(this, layout, constraints, categoryPanel = new CategoryPanel(), 0, 1, 1, 1);

  constraints.weightx = 1;
  constraints.weighty = 0;
  constraints.fill = GridBagConstraints.HORIZONTAL;
  addComponent(this, layout, constraints, answerPanel = new AnswerPanel(), 1, 0, 2, 1);

  constraints.weightx = 1;
  constraints.weighty = 2;
  constraints.fill = GridBagConstraints.BOTH;
  addComponent(this, layout, constraints, tabPane = new JTabbedPane(), 2, 0, 2, 1);

  constraints.weightx = 1;
  constraints.weighty = 0;
  constraints.fill = GridBagConstraints.NONE;
  constraints.anchor = GridBagConstraints.SOUTHEAST;
  addComponent(this, layout, constraints, buttonPanel = new ButtonPanel(), 3, 1, 1, 1);

I am using a private method addComponent:

private void addComponent(Container context, GridBagLayout _layout, GridBagConstraints            _constraints, Component component, int row, int column, int width, int height) {
  _constraints.gridx = column;
  _constraints.gridy = row;
  _constraints.gridwidth = width;
  _constraints.gridheight = height;

  _layout.setConstraints(component, _constraints);
  context.add(component);
 }

How can I add some "air(padding/margin)" between the cells?

Jauch answered 30/1, 2011 at 10:0 Comment(2)
I'd say yes and yes... However, without lookint at some code, it is hard to tell for sure. Could you post a snippet?. It would help a lot, your questions are very generic.Botulinus
Downvote because there are two questions there.Photometry
S
9

As far as I know, the only way of doing the first one is to set an Insets object for each constraint... (Hand-coding layout clearly wasn't designed to be easy in Java :P )

But there's a very simple way of doing the second, without messing around with any Insets or anything: set the layout of the contentPane to a FlowLayout with whatever vertical and horizontal gaps you want, add a panel to the contentPane, and instead of adding everything to the contentPane, add it to your new panel.

Stichous answered 30/1, 2011 at 10:54 Comment(0)
V
53

Use the insets property of the GridBagConstraints class

For example:

GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(3,3,3,3);

Hope this is what you are looking for.

Vanhook answered 18/6, 2011 at 0:40 Comment(0)
S
9

As far as I know, the only way of doing the first one is to set an Insets object for each constraint... (Hand-coding layout clearly wasn't designed to be easy in Java :P )

But there's a very simple way of doing the second, without messing around with any Insets or anything: set the layout of the contentPane to a FlowLayout with whatever vertical and horizontal gaps you want, add a panel to the contentPane, and instead of adding everything to the contentPane, add it to your new panel.

Stichous answered 30/1, 2011 at 10:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.