javax.swing

Class JComponent

Implemented Interfaces:
ImageObserver, MenuContainer, Serializable
Known Direct Subclasses:
AbstractButton, BasicInternalFrameTitlePane, Box, Box.Filler, JColorChooser, JComboBox, JFileChooser, JInternalFrame, JInternalFrame.JDesktopIcon, JLabel, JLayeredPane, JList, JMenuBar, JOptionPane, JPanel, JPopupMenu, JProgressBar, JRootPane, JScrollBar, JScrollPane, JSeparator, JSlider, JSpinner, JSplitPane, JTabbedPane, JTable, JTableHeader, JTextComponent, JToolBar, JToolTip, JTree, JViewport

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.
See Also:
Serialized Form

Nested Class Summary

abstract class
JComponent.AccessibleJComponent
Basic accessibility support for JComponent derived widgets.

Nested classes/interfaces inherited from class java.awt.Container

Container.AccessibleAWTContainer

Nested classes/interfaces inherited from class java.awt.Component

Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy

Field Summary

static String
TOOL_TIP_TEXT_KEY
static int
UNDEFINED_CONDITION
Constant used to indicate that no condition has been assigned to a particular action.
static 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.
static int
WHEN_FOCUSED
Constant used to indicate that an action should be performed only when the component has focus.
static 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.
protected AccessibleContext
accessibleContext
The accessible context of this JComponent.
protected EventListenerList
listenerList
Listeners for events other than PropertyChangeEvent are handled by this listener list.
protected ComponentUI
ui
The user interface delegate for this component.

Fields inherited from class java.awt.Component

BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT

Fields inherited from interface java.awt.image.ImageObserver

ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH

Constructor Summary

JComponent()
Creates a new JComponent instance.

Method Summary

