javax.swing.plaf

Class ComponentUI

Known Direct Subclasses:
ButtonUI, ColorChooserUI, ComboBoxUI, DesktopIconUI, DesktopPaneUI, FileChooserUI, InternalFrameUI, LabelUI, ListUI, MenuBarUI, OptionPaneUI, PanelUI, PopupMenuUI, ProgressBarUI, RootPaneUI, ScrollBarUI, ScrollPaneUI, SeparatorUI, SliderUI, SpinnerUI, SplitPaneUI, TabbedPaneUI, TableHeaderUI, TableUI, TextUI, ToolBarUI, ToolTipUI, TreeUI, ViewportUI

public abstract class ComponentUI
extends Object

The abstract base class for all delegates that provide the pluggable look and feel for Swing components. User applications should not need to access this class; it is internal to Swing and the look-and-feel implementations.

[UML diagram illustrating the architecture for pluggable
 look and feels]

Components such as JSlider do not directly implement operations related to the look and feel of the user interface, such as painting or layout. Instead, they use a delegate object for all such tasks. In the case of JSlider, the user interface would be provided by some concrete subclass of SliderUI.

Soon after its creation, a ComponentUI will be sent an installUI(JComponent) message. The ComponentUI will react by setting properties such as the border or the background color of the JComponent for which it provides its services. Soon before the end of its lifecycle, the ComponentUI will receive an uninstallUI(JComponent) message, at which time the ComponentUI is expected to undo any changes.

Note that the ui of a JComponent changes whenever the user switches between look and feels. For example, the ui property of a JSlider could change from an instance of MetalSliderUI to an instance of FooSliderUI. This switch can happen at any time, but it will always be performed from inside the Swing thread.

Constructor Summary

ComponentUI()
Constructs a new UI delegate.

Method Summary

boolean
contains(JComponent c, int x, int y)
Determines whether a click into the component at a specified location is considered as having hit the component.
static ComponentUI
createUI(JComponent c)
Creates a delegate object for the specified component.
Accessible
getAccessibleChild(JComponent c, int i)
Returns the specified accessible child of the component.
int
getAccessibleChildrenCount(JComponent c)
Counts the number of accessible children in the component.
Dimension
getMaximumSize(JComponent c)
Determines the maximum size of a component.
Dimension
getMinimumSize(JComponent c)
Determines the minimum size of a component.
Dimension
getPreferredSize(JComponent c)
Determines the preferred size of a component.
void
installUI(JComponent c)
Sets up the specified component so it conforms the the design guidelines of the implemented look and feel.
void
paint(Graphics g, JComponent c)
Paints the component according to the design guidelines of the look and feel.
void
uninstallUI(JComponent c)
Puts the specified component into the state it had before installUI(JComponent) was called.
void
update(Graphics g, JComponent c)
Fills the specified component with its background color (unless the opaque property is false) before calling paint(Graphics,JComponent).

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

ComponentUI

public ComponentUI()
Constructs a new UI delegate.

Method Details

contains

public boolean contains(JComponent c,
                        int x,
                        int y)
Determines whether a click into the component at a specified location is considered as having hit the component. The default implementation checks whether the point falls into the component’s bounding rectangle. Some subclasses might want to override this, for example in the case of a rounded button.
Parameters:
c - the component for which this delegate performs services.
x - the x coordinate of the point, relative to the local coordinate system of the component. Zero would be be component’s left edge, irrespective of the location inside its parent.
y - the y coordinate of the point, relative to the local coordinate system of the component. Zero would be be component’s top edge, irrespective of the location inside its parent.

createUI

public static ComponentUI createUI(JComponent c)
Creates a delegate object for the specified component. Users should use the createUI method of a suitable subclass. The implementation of ComponentUI always throws an error.
Parameters:
c - the component for which a UI delegate is requested.

getAccessibleChild

public Accessible getAccessibleChild(JComponent c,
                                     int i)
Returns the specified accessible child of the component. The default implementation delegates the inquiry to the AccessibleContext of c.
Parameters:
c - the component whose i-th accessible child is requested.
i - the index of the accessible child, starting at zero.

getAccessibleChildrenCount

public int getAccessibleChildrenCount(JComponent c)
Counts the number of accessible children in the component. The default implementation delegates the inquiry to the AccessibleContext of c.
Parameters:
c - the component whose accessible children are to be counted.

getMaximumSize

public Dimension getMaximumSize(JComponent c)
Determines the maximum size of a component. The default implementation calls getPreferredSize(JComponent), but subclasses might want to override this.
Parameters:
c - the component for which this delegate performs services.
Returns:
the maximum size, or null to indicate that c’s layout manager should be asked to calculate the maximum size.

getMinimumSize

public Dimension getMinimumSize(JComponent c)
Determines the minimum size of a component. The default implementation calls getPreferredSize(JComponent), but subclasses might want to override this.
Parameters:
c - the component for which this delegate performs services.
Returns:
the minimum size, or null to indicate that c’s layout manager should be asked to calculate the minimum size.

getPreferredSize

public Dimension getPreferredSize(JComponent c)
Determines the preferred size of a component. The default implementation returns null, which means that c’s layout manager should be asked to calculate the preferred size.
Parameters:
c - the component for which this delegate performs services.
Returns:
the preferred size, or null to indicate that c’s layout manager should be asked for the preferred size.

installUI

public void installUI(JComponent c)
Sets up the specified component so it conforms the the design guidelines of the implemented look and feel. When the look and feel changes, a ComponentUI delegate is created. The delegate object then receives an installUI message.

This method should perform the following tasks:

  • Set visual properties such as borders, fonts, colors, or icons. However, no change should be performed for those properties whose values have been directly set by the client application. To allow the distinction, LookAndFeels are expected to use values that implement the UIResource marker interface, such as BorderUIResource or ColorUIResource.
  • If necessary, install a LayoutManager.
  • Embed custom sub-components. For instance, the UI delegate for a JSplitPane might install a special component for the divider.
  • Register event listeners.
  • Set up properties related to keyborad navigation, such as mnemonics or focus traversal policies.
Parameters:
c - the component for which this delegate will provide services.

paint

public void paint(Graphics g,
                  JComponent c)
Paints the component according to the design guidelines of the look and feel. Most subclasses will want to override this method.
Parameters:
g - the graphics for painting.
c - the component for which this delegate performs services.

uninstallUI

public void uninstallUI(JComponent c)
Puts the specified component into the state it had before installUI(JComponent) was called.
Parameters:
c - the component for which this delegate has provided services.

update

public void update(Graphics g,
                   JComponent c)
Fills the specified component with its background color (unless the opaque property is false) before calling paint(Graphics,JComponent).

It is unlikely that a subclass needs to override this method. The actual rendering should be performed by the paint(Graphics,JComponent) method.

Parameters:
g - the graphics for painting.
c - the component for which this delegate performs services.

ComponentUI.java -- Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.