Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to set default font for an application

220 views
Skip to first unread message

Greg Jablonski

unread,
Jan 24, 2001, 12:12:51 PM1/24/01
to
Hi all,

I am creating application GUI in Swing but I have trouble with setting
default font for all buttons, labels etc. I tried this:

MetalLookAndFeel metalLook = (MetalLookAndFeel) UIManager.getLookAndFeel();
metalLook.getControlTextFont().deriveFont(Font.PLAIN);

UIManager.setLookAndFeel(metalLook);

but this didn't work. Any suggestions will be helpful.

Thanks
Greg J.

Linda Radecke

unread,
Jan 24, 2001, 2:28:40 PM1/24/01
to

Greg Jablonski escribió:

Hi!

> MetalLookAndFeel metalLook = (MetalLookAndFeel) UIManager.getLookAndFeel();
> metalLook.getControlTextFont().deriveFont(Font.PLAIN);
>
> UIManager.setLookAndFeel(metalLook);

To set the font for all your components you can do that, for instance, as
follows :

UIManager.put("myComponent.font",new Font("Serif",Font.ITALIC,12));


The Metal Look & Feel is already set per default, so you don't
have to determine this explicitely, but you can do that as follows :

try {
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) { }


Linda
--

E wieß Blatt Papier, ne Bleisteff, Jedanke bei dir,
setz ich ahm Finster un hühr, wat sich avvspillt vüür
der Düür, bess ich avvrötsch en die Zigg, en der et
dich für mich nit joov un mir ming Levve vürm Daach
X op einmohl vüürkütt wie en Stroof.
Do kanns zaubere, BAP


Christian Kaufhold

unread,
Jan 24, 2001, 3:45:52 PM1/24/01
to
Linda Radecke <li...@radecke.ch> wrote:

> Greg Jablonski escribió:

>> MetalLookAndFeel metalLook = (MetalLookAndFeel) UIManager.getLookAndFeel();
>> metalLook.getControlTextFont().deriveFont(Font.PLAIN);

That derives a Font and immediately throws it away.

>>
>> UIManager.setLookAndFeel(metalLook);

That is a no-op. The metalLook is already set.

There are no public setters for these properties. I would investigate
how easy it is to write a custom theme and then set that on the LAF
(This is Metal-specific).


Most stuff is hard-coded in the LAFs and cannot be be easily glo-
bally substituted.


> To set the font for all your components you can do that, for instance, as
> follows :

> UIManager.put("myComponent.font",new Font("Serif",Font.ITALIC,12));


> The Metal Look & Feel is already set per default, so you don't
> have to determine this explicitely, but you can do that as follows :

> try {
> UIManager.setLookAndFeel(
> UIManager.getCrossPlatformLookAndFeelClassName());
> } catch (Exception e) { }


I always wonder why you want to hardcode that. Customizing the default
look and feel should rather be done in a property file:

http://java.sun.com/docs/books/tutorial/uiswing/misc/plaf.html

Christian
--
Weck mich uss dämm Alpdraum, saach mir, wo ich benn.
En all dä Labyrinthe blieht mir nur ding Stemm.
Lieb mich, wenn ich hasse, wärm mich, wenn ich frier. BAP,
Et einz'je wat ich wirklich weiß, ess, wo ich hinjehühr. Nöher zu mir

Linda Radecke

unread,
Jan 24, 2001, 5:24:15 PM1/24/01
to

Christian Kaufhold escribió:

> I always wonder why you want to hardcode that. Customizing the default
> look and feel should rather be done in a property file:
>
> http://java.sun.com/docs/books/tutorial/uiswing/misc/plaf.html

Yes, of course, you're right

Linda :-)

Gamma

unread,
Jan 25, 2001, 1:37:49 PM1/25/01
to
Greg Jablonski <gr...@dcit.com> wrote:
>Hi all,
>
>I am creating application GUI in Swing but I have trouble with setting
>default font for all buttons, labels etc.


This is probably not the "right" way to do it, but it appears to work
in the app I'm developing.

The idea here is to work solely off of the UIDefaults. The code takes
everything in the defaults that looks like a font, and replaces it
with the font I chose.


/** Sets many UI fonts to a FontUIResource with the given name,
* style, and point size. */
public static void setUIFont (String name, int style, int size)
{
setUIFont (new FontUIResource(name, style, size));
}

/** Sets all UI fonts to the given FontUIResource. */
public static void setUIFont (FontUIResource f)
{
Enumeration keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = UIManager.get (key);
if (value instanceof FontUIResource)
UIManager.put (key, f);
}
}

--

Paul Brinkley
ga...@clark.net

0 new messages