Setting the Default Font of Swing Program

Setting the Default Font of Swing Program

try:

public static void setUIFont (javax.swing.plaf.FontUIResource f){
java.util.Enumeration keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = UIManager.get (key);
if (value instanceof javax.swing.plaf.FontUIResource)
UIManager.put (key, f);
}
}

Call by ...

setUIFont (new javax.swing.plaf.FontUIResource("Serif",Font.ITALIC,12));

Set default font of swing application once even even if new text is drawn

private JTextArea textArea = new JTextArea();
...
setUIFont(new javax.swing.plaf.FontUIResource("Sans", Font.PLAIN, 24));
...
JButton button = new JButton("test");

You have already created the JTextArea before you change the font.

The Swing component gets its UI properties at the time it is created.

So you need to create the JTextArea AFTER you set the font, the same as you do with the JButton.

Note: if you change the properties after the components have been created then you need to reset the properties.

SwingUtilities.updateComponentTreeUI(frame);
frame.pack();

Read the section from the Swing tutorial on How to Set the Look and Feel After Startup for more information.

How to change the default font size of Java swing components

Adjusting the UIDefaults is one way. Another is to design a pluggable look & feel.

changing default font sizes for Swing controls in Netbeans

I figured out why I'm getting the different sizing. I have a large, widescreen monitor on one of the Win7 computers, and a long time ago I had set the DPI to 120 (125%). This causes Netbeans and Java to run at the different sizes. Setting DPI back to 96 solves the problem. Most notably, anything developed at 96 DPI (on another Win7 pc) will run on this pc at 120 DPI with the edges shortened. End result is the Swing controls at the right and bottom edges are covered by the scrollbars of the application.

I can use the ctrl-scrollwheel trick to enlarge some of the text and icons while using 96DPI, but for someone needing everything shown to be larger, this is just a work-around. At least Netbeans allows a setting for the editor text.

It would be nice if Netbeans and Java would let me set the preferred DPI to develop in and run as. As for my Linux pc, that would be the only answer I can come up with.

Thanks all.

How to change default font in netbeans platform?

type and size of Font are diferrent by platform and used Look and Feel, there are these possibilities

  • GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(font);

and/or with

  • change Keys in UIManager for current JVM instance

or

  • Change Font at runtime

have look at UImanager default by @camickr

Setting the Global Font for a Java Application

Figured it out:

Call with: setUIFont (new javax.swing.plaf.FontUIResource(new Font("MS Mincho",Font.PLAIN, 12)));

private static void setUIFont(javax.swing.plaf.FontUIResource f)
{
java.util.Enumeration<Object> keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements())
{
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof javax.swing.plaf.FontUIResource)
{
UIManager.put(key, f);
}
}
}

How to set the system default font as the selected item of JComboBoxString?

The logical fonts don't seem to be listed among those returned by getAllFonts(). On the other hand, this works.

    ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
combo_fonts = new JComboBox<String>(ge.getAvailableFontFamilyNames());
combo_fonts.setSelectedItem(Font.SANS_SERIF);

How do I change only the size of all the fonts of a swing application?

You can do a small modification to that solution.

public static void setUIFontSize(int newSize) {
Enumeration<Object> keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value != null
&& value instanceof javax.swing.plaf.FontUIResource) {
FontUIResource oldFont = (FontUIResource) value;
UIManager.put(key, oldFont.deriveFont((float) newSize));
}
}
}


Related Topics



Leave a reply



Submit