1

I have to write app, which can scale font's size of label and buttons depending on size of entire frame. My idea is to increase size of font until whole text fits into label or button.

How can I check is all string fit to button/label?

Tetsujin no Oni
  • 7,300
  • 2
  • 29
  • 46
It'sMe
  • 125
  • 1
  • 15
  • Why AWT (e.g. `Label`) rather than Swing (e.g. `JLabel`)? – Andrew Thompson Nov 29 '12 at 22:34
  • *"I have to write app, which can scale font's size of label and buttons depending on size of entire frame"* If that were a ***good idea*** it probably would have been already done in Chrome, and FF, and web sites that appear in them, and Word, and the iTunes software, and.. – Andrew Thompson Nov 29 '12 at 22:37

1 Answers1

0

Something like the following

JComponent c = ... // it can be label, button or any other component
FontMetrics fm = c.getFontMetrics(c.getFont()); // or another font
int strw = fm.stringWidth("My text");

I took this example from Getting string size in java (without having a Graphics object available)

Community
  • 1
  • 1
AlexR
  • 114,158
  • 16
  • 130
  • 208