4

Is there a way to override just a single UI component's L&F (not a single instance of it, but all instances) with a custom one? Basically I want to override the L&F defaults for it.

It looks like all of that gets setup in BasicLookAndFeel#getDefaults

I guess if I really needed to I could create a custom look and feel that extends the one I want to use and override it in the initClassDefaults method, but I would prefer to just be able to override whatever UI is currently being used.

I was thinking I could just do UIManager.put(<ui_class_id>,<my-delegate-classname>)

This doesn't seem to work for the example I'm working on, but that could be because I'm trying to set a component that's not part of the standard look and feel (the component adds itself on). I'm trying to figure out if this is even the right approach.

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
  • 1
    only my curiosity, please why, why to override [UIDefaults](http://tips4java.wordpress.com/2008/10/09/uimanager-defaults/) – mKorbel Feb 28 '12 at 19:46
  • I'm using a 3rd party component that installs its own ui delegates. I want to customize how it looks so I'm trying to override the default ui used for that 3rd party component. – Jeff Storey Feb 28 '12 at 19:52

3 Answers3

2

Ah, the problem was that I forgot to implement createUI in my UI. Once I did that, I was able to just call UIManager.put(<ui_class_id>,<my-delegate-classname>) and it worked.

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
1

Here is the solution to change a L&F for a component

JFileChooser fc = new JFileChooser();
WindowsFileChooserUI wui = new WindowsFileChooserUI(fc);
wui.installUI(fc);

You can find all existing component class UI From

com.sun.java.swing.plaf.(L&F).*;
1

From the Java Tutorials:

Changing the Look and Feel After Startup

You can change the L&F with setLookAndFeel even after the program's GUI is visible. To make existing components reflect the new L&F, invoke the SwingUtilities updateComponentTreeUI method once per top-level container. Then you might wish to resize each top-level container to reflect the new sizes of its contained components. For example:

UIManager.setLookAndFeel(lnfName);
SwingUtilities.updateComponentTreeUI(frame);
frame.pack();

In other words: you can change the look and feel of a top-level container (I don't have tested if it is limited to top-level containers only or if this method can be applied to any container). Or, to answer your question even more precisely: I don't see there's method of styling a single component, but there is a way for single components in (toplevel) containers.

f4lco
  • 3,728
  • 5
  • 28
  • 53