Skip to main content

Apply Font to a JFrame and all Component ???

5 replies [Last post]
m.h.shams
Offline
Joined: 2006-02-17

HI all

Can i set a Font to a JFrame or JPanel and All Componet
that added to these ?

something like applyComponentOrientation that apply
orientaion to all component in a container.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
navinkjha
Offline
Joined: 2004-12-28

Shannon,
What is the best way to do this ? Also if a user requests a font change, is there an easy way to do this without closing the application ?
-Navin

zander
Offline
Joined: 2003-06-13

> Shannon,
> What is the best way to do this ? Also if a user
> requests a font change, is there an easy way to do
> this without closing the application ?

If you use the config dialog supplied with the L&F from http://uic.sf.net you'll find its as easy as pressing apply in that dialog.
Start the demo on the homepage (javawebstartable) which has a configuration option to see it yourself.

The trick is to apply the L&F to the window _after_ you made sure the L&F will return the right font. like this:
[code]
javax.swing.SwingUtilities.updateComponentTreeUI(frame);[/code]

You may ask how to change the font for the L&F; easiest way is to use the config dialog described above :)

insider
Offline
Joined: 2003-10-26

Hello Mohammad

I dont know a simple way how to set a font for particular JPanel and all its children

But there is a way to set a font for all Swing components anywhere in your application
[code]
import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;

public class TestFont {
public static void main(String[] args) {

setUIFont(new FontUIResource("Serif", Font.ITALIC, 30));
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel label = new JLabel("Test");

panel.add(label);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}

public static void setUIFont(FontUIResource f) {
//
// sets the default font for all Swing components.
// ex.
// setUIFont (new javax.swing.plaf.FontUIResource("Serif",Font.ITALIC,12));
//
java.util.Enumeration keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof javax.swing.plaf.FontUIResource)
UIManager.put(key, f);
}
}
}
[/code]
the key method is
public static void setUIFont(FontUIResource f)

you can insert it in you code and use it when your application starts

if you need to change font for particular components
you can use just

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

check a complete set of UI defaults
here http://www.rgagnon.com/javadetails/JavaUIDefaults.txt

Hope this helps

Alex

Message was edited by: shan-man (formatted code)

shan-man
Offline
Joined: 2006-02-17

Hi,

I just want to point out that iterating through all of the UI resources like this may not be an ideal thing to do. Some resources may be loaded lazily by Swing to enhance performance, and forcing them to be fetched defeats this.

Regards,
Shannon

m.h.shams
Offline
Joined: 2006-02-17

Hi Shannon

thanks for your kindly helps and guides.

i have a question too,

it is better to use Constants for this UI Defaults.
am i right?
finding this defaults isn't easy for everyone.

when i looking through swing sources. i see a lot of
hard coded string like this defaults and many others.

i think these are anti pattern in programming.

please write me what do you think about these .

Regards
Mohammad