1

I have for example 3x3 components in grid layout, and I would like all of them to be labels, but since I have some padding I want my labels to be in center of their cell. But I can't seem to manage it...

Part of the relevant code:

panel = new JPanel();
GridBagLayout gridBag = new GridBagLayout();
panel.setLayout(gridBag);
panel.setSize(new Dimension(30, 400));
GridBagConstraints c = new GridBagConstraints()     

JLabel lab = new JLabel("proba"); 
lab.setBorder(outline);
c.fill = GridBagConstraints.BOTH;
c.gridx =2; c.gridy=2; c.ipady = 10; c.ipadx=10;

c.ipadx=100; panel.add(lab,c);
user35443
  • 6,309
  • 12
  • 52
  • 75
obey
  • 796
  • 7
  • 16
  • 1) *"Part of the relevant code:"* For better help sooner, post an [SSCCE](http://sscce.org/). 2) A quick-n-dirty way to center a component (or a group of them) is to put in (or them) into a panel, and then put the panel into another panel with a `GridBagLayout` with no constraint that is then added to the outermost GBL. You can see an example of that in the red/orange image seen in the [Nested Layout Example](http://stackoverflow.com/a/5630271/418556). – Andrew Thompson Nov 17 '12 at 03:09

1 Answers1

1

[update]

You really should post an SSCCE so we can all try it and not have to guess where the problem is. My last guess - and this is something you should do regardless of whether it fixes your current problem - is

    label.setHorizontalAlignment(JLabel.CENTER);

If your label is centered and fills the entire grid cell, the text will still be left justified by default. The above change will cause the text to be centered within the label.

However if your label is not filling the cell, this won't help.

[original]

It's hard to say exactly what's wrong since this is not a full program, but here are a few comments that might get you on the right track.

First, you should be using setPreferredSize instead of setSize (see Java: Difference between the setPreferredSize() and setSize() methods in components)

When you make this change you will see that the panel is not quite what you want. It's very tall and thin. Perhaps a typo - did you mean (300,400) instead of (30,400)

Now I'm guessing all your labels will be clumped together. In order to get them to spread out you need to add:

c.weightx = .5;
c.weighty = .5;

(actually any non-zero value will work). This is described in http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container. This is because when the weight is 0.0 (the default), the GridBagLayout puts any extra space between its grid of cells and the edges of the container.

This should get you close(r) ...

Community
  • 1
  • 1
Guido Simone
  • 7,912
  • 2
  • 19
  • 21
  • Ok, so i got what you wanted to say about weightx,y and i know about it but the point is that i want to make a game of Yamb. so i want my cells small as a xxx number size. Also it wasn't a type about 30,400 coz i wanted to make a panel that looks like a column so that i could put a column of 15 numbers in it. but i changed it now coz it's unnecessary. My bottom line problem is how to put a label for example "56" or "tralala" into the middle of it's own cell. they are all stuck at left side (middle, concerning y). – obey Nov 16 '12 at 19:41
  • @user1830241 : Simply you have to use this `JLabel lab = new JLabel("proba", JLabel.CENTER);`. Seems like this alone will work for you, as pointed out by **"GuidoSimone"**. – nIcE cOw Nov 17 '12 at 05:57