The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

This topic is READ ONLY
import-bot (20211) [Avatar] Offline
[Originally posted by bbelian]

I have not been able to figure out an easy way to set/change the
font used for all the JLabels and JTextFields in my applet.
Is there a simple way to do this is one place, or do I have
to add a setFont call to each component in my user interface?
I have also considered creating a new class for each component
type (e.g., PLabel extends JLabel) and setting the font there,
and then use the PLabel class everywhere I was using JLabel.
import-bot (20211) [Avatar] Offline
Re: Changing fonts throughout an applet
[Originally posted by scdrye]

The easiest way to do this is to change the font associated with the component
in the UIDefaults table, and then call SwingUtilities.updateComponentTreeUI().

Font f = new Font(blah);

UIManager.getDefaults().put("Label.font", f);


That should do the trick.
import-bot (20211) [Avatar] Offline
Re: Changing fonts throughout an applet
[Originally posted by bbelian]

That worked fine - thanks!

Other readers may be interested in the "rest of the story". Once I
implemented the suggestion for labels, I wanted to do it for other
user interface objects with text on them such as JTextFields,
JRadioButtons, etc. The problem was that I did not know what key
value to use. For example, if "Label.font" is the key for JLabel
fonts, then what key should be used for JRadioButtons?

My solution was to print out all of the keys there are and their
initial values. The following code will accomplish that:

UIDefaults uid = UIManager.getDefaults();
Enumeration keyList = uid.elements();
Enumeration elementList = uid.keys();
while ( keyList.hasMoreElements() ) {
String element = elementList.nextElement().toString();
String key = keyList.nextElement().toString();
System.out.println(element + " " + key);

There are other ways to format it and make it prettier, but you get
the idea. Once you have the list, it is fairly easy to find the
appropriate key, especially if you write the output to a file instead
of the console and then alphabetize. When I did this, nearly 300 items
were listed. This gives you a great deal of control over the appearance
of you applet and allows you to put the "cosmetic" code in one place
rather than having to embed it throughout many files.