Pluggable Look and Feel
[Previous] [Main] [Next]

Inheriting from your own JFrame
- Pluggable Look and Feel

One technique that is widely used in Java is using the power of inheritance with Java components. Each of the components inherits considerable functionality from classes above it starting with the Object class. One place this technique is not discussed much involves standard components like JFrames and JButtons. If you instantiate these components directly into your classes you have no single point where you can make changes that affect all copies of a components. The solution involves adding a layer of classes between your code and Java's base classes. For example, if all the buttons in your program inherit from JxButton which inherits from JButton, then you have a single place to add functionality or change colors or to do whatever you want with buttons. A change to JxButton would be reflected in ALL of the your program.

This technique has been used here. Part of the "standard" set of classes being used by JGuiGen is a JxFrame class. This class has the code in it to look at the current system, decide what operating system is being used, check for the existence of the appropriate look and feel and use it if it is available. The code is here. JxFrame.html Each class created by JGuiGen is a panel. Each class is runnable and has a main method. If you look at the main method, you will see that it creates a Frame using JxFrame and then inserts the panel into it.

Each class created by JGuiGen has a popup menu. One of the options on this menu allows the user to change their look and feel. The system will determine which look-and-feels are installed on the user's PC and added these to the menu. A LowVisionMetalLookAndFeel (high contrast) is added as part of the system and when the user picks a new look and feel the look of the panel is changed. The Java Preferences system is used to save this setting so that when the user starts the appliction again their preferred look and feel is still in place.

Here is the code for switching the look and feel:

Here is the code to save the restore the user's preferences.