★ wanayoo — archive 1999 http://developer.java.sun.com/developer/qow/archive/17/index.htmlNouvelle recherche | Portail wanayoo
Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Question of the Week

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
 

Question of the Week No. 17
Researched by Calvin Austin


Question of the Week presents answers to key questions posed by the developer community. The intent is to pass this important, but not always easy-to-find information on to JDC members. The questions are selected from the JDC newsgroups generally because: they are frequently asked, they are significant or timely, or their answers are not easily accessible.

Note: If you have a question to which you need an answer, try the JDC newsgroups. You can read through the existing newsgroups, or with your free JDC membership, you can post new messages or threads.

Question of the Week Archive

Topic: Changing fonts in Swing.

Question: How do I change a Swing font across my whole application?

I would like to change the font for all panels, labels, menus, and so on, in my Swing application. I have tried to change the font using setFont as follows:

frame.getContentPane().setFont( sysfont );

However, the original default font continues to be used. Is there a way to do this in Swing or AWT?

Answer:

Unless you want to define your own 'look and feel,' you should update the user default font properties via the keys you see from: UIManager.getDefaults.

There is no magic way to alter panels, labels, menus, tooltips, and so on, with one statement, because they must all be set separately. For instance, the keyname for Panel is Panel.font, and the keyname for ToolTip is ToolTip.font, but that pattern breaks down for some Components, such as MenuItem.acceleratorFont.

Once you have the keyname, you should be able to make your 'global' change by using the put method of the UIManager class.

For example:

UIManager.put("ToolTip.font",new Font("SansSerif",Font.ITALIC,10));

changes the font for all tooltips. And

UIManager.put("Label.font",new Font("SansSerif",Font.ITALIC,10));

will change the font for all labels.


Many thanks to JDC members, rwiditz, and rockhopper for contributing to this answer.



Sat Nov 14 10:05:30 PST 1998
ananthr
Inside the actionListener for a button called
"Add one more textfield", I wrote code to
add a new JTextField, but it is not showing
up immediately unless I resize the screen. Is ther
an explanation and how would I correct it?
Thanks

Sat Nov 14 11:19:50 PST 1998
ananthr
Earlier on in the code, I had added a few
JTextFields to a frame, and later on, I added
the following code, which doesn't work:

JTextField txtfield=(JTextField)getComponent(3);
txtfield.setText("You clicked the button");
//txtfield.setText("You clicked the button");

Mon Nov 16 08:25:39 PST 1998
Anonymous
How do I tell the XML Parser to ignore whitespace and still be able to use "XMLDocumentBuilder.createDocument(..)" which is a static function?

Mon Nov 16 23:20:02 PST 1998
xiaoming_yang
How can I get a connection to oracle 7.3 for NT with the JDBC in Appplet.


Tue Nov 17 02:40:02 PST 1998
106065.432
Maybe it's a good idea to use a global Font variable, if all fonts point to that variable then all off these fonts are updated. if they do not update automaticaly you should make a new class that performs an update for the mentioned compnents

Tue Nov 17 06:22:38 PST 1998
AkroGirl
Is there possible to use ProgressBars when developing in
JDK 1.1.6 ? And is it possible to connect it to
a database so he checks how long time there is left?
I´m very greatful for your help!!

regards //Karin Johansson

Wed Nov 18 13:14:24 PST 1998
gurucharan
Hello there!!!

I love JAVA ... and this makes my luv even more
better. :)
thanks,
gurucharan.

Wed Nov 18 19:35:12 PST 1998
randy.schrickel
Sure would be nice if there was a reasonable way
to do this! The property names only seem to be
documented/mentioned in the Swing source code.
Would love to have the flexibility of X Windows
resource file for tasks like this.

Thu Nov 19 02:01:21 PST 1998
nisn
Good one!

Thu Nov 19 08:38:13 PST 1998
cargo
This works well in alot of situations - is it not
complete for a container hierarchy?

public static void changeFont(Component cmp, Font font) {
cmp.setFont(font);
if (cmp instanceof Container) {
Component[] kids = ((Container)cmp).getComponents();
for (int i = 0 ; i < kids.length ; i++) {
changeFont(kids[i], font);
}
}
return;
}

Thu Nov 19 11:27:00 PST 1998
rwiditz
In addition to fonts, this technique also works well for L&F colors...

Thu Nov 19 17:25:44 PST 1998
doguntack
³­ Çѱ¹»ç¶÷ÀÔ´Ï´Ù. ¿µ¾î¶ó¼­ ¹» ¸»ÇÏ´ÂÁö
¸ð¸£°Ú³»¿ä. Çѱ¹¸»·Î º¸³»ÁÙ ¼ö ¾ø³ª¿ä?
ÀúÀÇ À̸ÞÀÏÀº doguntack@hanmail.net ÀÔ´Ï´Ù.

Thu Nov 19 19:22:46 PST 1998
anven
answer is very clear.

Fri Nov 20 01:24:13 PST 1998
CJanssen
Ok, but how do I put this in effect for existing Components? A call to JPanel.UIupdate() or JButton.UIupdate() for example shows no reaction.


THE INFORMATION PROVIDED IN THE JAVA DEVELOPER'S CONNECTION IS PROVIDED 'AS IS' WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR, AND NON-INFRINGEMENT.

THIS INFORMATION IS PROVIDED AT NO CHARGE. BY USE OF THE INFORMATION YOU AGREE THAT NEITHER SUN MICROSYSTEMS, INC. NOR ITS LICENSORS WILL BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF SUN OR ITS LICENSORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

THIS INFORMATION COULD INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS. CHANGES ARE PERIODICALLY ADDED TO THE INFORMATION HEREIN. SUN MICROSYSTEMS, INC. MAY MAKE IMPROVEMENTS AND OTHER CHANGES IN THE PRODUCTS AND THE PROGRAMS DESCRIBED HEREIN AT ANY TIME WITHOUT NOTICE.


[ This page was updated: 12-Jan-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.