1

I have following code that is used to compute dialog title width.

FontRenderContext frc = new FontRenderContext(null, true, true);
TextLayout tl = new TextLayout(getTitle(), getFont(), frc);
double w = tl.getPixelBounds(null,  0, 0).getWidth();

However for some reason text width is computed wrongly. I checked this code for computing radio button label text width and it worked correctly. My main concern is about dialog font, I am not sure if I correctly get it.

For example for title test computed width is 20 however actual width is 23. The longer string is the bigger difference between computed and actual widths.

2
  • 1
    sounds to me like it computes width for the wrong font Commented Jul 3, 2012 at 10:45
  • 1
    For better help sooner post an SSCCE Commented Jul 3, 2012 at 11:16

1 Answer 1

5

You're getting a wrong result because the dialog title and the font it's using are native resources.

If you're application is Windows-only, you can get the width with this code:

Font f = (Font)Toolkit.getDefaultToolkit().getDesktopProperty("win.frame.captionFont");  
Graphics gr = getGraphics();  
FontMetrics metrics = gr.getFontMetrics(f);  
int width = metrics.stringWidth(getTitle());  

Otherwise try to get the FontMetrics from the title bar's font:

Container titleBar = (Container) dialog.getLayeredPane().getComponents()[1];
FontMetrics metrics = titleBar.getFontMetrics(titleBar.getFont());
int width = metrics.stringWidth(getTitle());

If it's to dynamically set the width of the dialog, you also need to take into account the LaF spacing and the borders. Try this:

// This is the space inserted on the left of the title, 5px in Metal LaF
width += 5; 

// This is the space for the close button, LaF dependent.
width += 4;

// Add the borders
width += dialog.getWidth() - dialog.getContentPane().getWidth();

// Finally set the size
dialog.setSize(new Dimension(width, dialog.getPreferredSize().height));

Hopefully this will work. If you wonder where the numbers come from, they're in the JDK source code.

Sign up to request clarification or add additional context in comments.

6 Comments

windows only solution, but it is better than nothing, thanks :)
are there any solution that does not depend on windows?
the second block of code I've written is not windows-specific, only the first one is as it uses getDesktopProperty("win.frame.captionFont");
second code snippet is not working. I suppose it works only for undecorable LaF
depends what you mean by not working. You won't get the same result as if you put the string in a radio button because the LaF add some spacing on the dialog title (5px in the metal LaF). It is still not clear to me though why you want to find out the width of the text, is it to dynamically set the dialog width?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.