Re: How to define a default font for JLabel or whole program?



On 15-4-2005 15:28, Chris wrote:

I don't really like how Java2 default font is bold.  I think the plain
looks much better.

How can I set the default font for a JLabel or for the whole program.
I would like to change it from BOLD to PLAIN.

About half of my JLabels in my program are anonymous.  It would quite a
pain to go and define a bunch of new JLabels, then change the font to
plain.

Use UIManager to define JLabel's default font

import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;

public class LabelFont {

   public static void main(String[] args) {
      Font oldLabelFont = UIManager.getFont("Label.font");
      UIManager.put("Label.font", oldLabelFont.deriveFont(Font.PLAIN));

      JFrame f = new JFrame("LabelFont Test");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.getContentPane().setLayout(new FlowLayout());

      JLabel df = new JLabel("Default JLabel font");
      f.getContentPane().add(df);

      JLabel ef = new JLabel("Font explicitly set");
      ef.setFont(oldLabelFont);
      f.getContentPane().add(ef);

      f.pack();
      f.setVisible(true);
   }
}

--
Regards,

Roland de Ruiter
  ___      ___
 /__/ w_/ /__/
/  \ /_/ /  \
.