Reputation:
Are there any built-in methods in Java to increase Font size?
Upvotes: 19
Views: 75195
Reputation: 1
Here are some changes to the initializeFontSize
method from a previous answer. I calculate a positive or negative amount and rescale the font size.
private void initializeFontSize(int fontSizeDiffValue, UIDefaults defaults) {
if(fontSizeDiffValue == 0) return;
for (Enumeration<Object> e = defaults.keys(); e.hasMoreElements();) {
Object key = e.nextElement();
Font font = UIManager.getFont(key);
if(font != null) {
int newSize = font.getSize() + fontSizeDiffValue;
UIManager.put(key, new FontUIResource(new Font(font.getName(), font.getStyle(), newSize)));
}
}
}
Upvotes: 0
Reputation: 85541
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)));
Upvotes: 3
Reputation: 21184
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
Upvotes: 15
Reputation: 1666
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));
}
}
}
}
}
Upvotes: 12
Reputation: 16534
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.
Upvotes: 7
Reputation: 5027
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
.
Upvotes: 13
Reputation: 138982
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);
Upvotes: 19