It's not a secret anymore!*
The moose likes Swing / AWT / SWT and the fly likes Default Font Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Default Font" Watch "Default Font" New topic
Author

Default Font

Helmut Lerch
Ranch Hand

Joined: Feb 11, 2001
Posts: 48
Hy all,

suppose the following Code:
<pre>
import java.awt.*;
public class TestFonts {
public static void main (String[] args) {
Frame f = new Frame();
Button bt = new Button();
f.add(bt);
System.out.println(bt.getFont());
}
}
</pre>
The Output is "null".



My Questions:

1.) When is a Font assigned to a Component if we don't use setFont(Font f)?

2.) Which "default" Font is used by a Component if we don't assign a Font to a Component by using setFont(Font f)? (And how can we get it?)

Thanks for help.
MSD
Greenhorn

Joined: Jan 22, 2000
Posts: 20
Hi,
You are right. The output is null.
A font is assigned to a component when it first becomes visible. Since you never made the button visible, the font returned was null. Just add the following before System.out.println and it should work :

The default font for a component is picked up from system. On mine it is "dialog" (ie san serif), styl plain, size 12.
Hope this helps,
Mohammed
Originally posted by Helmut Lerch:
Hy all,

suppose the following Code:
<pre>
import java.awt.*;
public class TestFonts {
public static void main (String[] args) {
Frame f = new Frame();
Button bt = new Button();
f.add(bt);
System.out.println(bt.getFont());
}
}
</pre>
The Output is "null".



My Questions:

1.) When is a Font assigned to a Component if we don't use setFont(Font f)?

2.) Which "default" Font is used by a Component if we don't assign a Font to a Component by using setFont(Font f)? (And how can we get it?)

Thanks for help.

Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

not to be picky but,
when you don't use setFont() on a component, the component
will inherit the font of its parent. Eventually, we may endup
picking it up from the default system font.
regds.
- satya

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Helmut Lerch
Ranch Hand

Joined: Feb 11, 2001
Posts: 48
Thanks for replying.
Originally posted by Madhav Lakkapragada:
Eventually, we may endup
picking it up from the default system font.
- satya[/B]

And where can I get this font before setVisible(true)?
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Default Font
 
Similar Threads
rowWeights and columnWeights
Font problem
How to set font type and color in a text field?
Does this piece of code hang for you all also ?
I think this qstn from Jxam is suitable for Errata.