![]() |
|||
![]() ![]() |
![]() |
|
![]() |
Pluggable Look-and-Feel SupportThe JComponent class is where the pluggable look-and-feel support is located. The JComponent class defines the methods that subclasses must override if they support the pluggable look-and-feel architecture. The UIManager uses these methods to determine the user interface class for instances. They are typically used when an instance is created to initialize the user interface and when the look-and-feel changes for the application. The UIManager is discussed in Chapter 30, Pluggable Look-and-Feel. The getUIClassID method returns the String class name for the look-and-feel class for the component. This String is actually a key into the look-and-feel user interface table contained in the UIManager. The value associated with this key will determine the user interface class for the component. A complete reference to how the user-interface object is created for a component is presented in Part V of this book. When the components look-and-feel needs changing, the public updateUI method is called. This method must query the UIManager for the current user interface for the class and then set the user interface for itself. The updateUI method is nearly identical in all JComponent sub- classes. A typical example, from the JLabel class, is shown next. The update method is required to allow a type-safe setUI method. public void updateUI() { setUI((LabelUI)UIManager.getUI(this)); invalidate(); } Subclasses of JComponent override the setUI method and change the user interface parameter to the specific type required for that component. The subclass then calls its parents setUI method. This allows the setUI method to be type safe while still allowing the functionality to be encapsulated in the JComponent class. The following is the setUI method in the JLabel class: public void setUI(LabelUI ui) { super.setUI(ui); } The setUI method contained in the JComponent class is shown next. It takes a ComponentUI instance as a parameter. The other user interface classes must extend this class for the user interface methods in the JFC to work. This method un-installs the current user interface and then installs the new user interface. As we saw in the previous section, the user interface is a bound property. protected void setUI(ComponentUI newUI) { /* We do not check that the UI instance is different * before allowing the switch in order to enable the * same UI instance *with different default settings* * to be installed. */ if (ui != null) { ui.uninstallUI(this); } ComponentUI oldUI = ui; ui = newUI; if (ui != null) { ui.installUI(this); } invalidate(); firePropertyChange(UI, oldUI, ui); } These methods will be demonstrated further in Part V of this book. Miscellaneous FeaturesUntil now, this chapter has presented major functional units contained in the JComponent class. The JComponent class also contains some smaller units of functionality that are explored in this section. Accessibility SupportThe JComponent class contains an accessibleContext property. This property can be queried with the getAccessibleContext method. The JComponent class provides the framework for supporting accessibility but does not implement the functionality. The JComponent class cannot do this; it doesnt have the required information to do so. Instead, subclasses provide the information and work in the framework defined in JComponent. Also, JComponent doesnt implement the Accessible interface. Subclasses that add the required knowledge to allow the accessibleContext defined in JComponent to have meaning should implement this interface. Accessibility is discussed further in Chapter 33, Accessibility. Parent ChangesThe JComponent class overrides the addNotify and removeNotify methods contained in the java.awt.Component class. These methods are called when the parent of a JComponent subclass is set or removed for the instance. The parent class methods are still called, but both these methods fire a property change event with the name of ancestor. Thus, a bound property named ancestor has been added for JFC components that are fired when the familiar addNotify and removeNotify methods are called. This property is to be moved into the Component class in a future version of the JDK.
|
![]() |
|