Take the 2-minute tour ×

In my Java Swing application, I have buttons. If the screen size is changed, I want to change the font of my button text also.

How do I implement it?

share|improve this question
what do you mean by "screen size"? Mine is fixed, looking at it :-) –  kleopatra Nov 18 '11 at 10:58
By Screen size, I mean user can resize the screen. If he changes teh screen size then the font of buttons should be changed automatically. –  Tvd Nov 18 '11 at 11:31
still don't understand: do you have futuristic hardware or do you mean the JFrame that's showing your application on the screen? –  kleopatra Nov 18 '11 at 11:38
@kleopatra I mean the JFrame that's showing. If the window is resized, I want to change the font size. Whats the best approach to change font based on what settings it should be done. How to calculate the font size based on screen size. Like if the screen is 200*200 then 14 size is good. If the screen turns 450*450 then how should the font size be calcuated and where should be added to set the font for all componetns. –  Tvd Nov 18 '11 at 11:53
duplicated? stackoverflow.com/questions/8183949/… –  kleopatra Nov 18 '11 at 14:33

3 Answers 3

I believe that you need a JFrame window listener and simply change the button attributes there:

//global JButton button1 defined earlier
button1.setFont(new Font("<FONT NAME>", <FONT STYLE>, <FONT SIZE>))
share|improve this answer
random guessing and inventing? a) there is no such thing as a "JFrame listener" b) you wouldn't create a Font unrelated to the original –  kleopatra Nov 18 '11 at 10:57
sorry, missed the window part –  shookees Nov 18 '11 at 11:00

GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds() ?

share|improve this answer
Where to implement this and how –  Tvd Nov 18 '11 at 11:43
Tvd, it is already part of the Java API (java.awt.GraphicsEnvironment). –  DejanLekic Nov 20 '11 at 17:43
up vote 0 down vote accepted

Thanks to all,

Based on listeners and frame size, border size and actual panel size I have figured font sizes for various screen size ranges.

share|improve this answer

Your Answer


