4

I have a situation where a user enters a String and my code makes a Jlabel for it and attempts to center it on a full screen JFrame/Pane. My problem is, to be able to accurately center it, I need to know its dimensions.

I need a way to figure out how long (wide) the JLabel should be so that it accomodates the length of the string and nothing more. I tried using the preferredSize() method that components have and it looked horrible (I passed a null value to let my UI determine the preferredSize). I know how to figure out the height that works (the size of the fontSize + 10px is usually good) but I can't get the width.

I am using absolute positioning (no LayoutManager).

Thanks

user83643
  • 2,901
  • 2
  • 17
  • 13
  • 3
    It sounds like you should use a LayoutManager for this. – Jonas Jun 07 '11 at 13:38
  • 1
    Unfortunately, for my current solution that's not possible. LayoutManagers do it a certain way, so it must be possible to implement. – user83643 Jun 07 '11 at 13:39
  • You can always use a layout manager (or nest panels using different layout managers). The fact that you are using the preferred size to determine the position of the component is exactly what the layout manager does in a well defined API. If for some reason none of the layout manager do what you want then you can write your own layout manager (which is what you are doing now). There is no need to use absolute positioning, unless you have an application that drags components to random places on a panel. – camickr Jun 07 '11 at 14:48

4 Answers4

8

I believe the actual width comes from getPreferredSize().getWidth() if you don't set it beforehand. Try outputting the preferred size without setting it to null. With MigLayout when centering components, I normally use pref! to make the component as small as possible for all sizes and avoid wrapping, and then center it in the spacious parent container.

Chris Dennett
  • 22,412
  • 8
  • 58
  • 84
  • 1
    I just tried that and it seems to work. I'll test it a little more robustly and let you know how it comes out. It's weird how that works but when I set preferredSize() it doesn't get the width right ... odd – user83643 Jun 07 '11 at 13:44
  • 1
    I ended up using this solution (even though the other was equally correct and good) because of the amount of lines it takes up. Thanks @Chris – user83643 Jun 07 '11 at 13:52
6

If you are already using the font height + 10 for height, you can easily use the string width + x.

You will use FontMetrics:

Graphics g;
FontMetrics met = g.getFontMetrics();
int height = met.getHeight();
int width = met.stringWidth(label.getText());
jjnguy
  • 136,852
  • 53
  • 295
  • 323
  • Although I might have found my fix (no drug puns intended), I would love to see this code. – user83643 Jun 07 '11 at 13:45
  • I didn't know you could do this, thanks for the code. I testing this and it works. So now, I have two working and elegant solutions and only one accepted answer to give out. – user83643 Jun 07 '11 at 13:49
  • @user, `:)` accept whichever one you end up using in your code. No worries if it isn't mine. – jjnguy Jun 07 '11 at 13:50
2

by using some of LayoutManagers check this How to determine the length of a graphic string?

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

I strongly suggesting using a LayoutManager for your GUI. Not sure why you are using absolute positioning, but this is a perfect example of the problems it causes.

FlowLayout has a simple centering ability and many other layout manager support centering as well.

jzd
  • 23,473
  • 9
  • 54
  • 76