javax.swing

Class JComponent

public abstract class JComponent extends Container implements Serializable

The base class of all Swing components. It contains generic methods to manage events, properties and sizes. Actual drawing of the component is channeled to a look-and-feel class that is implemented elsewhere.
Nested Class Summary
abstract classJComponent.AccessibleJComponent
Basic accessibility support for JComponent derived widgets.
Field Summary
protected AccessibleContextaccessibleContext
The accessible context of this JComponent.
protected EventListenerListlistenerList
Listeners for events other than {@link PropertyChangeEvent} are handled by this listener list.
static StringTOOL_TIP_TEXT_KEY
protected ComponentUIui
The user interface delegate for this component.
static intUNDEFINED_CONDITION
Constant used to indicate that no condition has been assigned to a particular action.
static intWHEN_ANCESTOR_OF_FOCUSED_COMPONENT
Constant used to indicate that an action should be performed only when the component is an ancestor of the component which has focus.
static intWHEN_FOCUSED
Constant used to indicate that an action should be performed only when the component has focus.
static intWHEN_IN_FOCUSED_WINDOW
Constant used to indicate that an action should be performed only when the component is in the window which has focus.
Constructor Summary
JComponent()
Creates a new JComponent instance.
Method Summary
voidaddAncestorListener(AncestorListener listener)
Register an AncestorListener.
voidaddNotify()
Receives notification if this component is added to a parent component.
voidaddVetoableChangeListener(VetoableChangeListener listener)
Register a VetoableChangeListener.
voidcomputeVisibleRect(Rectangle rect)
Compute the component's visible rectangle, which is defined recursively as either the component's bounds, if it has no parent, or the intersection of the component's bounds with the visible rectangle of its parent.
booleancontains(int x, int y)
Returns true if the coordinates (x, y) lie within the bounds of this component and false otherwise. x and y are relative to the coordinate space of the component.
JToolTipcreateToolTip()
Return the toolTip property of this component, creating it and setting it if it is currently null.
voiddisable()
Disables this component.
voidenable()
Enables this component.
voidfirePropertyChange(String property, int oldValue, int newValue)
Fires a property change for a primitive integer property.
voidfirePropertyChange(String property, boolean oldValue, boolean newValue)
Fires a property change for a primitive boolean property.
protected voidfireVetoableChange(String propertyName, Object oldValue, Object newValue)
Call {@link VetoableChangeListener#vetoableChange} on all listeners registered to listen to a given property.
AccessibleContextgetAccessibleContext()
Get the value of the accessibleContext property for this component.
ActionListenergetActionForKeyStroke(KeyStroke ks)
Get the ActionListener (typically an {@link Action} object) which is associated with a particular keystroke.
ActionMapgetActionMap()
floatgetAlignmentX()
Get the value of the {@link #alignmentX} property.
floatgetAlignmentY()
Get the value of the {@link #alignmentY} property.
AncestorListener[]getAncestorListeners()
Return all registered AncestorListener objects.
booleangetAutoscrolls()
Get the current value of the {@link #autoscrolls} property.
BordergetBorder()
Get the value of the {@link #border} property.
RectanglegetBounds(Rectangle rv)
Get the component's current bounding box.
ObjectgetClientProperty(Object key)
Get a client property associated with this component and a particular key.
protected GraphicsgetComponentGraphics(Graphics g)
Prepares a graphics context for painting this object.
JPopupMenugetComponentPopupMenu()
Returns the popup menu for this component.
intgetConditionForKeyStroke(KeyStroke ks)
Return the condition that determines whether a registered action occurs in response to the specified keystroke.
intgetDebugGraphicsOptions()
Get the value of the {@link #debugGraphicsOptions} property.
static LocalegetDefaultLocale()
Returns the locale used as the default for all new components.
GraphicsgetGraphics()
Returns the Graphics context for this component.
intgetHeight()
Returns the height of this component.
booleangetInheritsPopupMenu()
Returns the flag that controls whether or not the component inherits its parent's popup menu when no popup menu is specified for this component.
InputMapgetInputMap(int condition)
Returns the input map associated with this component for the given state/condition.
InputMapgetInputMap()
Returns the input map associated with this component for the {@link #WHEN_FOCUSED} state.
InputVerifiergetInputVerifier()
Returns the currently set input verifier for this component.
InsetsgetInsets()
Get the component's insets, which are calculated from the {@link #border} property.
InsetsgetInsets(Insets insets)
Get the component's insets, which are calculated from the {@link #border} property.
<T extends EventListener> T[]getListeners(Class<T> listenerType)
Returns all registered {@link EventListener}s of the given listenerType.
PointgetLocation(Point rv)
Get the component's location.
DimensiongetMaximumSize()
Get the component's maximum size.
DimensiongetMinimumSize()
Get the component's minimum size.
ComponentgetNextFocusableComponent()
Return the value of the nextFocusableComponent property.
DimensiongetPreferredSize()
Get the component's preferred size.
KeyStroke[]getRegisteredKeyStrokes()
Return the set of {@link KeyStroke} objects which are registered to initiate actions on this component.
JRootPanegetRootPane()
Returns the first ancestor of this component which is a {@link JRootPane}.
DimensiongetSize(Dimension rv)
Get the component's size.
PointgetToolTipLocation(MouseEvent event)
Return the location at which the toolTipText property should be displayed, when triggered by a particular mouse event.
StringgetToolTipText()
Returns the current tooltip text for this component, or null if none has been set.
StringgetToolTipText(MouseEvent event)
Returns the tooltip text for this component for a particular mouse event.
ContainergetTopLevelAncestor()
Return the top level ancestral container (usually a {@link java.awt.Window} or {@link java.applet.Applet}) which this component is contained within, or null if no ancestors exist.
TransferHandlergetTransferHandler()
Get the value of the {@link #transferHandler} property.
StringgetUIClassID()
Get the value of the UIClassID property.
booleangetVerifyInputWhenFocusTarget()
VetoableChangeListener[]getVetoableChangeListeners()
Return all registered VetoableChangeListener objects.
RectanglegetVisibleRect()
Return the component's visible rectangle in a new {@link Rectangle}, rather than via a return slot.
intgetWidth()
Returns the width of this component.
intgetX()
Returns the X coordinate of the upper left corner of this component.
intgetY()
Returns the Y coordinate of the upper left corner of this component.
voidgrabFocus()

Requests that this component receive input focus, giving window focus to the top level ancestor of this component.

booleanisDoubleBuffered()
Get the value of the {@link #doubleBuffered} property.
static booleanisLightweightComponent(Component c)
Return true if the provided component has no native peer; in other words, if it is a "lightweight component".
booleanisManagingFocus()
Return true if you wish this component to manage its own focus.
booleanisOpaque()
Return the current value of the {@link #opaque} property.
booleanisOptimizedDrawingEnabled()
Return true if the component can guarantee that none of its children will overlap in Z-order.
booleanisPaintingTile()
Return true if this component is currently painting a tile, this means that paint() is called again on another child component.
booleanisRequestFocusEnabled()
Get the value of the {@link #requestFocusEnabled} property.
booleanisValidateRoot()
Return true if this component is a validation root; this will cause calls to {@link #invalidate()} in this component's children to be "captured" at this component, and not propagate to its parents.
voidpaint(Graphics g)

Paint the component.

protected voidpaintBorder(Graphics g)
Paint the component's border.
protected voidpaintChildren(Graphics g)
Paint the component's children.
protected voidpaintComponent(Graphics g)
Paint the component's body.
voidpaintImmediately(int x, int y, int w, int h)
A variant of {@link #paintImmediately(Rectangle)} which takes integer parameters.
voidpaintImmediately(Rectangle r)
Transform the provided dirty rectangle for this component into the appropriate ancestral {@link JRootPane} and call {@link #paint} on that root pane.
protected StringparamString()
Return a string representation for this component, for use in debugging.
voidprint(Graphics g)
Prints this component to the given Graphics context.
voidprintAll(Graphics g)
Prints this component to the given Graphics context.
protected voidprintBorder(Graphics g)
Print this component's border to the specified Graphics context.
protected voidprintChildren(Graphics g)
Print this component's children to the specified Graphics context.
protected voidprintComponent(Graphics g)
Prints this component to the specified Graphics context.
protected voidprocessComponentKeyEvent(KeyEvent e)
A hook for subclasses which want to customize event processing.
protected booleanprocessKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)
protected voidprocessKeyEvent(KeyEvent e)
Override the default key dispatch system from Component to hook into the swing {@link InputMap} / {@link ActionMap} system.
protected voidprocessMouseMotionEvent(MouseEvent ev)
Processes mouse motion event, like dragging and moving.
voidputClientProperty(Object key, Object value)
Add a client property value to this component, associated with key.
voidregisterKeyboardAction(ActionListener act, KeyStroke stroke, int cond)
A variant of {@link #registerKeyboardAction(ActionListener,String,KeyStroke,int)} which provides null for the command name.
voidregisterKeyboardAction(ActionListener act, String cmd, KeyStroke stroke, int cond)
An obsolete method to register a keyboard action on this component.
voidremoveAncestorListener(AncestorListener listener)
Unregister an AncestorListener.
voidremoveNotify()
Receives notification that this component no longer has a parent.
voidremoveVetoableChangeListener(VetoableChangeListener listener)
Unregister a VetoableChangeChangeListener.
voidrepaint(long tm, int x, int y, int width, int height)
Mark the described region of this component as dirty in the current {@link RepaintManager}.
voidrepaint(Rectangle r)
Mark the described region of this component as dirty in the current {@link RepaintManager}.
booleanrequestDefaultFocus()
Request focus on the default component of this component's {@link FocusTraversalPolicy}.
voidrequestFocus()
Requests that this component gets the input focus if the requestFocusEnabled property is set to true.
booleanrequestFocus(boolean temporary)
This method is overridden to make it public so that it can be used by look and feel implementations.
booleanrequestFocusInWindow()
Requests that this component gets the input focus if the top level window that contains this component has the focus and the requestFocusEnabled property is set to true.
protected booleanrequestFocusInWindow(boolean temporary)
This method is overridden to make it public so that it can be used by look and feel implementations.
voidresetKeyboardActions()
Reset all keyboard action registries.
voidreshape(int x, int y, int w, int h)
Moves and resizes the component.
voidrevalidate()
Queue a an invalidation and revalidation of this component, using {@link RepaintManager#addInvalidComponent}.
voidscrollRectToVisible(Rectangle r)
Calls scrollRectToVisible on the component's parent.
voidsetActionMap(ActionMap map)
voidsetAlignmentX(float a)
Set the value of the {@link #alignmentX} property.
voidsetAlignmentY(float a)
Set the value of the {@link #alignmentY} property.
voidsetAutoscrolls(boolean a)
Set the value of the {@link #autoscrolls} property.
voidsetBackground(Color bg)
Set the value of the background property.
voidsetBorder(Border newBorder)
Set the value of the {@link #border} property.
voidsetComponentPopupMenu(JPopupMenu popup)
Sets the popup menu for this component (this is a bound property with the property name 'componentPopupMenu').
voidsetDebugGraphicsOptions(int debugOptions)
Set the value of the {@link #debugGraphicsOptions} property.
static voidsetDefaultLocale(Locale l)
Sets the locale to be used as the default for all new components.
voidsetDoubleBuffered(boolean db)
Set the value of the {@link #doubleBuffered} property.
voidsetEnabled(boolean enable)
Set the value of the enabled property.
voidsetFont(Font f)
Set the value of the font property.
voidsetForeground(Color fg)
Set the value of the foreground property.
voidsetInheritsPopupMenu(boolean inherit)
Sets the flag that controls whether or not the component inherits its parent's popup menu when no popup menu is specified for this component.
voidsetInputMap(int condition, InputMap map)
Sets the input map for the given condition.
voidsetInputVerifier(InputVerifier verifier)
Sets the input verifier to use by this component.
voidsetNextFocusableComponent(Component aComponent)
Set the specified component to be the next component in the focus cycle, overriding the {@link FocusTraversalPolicy} for this component.
voidsetOpaque(boolean isOpaque)
Set if the component should paint all pixels withing its bounds.
voidsetRequestFocusEnabled(boolean e)
Set the value of the {@link #requestFocusEnabled} property.
voidsetToolTipText(String text)
Set the tooltip text for this component.
voidsetTransferHandler(TransferHandler newHandler)
Set the value of the {@link #transferHandler} property.
protected voidsetUI(ComponentUI newUI)
Install a new UI delegate as the component's {@link #ui} property.
voidsetVerifyInputWhenFocusTarget(boolean verifyInputWhenFocusTarget)
voidsetVisible(boolean v)
Set the value of the visible property.
voidunregisterKeyboardAction(KeyStroke aKeyStroke)
Remove a keyboard action registry.
voidupdate(Graphics g)
Call {@link #paint}.
voidupdateUI()
This method should be overridden in subclasses.

Field Detail

accessibleContext

protected AccessibleContext accessibleContext
The accessible context of this JComponent.

listenerList

protected EventListenerList listenerList
Listeners for events other than {@link PropertyChangeEvent} are handled by this listener list. PropertyChangeEvents are handled in {@link #changeSupport}.

TOOL_TIP_TEXT_KEY

public static final String TOOL_TIP_TEXT_KEY

ui

protected ComponentUI ui
The user interface delegate for this component. Event delivery and repainting of the component are usually delegated to this object.

See Also: JComponent JComponent JComponent

UNDEFINED_CONDITION

public static final int UNDEFINED_CONDITION
Constant used to indicate that no condition has been assigned to a particular action.

See Also: JComponent

WHEN_ANCESTOR_OF_FOCUSED_COMPONENT

public static final int WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
Constant used to indicate that an action should be performed only when the component is an ancestor of the component which has focus.

See Also: JComponent

WHEN_FOCUSED

public static final int WHEN_FOCUSED
Constant used to indicate that an action should be performed only when the component has focus.

See Also: JComponent

WHEN_IN_FOCUSED_WINDOW

public static final int WHEN_IN_FOCUSED_WINDOW
Constant used to indicate that an action should be performed only when the component is in the window which has focus.

See Also: JComponent

Constructor Detail

JComponent

public JComponent()
Creates a new JComponent instance.

Method Detail

addAncestorListener

public void addAncestorListener(AncestorListener listener)
Register an AncestorListener.

Parameters: listener The listener to register

See Also: JComponent

addNotify

public void addNotify()
Receives notification if this component is added to a parent component. Notification is sent to all registered AncestorListeners about the new parent. This method sets up ActionListeners for all registered KeyStrokes of this component in the chain of parent components. A PropertyChange event is fired to indicate that the ancestor property has changed. This method is used internally and should not be used in applications.

addVetoableChangeListener

public void addVetoableChangeListener(VetoableChangeListener listener)
Register a VetoableChangeListener.

Parameters: listener The listener to register

See Also: JComponent listenerList

computeVisibleRect

public void computeVisibleRect(Rectangle rect)
Compute the component's visible rectangle, which is defined recursively as either the component's bounds, if it has no parent, or the intersection of the component's bounds with the visible rectangle of its parent.

Parameters: rect The return value slot to place the visible rectangle in

contains

public boolean contains(int x, int y)
Returns true if the coordinates (x, y) lie within the bounds of this component and false otherwise. x and y are relative to the coordinate space of the component.

Parameters: x the X coordinate of the point to check y the Y coordinate of the point to check

Returns: true if the specified point lies within the bounds of this component, false otherwise

createToolTip

public JToolTip createToolTip()
Return the toolTip property of this component, creating it and setting it if it is currently null. This method can be overridden in subclasses which wish to control the exact form of tooltip created.

Returns: The current toolTip

disable

public void disable()

Deprecated: replaced by {@link #setEnabled(boolean)}

Disables this component.

enable

public void enable()

Deprecated: replaced by {@link #setEnabled(boolean)}

Enables this component.

firePropertyChange

public void firePropertyChange(String property, int oldValue, int newValue)
Fires a property change for a primitive integer property.

Parameters: property the name of the property oldValue the old value of the property newValue the new value of the property

UNKNOWN: This method is implemented in {@link Component#firePropertyChange(String, int, int)}. It is only here because it is specified to be public, whereas the Component method is protected.

firePropertyChange

public void firePropertyChange(String property, boolean oldValue, boolean newValue)
Fires a property change for a primitive boolean property.

Parameters: property the name of the property oldValue the old value of the property newValue the new value of the property

UNKNOWN: This method is implemented in {@link Component#firePropertyChange(String, boolean, boolean)}. It is only here because it is specified to be public, whereas the Component method is protected.

fireVetoableChange

protected void fireVetoableChange(String propertyName, Object oldValue, Object newValue)
Call {@link VetoableChangeListener#vetoableChange} on all listeners registered to listen to a given property. Any method which changes the specified property of this component should call this method.

Parameters: propertyName The property which changed oldValue The old value of the property newValue The new value of the property

Throws: PropertyVetoException if the change was vetoed by a listener

See Also: JComponent JComponent

getAccessibleContext

public AccessibleContext getAccessibleContext()
Get the value of the accessibleContext property for this component.

Returns: the current value of the property

getActionForKeyStroke

public ActionListener getActionForKeyStroke(KeyStroke ks)
Get the ActionListener (typically an {@link Action} object) which is associated with a particular keystroke.

Parameters: ks The keystroke to retrieve the action of

Returns: The action associated with the specified keystroke

getActionMap

public final ActionMap getActionMap()

getAlignmentX

public float getAlignmentX()
Get the value of the {@link #alignmentX} property.

Returns: The current value of the property.

See Also: JComponent alignmentY

getAlignmentY

public float getAlignmentY()
Get the value of the {@link #alignmentY} property.

Returns: The current value of the property.

See Also: JComponent alignmentX

getAncestorListeners

public AncestorListener[] getAncestorListeners()
Return all registered AncestorListener objects.

Returns: The set of AncestorListener objects in {@link #listenerList}

getAutoscrolls

public boolean getAutoscrolls()
Get the current value of the {@link #autoscrolls} property.

Returns: The current value of the property

getBorder

public Border getBorder()
Get the value of the {@link #border} property.

Returns: The property's current value

See Also: JComponent

getBounds

public Rectangle getBounds(Rectangle rv)
Get the component's current bounding box. If a rectangle is provided, use this as the return value (adjusting its fields in place); otherwise (of null is provided) return a new {@link Rectangle}.

Parameters: rv Optional return value to use

Returns: A rectangle bounding the component

getClientProperty

public final Object getClientProperty(Object key)
Get a client property associated with this component and a particular key.

Parameters: key The key with which to look up the client property

Returns: A client property associated with this object and key

See Also: clientProperties JComponent JComponent

getComponentGraphics

protected Graphics getComponentGraphics(Graphics g)
Prepares a graphics context for painting this object. If {@link #debugGraphicsOptions} is not equal to {@link DebugGraphics#NONE_OPTION}, produce a new {@link DebugGraphics} object wrapping the parameter. Otherwise configure the parameter with this component's foreground color and font.

Parameters: g The graphics context to wrap or configure

Returns: A graphics context to paint this object with

See Also: debugGraphicsOptions JComponent

getComponentPopupMenu

public JPopupMenu getComponentPopupMenu()
Returns the popup menu for this component. If the popup menu is null AND the {@link #getInheritsPopupMenu()} method returns true, this method will return the parent's popup menu (if it has one).

Returns: The popup menu (possibly null.

Since: 1.5

See Also: setComponentPopupMenu getInheritsPopupMenu

getConditionForKeyStroke

public int getConditionForKeyStroke(KeyStroke ks)
Return the condition that determines whether a registered action occurs in response to the specified keystroke. As of 1.3 KeyStrokes can be registered with multiple simultaneous conditions.

Parameters: ks The keystroke to return the condition of

Returns: One of the values {@link #UNDEFINED_CONDITION}, {@link #WHEN_ANCESTOR_OF_FOCUSED_COMPONENT}, {@link #WHEN_FOCUSED}, or {@link #WHEN_IN_FOCUSED_WINDOW}

See Also: JComponent JComponent JComponent

getDebugGraphicsOptions

public int getDebugGraphicsOptions()
Get the value of the {@link #debugGraphicsOptions} property.

Returns: The current value of the property.

See Also: JComponent debugGraphicsOptions

getDefaultLocale

public static Locale getDefaultLocale()
Returns the locale used as the default for all new components. The default value is {@link Locale#getDefault()} (that is, the platform default locale).

Returns: The locale (never null).

See Also: setDefaultLocale

getGraphics

public Graphics getGraphics()
Returns the Graphics context for this component. This can be used to draw on a component.

Returns: the Graphics context for this component

getHeight

public int getHeight()
Returns the height of this component. Prefer this method over {@link #getBounds} or {@link #getSize} because it does not cause any heap allocation.

Returns: the height of the component

getInheritsPopupMenu

public boolean getInheritsPopupMenu()
Returns the flag that controls whether or not the component inherits its parent's popup menu when no popup menu is specified for this component.

Returns: A boolean.

Since: 1.5

See Also: JComponent

getInputMap

public final InputMap getInputMap(int condition)
Returns the input map associated with this component for the given state/condition.

Parameters: condition the state (one of {@link #WHEN_FOCUSED}, {@link #WHEN_ANCESTOR_OF_FOCUSED_COMPONENT} and {@link #WHEN_IN_FOCUSED_WINDOW}).

Returns: The input map.

Throws: IllegalArgumentException if condition is not one of the specified values.

Since: 1.3

getInputMap

public final InputMap getInputMap()
Returns the input map associated with this component for the {@link #WHEN_FOCUSED} state.

Returns: The input map.

Since: 1.3

See Also: JComponent

getInputVerifier

public InputVerifier getInputVerifier()
Returns the currently set input verifier for this component.

Returns: the input verifier, or null if none

getInsets

public Insets getInsets()
Get the component's insets, which are calculated from the {@link #border} property. If the border is null, calls {@link Container#getInsets}.

Returns: The component's current insets

getInsets

public Insets getInsets(Insets insets)
Get the component's insets, which are calculated from the {@link #border} property. If the border is null, calls {@link Container#getInsets}. The passed-in {@link Insets} value will be used as the return value, if possible.

Parameters: insets Return value object to reuse, if possible

Returns: The component's current insets

getListeners

public <T extends EventListener> T[] getListeners(Class<T> listenerType)
Returns all registered {@link EventListener}s of the given listenerType.

Parameters: listenerType the class of listeners to filter (null not permitted).

Returns: An array of registered listeners.

Throws: ClassCastException if listenerType does not implement the {@link EventListener} interface. NullPointerException if listenerType is null.

Since: 1.3

See Also: getAncestorListeners listenerList

getLocation

public Point getLocation(Point rv)
Get the component's location. The passed-in {@link Point} value will be used as the return value, if possible.

Parameters: rv Return value object to reuse, if possible

Returns: The component's current location

getMaximumSize

public Dimension getMaximumSize()
Get the component's maximum size. If the maximumSize property has been explicitly set, it is returned. If the maximumSize property has not been set but the {@link #ui} property has been, the result of {@link ComponentUI#getMaximumSize} is returned. If neither property has been set, the result of {@link Container#getMaximumSize} is returned.

Returns: the maximum size of the component

See Also: Component getMaximumSize isMaximumSizeSet getMaximumSize

getMinimumSize

public Dimension getMinimumSize()
Get the component's minimum size. If the minimumSize property has been explicitly set, it is returned. If the minimumSize property has not been set but the {@link #ui} property has been, the result of {@link ComponentUI#getMinimumSize} is returned. If neither property has been set, the result of {@link Container#getMinimumSize} is returned.

Returns: The minimum size of the component

See Also: Component getMinimumSize isMinimumSizeSet getMinimumSize

getNextFocusableComponent

public Component getNextFocusableComponent()

Deprecated: See {@link java.awt.FocusTraversalPolicy}

Return the value of the nextFocusableComponent property.

Returns: The current value of the property, or null if none has been set.

getPreferredSize

public Dimension getPreferredSize()
Get the component's preferred size. If the preferredSize property has been explicitly set, it is returned. If the preferredSize property has not been set but the {@link #ui} property has been, the result of {@link ComponentUI#getPreferredSize} is returned. If neither property has been set, the result of {@link Container#getPreferredSize} is returned.

Returns: The preferred size of the component

See Also: Component getPreferredSize isPreferredSizeSet getPreferredSize

getRegisteredKeyStrokes

public KeyStroke[] getRegisteredKeyStrokes()
Return the set of {@link KeyStroke} objects which are registered to initiate actions on this component.

Returns: An array of the registered keystrokes (possibly empty but never null).

getRootPane

public JRootPane getRootPane()
Returns the first ancestor of this component which is a {@link JRootPane}. Equivalent to calling SwingUtilities.getRootPane(this);.

Returns: An ancestral JRootPane, or null if none exists.

getSize

public Dimension getSize(Dimension rv)
Get the component's size. The passed-in {@link Dimension} value will be used as the return value, if possible.

Parameters: rv Return value object to reuse, if possible

Returns: The component's current size

getToolTipLocation

public Point getToolTipLocation(MouseEvent event)
Return the location at which the toolTipText property should be displayed, when triggered by a particular mouse event.

Parameters: event The event the tooltip is being presented in response to

Returns: The point at which to display a tooltip, or null if swing is to choose a default location.

getToolTipText

public String getToolTipText()
Returns the current tooltip text for this component, or null if none has been set.

Returns: the current tooltip text for this component, or null if none has been set

See Also: JComponent getToolTipText

getToolTipText

public String getToolTipText(MouseEvent event)
Returns the tooltip text for this component for a particular mouse event. This can be used to support context sensitive tooltips that can change with the mouse location. By default this returns the static tooltip text returned by {@link #getToolTipText()}.

Parameters: event the mouse event which triggered the tooltip

Returns: the tooltip text for this component for a particular mouse event

See Also: JComponent getToolTipText

getTopLevelAncestor

public Container getTopLevelAncestor()
Return the top level ancestral container (usually a {@link java.awt.Window} or {@link java.applet.Applet}) which this component is contained within, or null if no ancestors exist.

Returns: The top level container, if it exists

getTransferHandler

public TransferHandler getTransferHandler()
Get the value of the {@link #transferHandler} property.

Returns: The current value of the property

See Also: JComponent

getUIClassID

public String getUIClassID()
Get the value of the UIClassID property. This property should be a key in the {@link UIDefaults} table managed by {@link UIManager}, the value of which is the name of a class to load for the component's {@link #ui} property.

Returns: A "symbolic" name which will map to a class to use for the component's UI, such as "ComponentUI"

See Also: JComponent JComponent

getVerifyInputWhenFocusTarget

public boolean getVerifyInputWhenFocusTarget()

Since: 1.3

getVetoableChangeListeners

public VetoableChangeListener[] getVetoableChangeListeners()
Return all registered VetoableChangeListener objects.

Returns: An array of the VetoableChangeListener objects registered with this component (possibly empty but never null).

Since: 1.4

getVisibleRect

public Rectangle getVisibleRect()
Return the component's visible rectangle in a new {@link Rectangle}, rather than via a return slot.

Returns: the component's visible rectangle

See Also: computeVisibleRect

getWidth

public int getWidth()
Returns the width of this component. Prefer this method over {@link #getBounds} or {@link #getSize} because it does not cause any heap allocation.

Returns: the width of the component

getX

public int getX()
Returns the X coordinate of the upper left corner of this component. Prefer this method over {@link #getBounds} or {@link #getLocation} because it does not cause any heap allocation.

Returns: the X coordinate of the upper left corner of the component

getY

public int getY()
Returns the Y coordinate of the upper left corner of this component. Prefer this method over {@link #getBounds} or {@link #getLocation} because it does not cause any heap allocation.

Returns: the Y coordinate of the upper left corner of the component

grabFocus

public void grabFocus()

Requests that this component receive input focus, giving window focus to the top level ancestor of this component. Only works on displayable, focusable, visible components.

This method should not be called by clients; it is intended for focus implementations. Use {@link Component#requestFocus()} instead.

See Also: requestFocus

isDoubleBuffered

public boolean isDoubleBuffered()
Get the value of the {@link #doubleBuffered} property.

Returns: The property's current value

isLightweightComponent

public static boolean isLightweightComponent(Component c)
Return true if the provided component has no native peer; in other words, if it is a "lightweight component".

Parameters: c The component to test for lightweight-ness

Returns: Whether or not the component is lightweight

isManagingFocus

public boolean isManagingFocus()

Deprecated: 1.4 Use {@link Component#setFocusTraversalKeys(int, Set)} and {@link Container#setFocusCycleRoot(boolean)} instead

Return true if you wish this component to manage its own focus. In particular: if you want this component to be sent TAB and SHIFT+TAB key events, and to not have its children considered as focus transfer targets. If true, focus traversal around this component changes to CTRL+TAB and CTRL+SHIFT+TAB.

Returns: true if you want this component to manage its own focus, otherwise (by default) false

isOpaque

public boolean isOpaque()
Return the current value of the {@link #opaque} property.

Returns: The current property value

isOptimizedDrawingEnabled

public boolean isOptimizedDrawingEnabled()
Return true if the component can guarantee that none of its children will overlap in Z-order. This is a hint to the painting system. The default is to return true, but some components such as {@link JLayeredPane} should override this to return false.

Returns: Whether the component tiles its children

isPaintingTile

public boolean isPaintingTile()
Return true if this component is currently painting a tile, this means that paint() is called again on another child component. This method returns false if this component does not paint a tile or if the last tile is currently painted.

Returns: whether the component is painting a tile

isRequestFocusEnabled

public boolean isRequestFocusEnabled()
Get the value of the {@link #requestFocusEnabled} property.

Returns: The current value of the property

isValidateRoot

public boolean isValidateRoot()
Return true if this component is a validation root; this will cause calls to {@link #invalidate()} in this component's children to be "captured" at this component, and not propagate to its parents. For most components this should return false, but some components such as {@link JViewport} will want to return true.

Returns: Whether this component is a validation root

paint

public void paint(Graphics g)

Paint the component. This is a delicate process, and should only be called from the repaint thread, under control of the {@link RepaintManager}. Client code should usually call {@link #repaint()} to trigger painting.

The body of the paint call involves calling {@link #paintComponent}, {@link #paintBorder}, and {@link #paintChildren} in order. If you want to customize painting behavior, you should override one of these methods rather than paint.

For more details on the painting sequence, see this article.

Parameters: g The graphics context to paint with

See Also: paintImmediately

paintBorder

protected void paintBorder(Graphics g)
Paint the component's border. This usually means calling {@link Border#paintBorder} on the {@link #border} property, if it is non-null. You may override this if you wish to customize border painting behavior. The border is painted after the component's body, but before the component's children.

Parameters: g The graphics context with which to paint the border

See Also: JComponent JComponent JComponent

paintChildren

protected void paintChildren(Graphics g)
Paint the component's children. This usually means calling {@link Container#paint}, which recursively calls {@link #paint} on any of the component's children, with appropriate changes to coordinate space and clipping region. You may override this if you wish to customize children painting behavior. The children are painted after the component's body and border.

Parameters: g The graphics context with which to paint the children

See Also: JComponent JComponent JComponent

paintComponent

protected void paintComponent(Graphics g)
Paint the component's body. This usually means calling {@link ComponentUI#update} on the {@link #ui} property of the component, if it is non-null. You may override this if you wish to customize the component's body-painting behavior. The component's body is painted first, before the border and children.

Parameters: g The graphics context with which to paint the body

See Also: JComponent JComponent JComponent

paintImmediately

public void paintImmediately(int x, int y, int w, int h)
A variant of {@link #paintImmediately(Rectangle)} which takes integer parameters.

Parameters: x The left x coordinate of the dirty region y The top y coordinate of the dirty region w The width of the dirty region h The height of the dirty region

paintImmediately

public void paintImmediately(Rectangle r)
Transform the provided dirty rectangle for this component into the appropriate ancestral {@link JRootPane} and call {@link #paint} on that root pane. This method is called from the {@link RepaintManager} and should always be called within the painting thread.

This method will acquire a double buffer from the {@link RepaintManager} if the component's {@link #doubleBuffered} property is true and the paint call is the first recursive paint call inside swing.

The method will also modify the provided {@link Graphics} context via the {@link #getComponentGraphics} method. If you want to customize the graphics object used for painting, you should override that method rather than paint.

Parameters: r The dirty rectangle to paint

paramString

protected String paramString()
Return a string representation for this component, for use in debugging.

Returns: A string describing this component.

print

public void print(Graphics g)
Prints this component to the given Graphics context. A call to this method results in calls to the methods {@link #printComponent}, {@link #printBorder} and {@link #printChildren} in this order. Double buffering is temporarily turned off so the painting goes directly to the supplied Graphics context.

Parameters: g the Graphics context to print onto

printAll

public void printAll(Graphics g)
Prints this component to the given Graphics context. This invokes {@link #print}.

Parameters: g the Graphics context to print onto

printBorder

protected void printBorder(Graphics g)
Print this component's border to the specified Graphics context. The default behaviour is to invoke {@link #paintBorder}. Override this if you want special behaviour for printing.

Parameters: g the Graphics context to print onto

Since: 1.3

printChildren

protected void printChildren(Graphics g)
Print this component's children to the specified Graphics context. The default behaviour is to invoke {@link #paintChildren}. Override this if you want special behaviour for printing.

Parameters: g the Graphics context to print onto

Since: 1.3

printComponent

protected void printComponent(Graphics g)
Prints this component to the specified Graphics context. The default behaviour is to invoke {@link #paintComponent}. Override this if you want special behaviour for printing.

Parameters: g the Graphics context to print onto

Since: 1.3

processComponentKeyEvent

protected void processComponentKeyEvent(KeyEvent e)
A hook for subclasses which want to customize event processing.

processKeyBinding

protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)

processKeyEvent

protected void processKeyEvent(KeyEvent e)
Override the default key dispatch system from Component to hook into the swing {@link InputMap} / {@link ActionMap} system. See this report for more details, it's somewhat complex.

processMouseMotionEvent

protected void processMouseMotionEvent(MouseEvent ev)
Processes mouse motion event, like dragging and moving.

Parameters: ev the MouseEvent describing the mouse motion

putClientProperty

public final void putClientProperty(Object key, Object value)
Add a client property value to this component, associated with key. If there is an existing client property associated with key, it will be replaced. A {@link PropertyChangeEvent} is sent to registered listeners (with the name of the property being key.toString()).

Parameters: key The key of the client property association to add value The value of the client property association to add

See Also: clientProperties JComponent JComponent

registerKeyboardAction

public void registerKeyboardAction(ActionListener act, KeyStroke stroke, int cond)
A variant of {@link #registerKeyboardAction(ActionListener,String,KeyStroke,int)} which provides null for the command name.

Parameters: act the action listener to notify when the keystroke occurs. stroke the key stroke. cond the condition (one of {@link #WHEN_FOCUSED}, {@link #WHEN_IN_FOCUSED_WINDOW} and {@link #WHEN_ANCESTOR_OF_FOCUSED_COMPONENT}).

registerKeyboardAction

public void registerKeyboardAction(ActionListener act, String cmd, KeyStroke stroke, int cond)
An obsolete method to register a keyboard action on this component. You should use getInputMap and getActionMap to fetch mapping tables from keystrokes to commands, and commands to actions, respectively, and modify those mappings directly.

Parameters: act The action to be registered cmd The command to deliver in the delivered {@link java.awt.event.ActionEvent} stroke The keystroke to register on cond One of the values {@link #UNDEFINED_CONDITION}, {@link #WHEN_ANCESTOR_OF_FOCUSED_COMPONENT}, {@link #WHEN_FOCUSED}, or {@link #WHEN_IN_FOCUSED_WINDOW}, indicating the condition which must be met for the action to be fired

See Also: JComponent JComponent JComponent

removeAncestorListener

public void removeAncestorListener(AncestorListener listener)
Unregister an AncestorListener.

Parameters: listener The listener to unregister

See Also: JComponent

removeNotify

public void removeNotify()
Receives notification that this component no longer has a parent. This method sends an AncestorEvent to all registered AncestorListeners, notifying them that the parent is gone. The keybord actions of this component are removed from the parent and its ancestors. A PropertyChangeEvent is fired to indicate that the 'ancestor' property has changed. This method is called before the component is actually removed from its parent, so the parent is still visible through {@link Component#getParent}.

removeVetoableChangeListener

public void removeVetoableChangeListener(VetoableChangeListener listener)
Unregister a VetoableChangeChangeListener.

Parameters: listener The listener to unregister

See Also: JComponent

repaint

public void repaint(long tm, int x, int y, int width, int height)
Mark the described region of this component as dirty in the current {@link RepaintManager}. This will queue an asynchronous repaint using the system painting thread in the near future.

Parameters: tm ignored x coordinate of the region to mark as dirty y coordinate of the region to mark as dirty width dimension of the region to mark as dirty height dimension of the region to mark as dirty

repaint

public void repaint(Rectangle r)
Mark the described region of this component as dirty in the current {@link RepaintManager}. This will queue an asynchronous repaint using the system painting thread in the near future.

Parameters: r The rectangle to mark as dirty

requestDefaultFocus

public boolean requestDefaultFocus()

Deprecated: Use {@link #requestFocus()} on the default component provided from the {@link FocusTraversalPolicy} instead.

Request focus on the default component of this component's {@link FocusTraversalPolicy}.

Returns: The result of {@link #requestFocus()}

requestFocus

public void requestFocus()
Requests that this component gets the input focus if the requestFocusEnabled property is set to true. This also means that this component's top-level window becomes the focused window, if that is not already the case. The preconditions that have to be met to become a focus owner is that the component must be displayable, visible and focusable. Note that this signals only a request for becoming focused. There are situations in which it is not possible to get the focus. So developers should not assume that the component has the focus until it receives a {@link java.awt.event.FocusEvent} with a value of {@link java.awt.event.FocusEvent#FOCUS_GAINED}.

See Also: requestFocus

requestFocus

public boolean requestFocus(boolean temporary)
This method is overridden to make it public so that it can be used by look and feel implementations. You should not use this method directly. Instead you are strongly encouraged to call {@link #requestFocus()} or {@link #requestFocusInWindow()} instead.

Parameters: temporary if the focus change is temporary

Returns: false if the focus change request will definitly fail, true if it will likely succeed

Since: 1.4

See Also:

requestFocusInWindow

public boolean requestFocusInWindow()
Requests that this component gets the input focus if the top level window that contains this component has the focus and the requestFocusEnabled property is set to true. The preconditions that have to be met to become a focus owner is that the component must be displayable, visible and focusable. Note that this signals only a request for becoming focused. There are situations in which it is not possible to get the focus. So developers should not assume that the component has the focus until it receives a {@link java.awt.event.FocusEvent} with a value of {@link java.awt.event.FocusEvent#FOCUS_GAINED}.

Returns: false if the focus change request will definitly fail, true if it will likely succeed

See Also: requestFocusInWindow

requestFocusInWindow

protected boolean requestFocusInWindow(boolean temporary)
This method is overridden to make it public so that it can be used by look and feel implementations. You should not use this method directly. Instead you are strongly encouraged to call {@link #requestFocus()} or {@link #requestFocusInWindow()} instead.

Parameters: temporary if the focus change is temporary

Returns: false if the focus change request will definitly fail, true if it will likely succeed

Since: 1.4

See Also:

resetKeyboardActions

public void resetKeyboardActions()
Reset all keyboard action registries.

See Also: JComponent JComponent JComponent

reshape

public void reshape(int x, int y, int w, int h)
Moves and resizes the component.

Parameters: x the new horizontal location y the new vertial location w the new width h the new height

revalidate

public void revalidate()
Queue a an invalidation and revalidation of this component, using {@link RepaintManager#addInvalidComponent}.

scrollRectToVisible

public void scrollRectToVisible(Rectangle r)
Calls scrollRectToVisible on the component's parent. Components which can service this call should override.

Parameters: r The rectangle to make visible

setActionMap

public final void setActionMap(ActionMap map)

setAlignmentX

public void setAlignmentX(float a)
Set the value of the {@link #alignmentX} property.

Parameters: a The new value of the property

setAlignmentY

public void setAlignmentY(float a)
Set the value of the {@link #alignmentY} property.

Parameters: a The new value of the property

setAutoscrolls

public void setAutoscrolls(boolean a)
Set the value of the {@link #autoscrolls} property.

Parameters: a The new value of the property

setBackground

public void setBackground(Color bg)
Set the value of the background property.

Parameters: bg The new value of the property

setBorder

public void setBorder(Border newBorder)
Set the value of the {@link #border} property.

Parameters: newBorder The new value of the property

See Also: JComponent

setComponentPopupMenu

public void setComponentPopupMenu(JPopupMenu popup)
Sets the popup menu for this component (this is a bound property with the property name 'componentPopupMenu').

Parameters: popup the popup menu (null permitted).

Since: 1.5

See Also: getComponentPopupMenu

setDebugGraphicsOptions

public void setDebugGraphicsOptions(int debugOptions)
Set the value of the {@link #debugGraphicsOptions} property.

Parameters: debugOptions The new value of the property

setDefaultLocale

public static void setDefaultLocale(Locale l)
Sets the locale to be used as the default for all new components. If this is set to null, the {@link #getDefaultLocale()} method will return the platform default locale.

Parameters: l the locale (null permitted).

setDoubleBuffered

public void setDoubleBuffered(boolean db)
Set the value of the {@link #doubleBuffered} property.

Parameters: db The new value of the property

setEnabled

public void setEnabled(boolean enable)
Set the value of the enabled property.

Parameters: enable The new value of the property

setFont

public void setFont(Font f)
Set the value of the font property.

Parameters: f The new value of the property

setForeground

public void setForeground(Color fg)
Set the value of the foreground property.

Parameters: fg The new value of the property

setInheritsPopupMenu

public void setInheritsPopupMenu(boolean inherit)
Sets the flag that controls whether or not the component inherits its parent's popup menu when no popup menu is specified for this component. This is a bound property with the property name 'inheritsPopupMenu'.

Parameters: inherit the new flag value.

Since: 1.5

See Also: getInheritsPopupMenu

setInputMap

public final void setInputMap(int condition, InputMap map)
Sets the input map for the given condition.

Parameters: condition the condition (one of {@link #WHEN_FOCUSED}, {@link #WHEN_IN_FOCUSED_WINDOW} and {@link #WHEN_ANCESTOR_OF_FOCUSED_COMPONENT}). map the map.

Throws: IllegalArgumentException if condition is not one of the specified values.

setInputVerifier

public void setInputVerifier(InputVerifier verifier)
Sets the input verifier to use by this component.

Parameters: verifier the input verifier, or null

setNextFocusableComponent

public void setNextFocusableComponent(Component aComponent)

Deprecated: Use FocusTraversalPolicy instead

Set the specified component to be the next component in the focus cycle, overriding the {@link FocusTraversalPolicy} for this component.

Parameters: aComponent The component to set as the next focusable

setOpaque

public void setOpaque(boolean isOpaque)
Set if the component should paint all pixels withing its bounds. If this property is set to false, the component expects the cleared background.

Parameters: isOpaque if true, paint all pixels. If false, expect the clean background.

See Also: ComponentUI

setRequestFocusEnabled

public void setRequestFocusEnabled(boolean e)
Set the value of the {@link #requestFocusEnabled} property.

Parameters: e The new value of the property

setToolTipText

public void setToolTipText(String text)
Set the tooltip text for this component. If a non-null value is set, this component is registered in the ToolTipManager in order to turn on tooltips for this component. If a null value is set, tooltips are turne off for this component.

Parameters: text the tooltip text for this component

See Also: getToolTipText getToolTipText

setTransferHandler

public void setTransferHandler(TransferHandler newHandler)
Set the value of the {@link #transferHandler} property.

Parameters: newHandler The new value of the property

See Also: JComponent

setUI

protected void setUI(ComponentUI newUI)
Install a new UI delegate as the component's {@link #ui} property. In the process, this will call {@link ComponentUI#uninstallUI} on any existing value for the {@link #ui} property, and {@link ComponentUI#installUI} on the new UI delegate.

Parameters: newUI The new UI delegate to install

See Also: JComponent JComponent

setVerifyInputWhenFocusTarget

public void setVerifyInputWhenFocusTarget(boolean verifyInputWhenFocusTarget)

Since: 1.3

setVisible

public void setVisible(boolean v)
Set the value of the visible property. If the value is changed, then the AncestorListeners of this component and all its children (recursivly) are notified.

Parameters: v The new value of the property

unregisterKeyboardAction

public void unregisterKeyboardAction(KeyStroke aKeyStroke)
Remove a keyboard action registry.

Parameters: aKeyStroke The keystroke to unregister

See Also: JComponent JComponent JComponent

update

public void update(Graphics g)
Call {@link #paint}.

Parameters: g The graphics context to paint into

updateUI

public void updateUI()
This method should be overridden in subclasses. In JComponent, the method does nothing. In subclasses, it should a UI delegate (corresponding to the symbolic name returned from {@link #getUIClassID}) from the {@link UIManager}, and calls {@link #setUI} with the new delegate.