void
addAncestorListener(AncestorListener listener)
Register an AncestorListener.
void
addNotify()
Receives notification if this component is added to a parent component.
void
addVetoableChangeListener(VetoableChangeListener listener)
Register a VetoableChangeListener.
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.
boolean
contains(int x, int y)
Returns true if the coordinates (x, y) lie within the bounds of this component and false otherwise.
JToolTip
createToolTip()
Return the toolTip property of this component, creating it and setting it if it is currently null.
void
disable()
Deprecated. replaced by setEnabled(boolean)
void
enable()
Deprecated. replaced by setEnabled(boolean)
extends EventListener> T[] getListeners(Class listenerType)
Returns all registered EventListeners of the given listenerType.
void
firePropertyChange(String property, boolean oldValue, boolean newValue)
Fires a property change for a primitive boolean property.
void
firePropertyChange(String property, int oldValue, int newValue)
Fires a property change for a primitive integer property.
protected void
fireVetoableChange(String propertyName, Object oldValue, Object newValue)
Call VetoableChangeListener.vetoableChange(PropertyChangeEvent) on all listeners registered to listen to a given property.
AccessibleContext
getAccessibleContext()
Get the value of the accessibleContext property for this component.
ActionListener
getActionForKeyStroke(KeyStroke ks)
Get the ActionListener (typically an Action object) which is associated with a particular keystroke.
ActionMap
getActionMap()
float
getAlignmentX()
Get the value of the alignmentX property.
float
getAlignmentY()
Get the value of the alignmentY property.
AncestorListener[]
getAncestorListeners()
Return all registered AncestorListener objects.
boolean
getAutoscrolls()
Get the current value of the autoscrolls property.
Border
getBorder()
Get the value of the border property.
Rectangle
getBounds(Rectangle rv)
Get the component's current bounding box.
Object
getClientProperty(Object key)
Get a client property associated with this component and a particular key.
protected Graphics
getComponentGraphics(Graphics g)
Prepares a graphics context for painting this object.
JPopupMenu
getComponentPopupMenu()
Returns the popup menu for this component.
int
getConditionForKeyStroke(KeyStroke ks)
Return the condition that determines whether a registered action occurs in response to the specified keystroke.
int
getDebugGraphicsOptions()
Get the value of the debugGraphicsOptions property.
static Locale
getDefaultLocale()
Returns the locale used as the default for all new components.
Graphics
getGraphics()
Returns the Graphics context for this component.
int
getHeight()
Returns the height of this component.
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.
InputMap
getInputMap()
Returns the input map associated with this component for the WHEN_FOCUSED state.
InputMap
getInputMap(int condition)
Returns the input map associated with this component for the given state/condition.
InputVerifier
getInputVerifier()
Returns the currently set input verifier for this component.
Insets
getInsets()
Get the component's insets, which are calculated from the border property.
Insets
getInsets(Insets insets)
Get the component's insets, which are calculated from the border property.
Point
getLocation(Point rv)
Get the component's location.
Dimension
getMaximumSize()
Get the component's maximum size.
Dimension
getMinimumSize()
Get the component's minimum size.
Component
getNextFocusableComponent()
Deprecated. See FocusTraversalPolicy
Dimension
getPreferredSize()
Get the component's preferred size.
KeyStroke[]
getRegisteredKeyStrokes()
Return the set of KeyStroke objects which are registered to initiate actions on this component.
JRootPane
getRootPane()
Returns the first ancestor of this component which is a JRootPane.
Dimension
getSize(Dimension rv)
Get the component's size.
Point
getToolTipLocation(MouseEvent event)
Return the location at which the toolTipText property should be displayed, when triggered by a particular mouse event.
String
getToolTipText()
Returns the current tooltip text for this component, or null if none has been set.
String
getToolTipText(MouseEvent event)
Returns the tooltip text for this component for a particular mouse event.
Container
getTopLevelAncestor()
Return the top level ancestral container (usually a Window or Applet) which this component is contained within, or null if no ancestors exist.
TransferHandler
getTransferHandler()
Get the value of the transferHandler property.
String
getUIClassID()
Get the value of the UIClassID property.
boolean
getVerifyInputWhenFocusTarget()
VetoableChangeListener[]
getVetoableChangeListeners()
Return all registered VetoableChangeListener objects.
Rectangle
getVisibleRect()
Return the component's visible rectangle in a new Rectangle, rather than via a return slot.
int
getWidth()
Returns the width of this component.
int
getX()
Returns the X coordinate of the upper left corner of this component.
int
getY()
Returns the Y coordinate of the upper left corner of this component.
void
grabFocus()
Requests that this component receive input focus, giving window focus to the top level ancestor of this component.
boolean
isDoubleBuffered()
Get the value of the doubleBuffered property.
static boolean
isLightweightComponent(Component c)
Return true if the provided component has no native peer; in other words, if it is a "lightweight component".
boolean
isManagingFocus()
Deprecated. 1.4 Use Component.setFocusTraversalKeys(int, Set) and Container.setFocusCycleRoot(boolean) instead
boolean
isOpaque()
Return the current value of the opaque property.
boolean
isOptimizedDrawingEnabled()
Return true if the component can guarantee that none of its children will overlap in Z-order.
boolean
isPaintingTile()
Return true if this component is currently painting a tile, this means that paint() is called again on another child component.
boolean
isRequestFocusEnabled()
Get the value of the requestFocusEnabled property.
boolean
isValidateRoot()
Return true if this component is a validation root; this will cause calls to invalidate() in this component's children to be "captured" at this component, and not propagate to its parents.
void
paint(Graphics g)
Paint the component.
protected void
paintBorder(Graphics g)
Paint the component's border.
protected void
paintChildren(Graphics g)
Paint the component's children.
protected void
paintComponent(Graphics g)
Paint the component's body.
void
paintImmediately(int x, int y, int w, int h)
A variant of paintImmediately(Rectangle) which takes integer parameters.
void
paintImmediately(Rectangle r)
Transform the provided dirty rectangle for this component into the appropriate ancestral JRootPane and call paint(Graphics) on that root pane.
protected String
paramString()
Return a string representation for this component, for use in debugging.
void
print(Graphics g)
Prints this component to the given Graphics context.
void
printAll(Graphics g)
Prints this component to the given Graphics context.
protected void
printBorder(Graphics g)
Print this component's border to the specified Graphics context.
protected void
printChildren(Graphics g)
Print this component's children to the specified Graphics context.
protected void
printComponent(Graphics g)
Prints this component to the specified Graphics context.
protected void
processComponentKeyEvent(KeyEvent e)
A hook for subclasses which want to customize event processing.
protected boolean
processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)
protected void
processKeyEvent(KeyEvent e)
Override the default key dispatch system from Component to hook into the swing InputMap / ActionMap system.
protected void
processMouseMotionEvent(MouseEvent ev)
Processes mouse motion event, like dragging and moving.
void
putClientProperty(Object key, Object value)
Add a client property value to this component, associated with key.
void
registerKeyboardAction(ActionListener act, String cmd, KeyStroke stroke, int cond)
An obsolete method to register a keyboard action on this component.
void
registerKeyboardAction(ActionListener act, KeyStroke stroke, int cond)
A variant of registerKeyboardAction(ActionListener,String,KeyStroke,int) which provides null for the command name.
void
removeAncestorListener(AncestorListener listener)
Unregister an AncestorListener.
void
removeNotify()
Receives notification that this component no longer has a parent.
void
removeVetoableChangeListener(VetoableChangeListener listener)
Unregister a VetoableChangeChangeListener.
void
repaint(Rectangle r)
Mark the described region of this component as dirty in the current RepaintManager.
void
repaint(long tm, int x, int y, int width, int height)
Mark the described region of this component as dirty in the current RepaintManager.
boolean
requestDefaultFocus()
Deprecated. Use requestFocus() on the default component provided from the FocusTraversalPolicy instead.
void
requestFocus()
Requests that this component gets the input focus if the requestFocusEnabled property is set to true.
boolean
requestFocus(boolean temporary)
This method is overridden to make it public so that it can be used by look and feel implementations.
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.
protected boolean
requestFocusInWindow(boolean temporary)
This method is overridden to make it public so that it can be used by look and feel implementations.
void
resetKeyboardActions()
Reset all keyboard action registries.
void
reshape(int x, int y, int w, int h)
Moves and resizes the component.
void
revalidate()
Queue a an invalidation and revalidation of this component, using RepaintManager.addInvalidComponent(JComponent).
void
scrollRectToVisible(Rectangle r)
Calls scrollRectToVisible on the component's parent.
void
setActionMap(ActionMap map)
void
setAlignmentX(float a)
Set the value of the alignmentX property.
void
setAlignmentY(float a)
Set the value of the alignmentY property.
void
setAutoscrolls(boolean a)
Set the value of the autoscrolls property.
void
setBackground(Color bg)
Set the value of the background property.
void
setBorder(Border newBorder)
Set the value of the border property.
void
setComponentPopupMenu(JPopupMenu popup)
Sets the popup menu for this component (this is a bound property with the property name 'componentPopupMenu').
void
setDebugGraphicsOptions(int debugOptions)
Set the value of the debugGraphicsOptions property.
static void
setDefaultLocale(Locale l)
Sets the locale to be used as the default for all new components.
void
setDoubleBuffered(boolean db)
Set the value of the doubleBuffered property.
void
setEnabled(boolean enable)
Set the value of the enabled property.
void
setFont(Font f)
Set the value of the font property.
void
setForeground(Color fg)
Set the value of the foreground property.
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.
void
setInputMap(int condition, InputMap map)
Sets the input map for the given condition.
void
setInputVerifier(InputVerifier verifier)
Sets the input verifier to use by this component.
void
setNextFocusableComponent(Component aComponent)
Deprecated. Use FocusTraversalPolicy instead
void
setOpaque(boolean isOpaque)
Set if the component should paint all pixels withing its bounds.
void
setRequestFocusEnabled(boolean e)
Set the value of the requestFocusEnabled property.
void
setToolTipText(String text)
Set the tooltip text for this component.
void
setTransferHandler(TransferHandler newHandler)
Set the value of the transferHandler property.
protected void
setUI(ComponentUI newUI)
Install a new UI delegate as the component's ui property.
void
setVerifyInputWhenFocusTarget(boolean verifyInputWhenFocusTarget)
void
setVisible(boolean v)
Set the value of the visible property.
void
unregisterKeyboardAction(KeyStroke aKeyStroke)
Remove a keyboard action registry.
void
update(Graphics g)
Call paint(Graphics).
void
updateUI()
This method should be overridden in subclasses.

