File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Using UIDefaults to configure L&F Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Using UIDefaults to configure L&F" Watch "Using UIDefaults to configure L&F" New topic

Using UIDefaults to configure L&F

Terence Doyle
Ranch Hand

Joined: May 30, 2001
Posts: 328
I'm trying to give a few Font & Color options to users of an application and I thought changing the UIDefaults contents might work.

And it does, to some extent. If I replace a property for say the ToolTip.background while the application is running it works fine. As does changing the Table.selectionBackground.
But changing the Label.font does not change the Labels already painted.
I presume this is because the tooltip and table selection properties are created "on-the-fly" from the UIDefaults for each new event. The Label however doesn't change because no event has caused it to recreate itself. (I've tried using repaint() and revalidate() but no luck ).
Has anyone found a way around this?
NEP - edited post to make long code line wrap.
[ January 15, 2002: Message edited by: Nathan Pruett ]

Raising Flares debut album 'Ignition' out now

Terry Doyle <br />SCPJ 1.4 , SCWCD , SCMAD(Beta)
Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
you are right Terry, we just cant change on the fly , but I tried creating a new UIResource object for Font & set it explicitly to the components whose Label's font I wanted to change, it worked then. Heres the code, I hope this solves ur problem:
FontUIResource fontChange = new FontUIResource(new Font("MS Sans Serif",Font.ITALIC,11));
& then :
//some button creation code ...
if ((button.getFont() instanceof UIResource))
button.setFont(UIManager.getFont("ToolTip.font"));//this works!
I guess components have the ComponentUI classes's methods called when they are painted ,so thats y it gets the DefaultUI , in this case for a button.

- Vinod<br />-------<br />SCJP2
I agree. Here's the link:
subject: Using UIDefaults to configure L&F
It's not a secret anymore!