20

Are there any built-in methods in Java to increase Font size?

jjnguy
  • 136,852
  • 53
  • 295
  • 323

6 Answers6

19

The Font class allows you to specify font size.

So, to create a font you do something like this:

Font f = new Font("serif", Font.PLAIN, fontSize);

The fontSize parameter will determine the size of your Font.

You can't actually change the size of an existing Font object. The best way to achieve a similar effect is to use the deriveFont(size) method to create a new almost identical Font that is a different size.

Font biggerFont = existingFont.deriveFont(bigNumber);
jjnguy
  • 136,852
  • 53
  • 295
  • 323
  • 4
    Note: you need to specify that `bigNumber` is a float, otherwise you'll trigger the `deriveFont(int style)` overload. I.e. `existingFont.deriveFont(100f);`. – Duncan Jones Aug 11 '16 at 12:30
15

You can derive a new Font with a different size by using the following:

Font original = // some font
Font bigger = original.deriveFont(newSize);

Where newSize is a float, not an int. This is well documented in the JavaDoc for Font as other people have pointed out

tddmonkey
  • 20,798
  • 10
  • 58
  • 67
  • 2
    +1. @Raji: In addition, if you want to increase the font size on the GUI components this way you can do it while retaining the font set and format. For example: myLabel.setFont(myLabel.getFont().deriveFont(20)); You can then implement a recursive algorithm which performs this operation on the entire component hierarchy. Just an idea. – akarnokd Jun 25 '09 at 14:08
  • 3
    newSize _must_ be a `float`, not an `int`. See [deriveFont(float)](http://docs.oracle.com/javase/6/docs/api/java/awt/Font.html#deriveFont(float)) and answer from Avrom. [deriveFont(int)](http://docs.oracle.com/javase/6/docs/api/java/awt/Font.html#deriveFont(int)) applies a _style_ (encoded as an `int` value) to the font. – Eugen Labun Mar 06 '12 at 14:14
13

Assuming that you want to change the font size on a specific JLabel, you can do:

label.setFont(label.getFont().deriveFont(newSize));

Make sure that newSize is a float not an int.

Avrom
  • 4,967
  • 2
  • 28
  • 35
11

I interpreted this question as "How can I increase font size for Swing across the board." I'm not aware of any built-in way to do this, but you could do it yourself by modifying the values in the UIManager class on startup before you create any Swing components.

I do this by having a parameter passed into my app that I use as a multiplier. If I pass in 150 it'll multiply all existing fonts by 150%. The code is as follows

public static void initializeFontSize() {
    String fontSizeParam = System.getProperty("myapp.fontSize");
    if (fontSizeParam != null) {
        float multiplier = Integer.parseInt(fontSizeParam) / 100.0f;
        UIDefaults defaults = UIManager.getDefaults();
        int i = 0;
        for (Enumeration e = defaults.keys(); e.hasMoreElements(); i++) {
            Object key = e.nextElement();
            Object value = defaults.get(key);
            if (value instanceof Font) {
                Font font = (Font) value;
                int newSize = Math.round(font.getSize() * multiplier);
                if (value instanceof FontUIResource) {
                    defaults.put(key, new FontUIResource(font.getName(), font.getStyle(), newSize));
                } else {
                    defaults.put(key, new Font(font.getName(), font.getStyle(), newSize));
                }
            }
        }
    }
}
CarlG
  • 1,656
  • 1
  • 17
  • 21
6

you can set the property swing.plaf.metal.controlFont when running you application:

java -Dswing.plaf.metal.controlFont=Dialog-50 YourMainClass

in this example, you set the default font to be "Dialog" with size 50.

cd1
  • 15,908
  • 12
  • 46
  • 47
3

The question is way too vague to give a good answer. But I think you want to systematically increase font size in your application.

The font face, style and size in a Java Swing application is controlled via the LookAndFeel mechanism. You need to change the font in the look-and-feel if you want the change to apply to all Swing components of a given type.

Have a look at the UIManager example.

Here's how to change the font globally for some UI components:

UIManager.put("Label.font", new FontUIResource(new Font("Dialog", Font.PLAIN, 10)));
UIManager.put("Button.font", new FontUIResource(new Font("Dialog", Font.BOLD, 10)));
UIManager.put("TextField.font", new FontUIResource(new Font("Dialog", Font.PLAIN, 10)));
Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
rustyx
  • 80,671
  • 25
  • 200
  • 267