Notes on Changing Look and Feel
Look and Feel refers generally to things that have
little or no effect on the functionality of the program.
Not to say unimportant--look and feel is very important--just orthogonal.
We can change one without affecting the other.
- Pluggable Look and Feel
- There are two
setLookAndFeel methods. One takes an object, the other a class name.
- You can also get a list of the available Look and Feels.
- MetalLookAndFeel is the cross-platform "Java" Look and Feel.
public static void main is the entry point of any Java app.
This is where we set the Look and Feel--before we create a single component.
- Catalyst Theme
- We have two ways to modify the Metal L&F.
- We can extend it, and override the parts we want to change
or easier still
- We can install a MetalTheme.
- At a minimum, we create our own theme by defining six colors.
- We can also set a few fonts.
- To do more, we need to implement the
- Here's where we override default borders, colors, and fonts on specific component types.
- We can also install our own UI (as we do for ComboBoxUI).
- Some little things (such as menu item accelerator delimiter) can be set here and nowhere else.
- Catalyst Combo Box UI
- We got the buttons we wanted simply by overriding Metal's button border.
- Metal's combo box was so completely wrong, we had no choice but to plug in our own
- We had to add our own arrow buttons, re-work the layout, and add a default list cell renderer.
- This just provides the default renderer--you can still call
setRenderer when you need something different.
- The power you have when you write your own UI is just absolutely awesome.
Full Custom UI
- Writing Custom UIs
- Always assume with any programming problem that at least a hundred other programmers have had the same problem.
- Further assume that at least one of those programmers has written an article bragging about how he solved it.
- Finally assume that the article he wrote is posted on Sun's Java site. All you have to do is find it.
- This fellow Ralph Rar works for SAP and had to create a custom L&F. In this article he weighs the options.
- Borrowing Code
- I read a lot of code. A lot of code.
- Sun provides the source for all the Java libraries. It's packed with the JDK in a zip file.
- Before you start hacking your own solution look at Sun's code. You might get some ideas.
- You may even get the impression you can improve on it. (You probably can.)
- Don't forget to run up the Jolly Roger while you're doing this.
- Oh, man, look at this pathetic code to draw a triangle on the arrow buttons...
(Proof, if you needed it, that Sun uses contractors.)
- Custom by Composition
- When you don't need global changes, you can do a lot just by plugging in to the Swing API.
- Virtually everything about a component can be customized, frequently by composition.
- A combo box is not one object; rather it is composed of many objects, such as borders, lists, editors, and renderers.
- You modify it by swapping out the objects that compose it.
- The distinction between composition and property setting is a fuzzy one.
- Factories for Properties
- When we need a bunch of custom components we naturally think of sub-classing.
- A better option in many cases is a factory method.
- Factory methods don't clutter up the name space.
Also, they tend to be easier to read and modify.
- As always the goal is to avoid duplicating code.
- Extending for Behavior
- Sometimes you have no choice but to extend a component to get the behavior you want.
- Even so, you can do it inside a factory method using an anonymous inner class.
- Summary of L&F Options
- Here are your options for modifying the Look and Feel of your application.
- I list these in order of increasing complexity.
- Coincidentally, they also appear in order
from local (one or two components)
to global (every component in the program).