Methods inherited from class java.awt.Container

add, add, add, add, add, addContainerListener, addImpl, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, extends EventListener> T[] getListeners, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponentZOrder, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, setLayout, transferFocusDownCycle, update, validate, validateTree

Methods inherited from class java.awt.Component

action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, extends EventListener> T[] getListeners, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFocusable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paint, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFocusable, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate

Methods inherited from class java.lang.Object

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

Field Details

TOOL_TIP_TEXT_KEY

public static final String TOOL_TIP_TEXT_KEY
Field Value:
"ToolTipText"

UNDEFINED_CONDITION

public static final int UNDEFINED_CONDITION
Constant used to indicate that no condition has been assigned to a particular action.
Field Value:
-1

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.
Field Value:
1

WHEN_FOCUSED

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

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.
Field Value:
2

accessibleContext

protected AccessibleContext accessibleContext
The accessible context of this JComponent.

listenerList

protected EventListenerList listenerList
Listeners for events other than PropertyChangeEvent are handled by this listener list. PropertyChangeEvents are handled in Component.changeSupport.

ui

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

Constructor Details

JComponent

public JComponent()
Creates a new JComponent instance.

Method Details

addAncestorListener

public void addAncestorListener(AncestorListener listener)
Register an AncestorListener.
Parameters:
listener - The listener to register

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.
Overrides:
addNotify in interface Container

addVetoableChangeListener

public void addVetoableChangeListener(VetoableChangeListener listener)
Register a VetoableChangeListener.
Parameters:
listener - The listener to register

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 -