Swing JComponent Class

Advertisements


Introduction

The class JComponent is the base class for all Swing components except top-level containers. To use a component that inherits from JComponent, you must place the component in a containment hierarchy whose root is a top-level Swing container.

Class declaration

Following is the declaration for javax.swing.JComponent class:

public abstract class JComponent
   extends Container
      implements Serializable

Field

Following are the fields for java.awt.Component class:

  • protected AccessibleContext accessibleContext -- The AccessibleContext associated with this JComponent.

  • protected EventListenerList listenerList -- A list of event listeners for this component.

  • static String TOOL_TIP_TEXT_KEY -- The comment to display when the cursor is over the component, also known as a "value tip", "flyover help", or "flyover label".

  • protected ComponentUI ui -- The look and feel delegate for this component.

  • static int UNDEFINED_CONDITION -- Constant used by some of the APIs to mean that no condition is defined.

  • static int WHEN_ANCESTOR_OF_FOCUSED_COMPONENT -- Constant used for registerKeyboardAction that means that the command should be invoked when the receiving component is an ancestor of the focused component or is itself the focused component.

  • static int WHEN_FOCUSED -- Constant used for registerKeyboardAction that means that the command should be invoked when the component has the focus.

  • static int WHEN_IN_FOCUSED_WINDOW -- Constant used for registerKeyboardAction that means that the command should be invoked when the receiving component is in the window that has the focus or is itself the focused component.

Class constructors

S.N.Constructor & Description
1JComponent()
Default JComponent constructor.

Class methods

S.N.Method & Description
1void addAncestorListener(AncestorListener listener)
Registers listener so that it will receive AncestorEvents when it or any of its ancestors move or are made visible or invisible.
2void addNotify()
Notifies this component that it now has a parent component.
3void addVetoableChangeListener(VetoableChangeListener listener)
Adds a VetoableChangeListener to the listener list.
4void computeVisibleRect(Rectangle visibleRect)
Returns the Component's "visible rect rectangle" - the intersection of the visible rectangles for this component and all of its ancestors.
5boolean contains(int x, int y)
Gives the UI delegate an opportunity to define the precise shape of this component for the sake of mouse processing.
6JToolTip createToolTip()
Returns the instance of JToolTip that should be used to display the tooltip.
7void disable()
Deprecated.As of JDK version 1.1, replaced by java.awt.Component.setEnabled(boolean).
8void enable()
Deprecated. As of JDK version 1.1, replaced by java.awt.Component.setEnabled(boolean).
9void firePropertyChange(String propertyName, boolean oldValue, boolean newValue)
Support for reporting bound property changes for boolean properties.
10void firePropertyChange(String propertyName, char oldValue, char newValue)
Reports a bound property change.
11void firePropertyChange(String propertyName, int oldValue, int newValue)
Support for reporting bound property changes for integer properties.
12protected void fireVetoableChange(String propertyName, Object oldValue, Object newValue)
Supports reporting constrained property changes.
13AccessibleContext getAccessibleContext()
Returns the AccessibleContext associated with this JComponent.
14ActionListener getActionForKeyStroke(KeyStroke aKeyStroke)
Returns the object that will perform the action registered for a given keystroke.
15ActionMap getActionMap()
Returns the ActionMap used to determine what Action to fire for particular KeyStroke binding.
16float getAlignmentX()
Overrides Container.getAlignmentX to return the vertical alignment.
17float getAlignmentY()
Overrides Container.getAlignmentY to return the horizontal alignment.
18AncestorListener[] getAncestorListeners()
Returns an array of all the ancestor listeners registered on this component.
19boolean getAutoscrolls()
Gets the autoscrolls property.
20int getBaseline(int width, int height)
Returns the baseline.
21Component.BaselineResizeBehavior getBaselineResizeBehavior()
Returns an enum indicating how the baseline of the component changes as the size changes.
22Border getBorder()
Returns the border of this component or null if no border is currently set.
23Rectangle getBounds(Rectangle rv)
Stores the bounds of this component into "return value" rv and returns rv.
24Object getClientProperty(Object key)
Returns the value of the property with the specified key.
25protected Graphics getComponentGraphics(Graphics g)
Returns the graphics object used to paint this component.
26JPopupMenu getComponentPopupMenu()
Returns JPopupMenu that assigned for this component.
27int getConditionForKeyStroke(KeyStroke aKeyStroke)
Returns the condition that determines whether a registered action occurs in response to the specified keystroke.
28int getDebugGraphicsOptions()
Returns the state of graphics debugging.
29static Locale getDefaultLocale()
Returns the default locale used to initialize each JComponent's locale property upon creation.
30FontMetrics getFontMetrics(Font font)
Gets the FontMetrics for the specified Font.
31Graphics getGraphics()
Returns this component's graphics context, which lets you draw on a component.
32int getHeight()
Returns the current height of this component.
33boolean getInheritsPopupMenu()
Returns true if the JPopupMenu should be inherited from the parent.
34InputMap getInputMap()
Returns the InputMap that is used when the component has focus.
35InputMap getInputMap(int condition)
Returns the InputMap that is used during condition.
36InputVerifier getInputVerifier()
Returns the input verifier for this component.
37Insets getInsets()
If a border has been set on this component, returns the border's insets; otherwise calls super.getInsets.
38Insets getInsets(Insets insets)
Returns an Insets object containing this component's inset values.
39<T extends EventListener> T[] getListeners(Class<T> listenerType)
Returns an array of all the objects currently registered as FooListeners upon this JComponent.
40Point getLocation(Point rv)
Stores the x,y origin of this component into "return value" rv and returns rv.
41Dimension getMaximumSize()
If the maximum size has been set to a non-null value just returns it.
42Dimension getMinimumSize()
If the minimum size has been set to a non-null value just returns it.
43Component getNextFocusableComponent()
Deprecated. As of 1.4, replaced by FocusTraversalPolicy.
44Point getPopupLocation(MouseEvent event)
Returns the preferred location to display the popup menu in this component's coordinate system.
45Dimension getPreferredSize()
If the preferredSize has been set to a non-null value just returns it.
46KeyStroke[] getRegisteredKeyStrokes()
Returns the KeyStrokes that will initiate registered actions.
47JRootPane getRootPane()
Returns the JRootPane ancestor for this component.
48 Dimension getSize(Dimension rv)
Stores the width/height of this component into "return value" rv and returns rv.
49Point getToolTipLocation(MouseEvent event)
Returns the tooltip location in this component's coordinate system.
50String getToolTipText()
Returns the tooltip string that has been set with setToolTipText.
51String getToolTipText(MouseEvent event)
Returns the string to be used as the tooltip for event.
52Container getTopLevelAncestor()
Returns the top-level ancestor of this component (either the containing Window or Applet), or null if this component has not been added to any container.
53TransferHandler getTransferHandler()
Gets the transferHandler property.
54 String getUIClassID()
Returns the UIDefaults key used to look up the name of the swing.plaf.ComponentUI class that defines the look and feel for this component.
55boolean getVerifyInputWhenFocusTarget()
Returns the value that indicates whether the input verifier for the current focus owner will be called before this component requests focus.
56VetoableChangeListener[] getVetoableChangeListeners()
Returns an array of all the vetoable change listeners registered on this component.
57Rectangle getVisibleRect()
Returns the Component's "visible rectangle" - the intersection of this component's visible rectangle, new Rectangle(0, 0, getWidth(), getHeight()) , and all of its ancestors' visible rectangles.
58int getWidth()
Returns the current width of this component.
59int getX()
Returns the current x coordinate of the component's origin.
60int getY()
Returns the current y coordinate of the component's origin.
61void grabFocus()
Requests that this Component get the input focus, and that this Component's top-level ancestor become the focused Window.
62boolean isDoubleBuffered()
Returns whether this component should use a buffer to paint.
63static boolean isLightweightComponent(Component c)
Returns true if this component is lightweight, that is, if it doesn't have a native window system peer.
64boolean isManagingFocus()
Deprecated.As of 1.4, replaced by Component.setFocusTraversalKeys(int, Set) and Container.setFocusCycleRoot(boolean).
65boolean isOpaque()
Returns true if this component is completely opaque.
66boolean isOptimizedDrawingEnabled()
Returns true if this component tiles its children -- that is, if it can guarantee that the children will not overlap.
67boolean isPaintingForPrint()
Returns true if the current painting operation on this component is part of a print operation.
68boolean isPaintingTile()
Returns true if the component is currently painting a tile.
69boolean isRequestFocusEnabled()
Returns true if this JComponent should get focus; otherwise returns false.
70boolean isValidateRoot()
If this method returns true, revalidate calls by descendants of this component will cause the entire tree beginning with this root to be validated.
71void paint(Graphics g)
Invoked by Swing to draw components.
72protected void paintBorder(Graphics g)
Paints the component's border.
73protected void paintChildren(Graphics g)
Paints this component's children.
74protected void paintComponent(Graphics g)
Calls the UI delegate's paint method, if the UI delegate is non-null.
75void paintImmediately(int x, int y, int w, int h)
Paints the specified region in this component and all of its descendants that overlap the region, immediately.
76void paintImmediately(Rectangle r)
Paints the specified region now.
77protected String paramString()
Returns a string representation of this JComponent.
78void print(Graphics g)
Invoke this method to print the component to the specified Graphics.
79void printAll(Graphics g)
Invoke this method to print the component.
80protected void printBorder(Graphics g)
Prints the component's border.
81protected void printChildren(Graphics g)
Prints this component's children.
82protected void printComponent(Graphics g)
This is invoked during a printing operation.
82protected void processComponentKeyEvent(KeyEvent e)
Processes any key events that the component itself recognizes.
84protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)
Invoked to process the key bindings for ks as the result of the KeyEvent e.
85protected void processKeyEvent(KeyEvent e)
Overrides processKeyEvent to process events.
86protected void processMouseEvent(MouseEvent e)
Processes mouse events occurring on this component by dispatching them to any registered MouseListener objects, refer to Component.processMouseEvent(MouseEvent) for a complete description of this method.
87protected void processMouseMotionEvent(MouseEvent e)
Processes mouse motion events, such as MouseEvent.MOUSE_DRAGGED.
88void putClientProperty(Object key, Object value)
Adds an arbitrary key/value "client property" to this component.
89void registerKeyboardAction(ActionListener anAction, KeyStroke aKeyStroke, int aCondition)
This method is now obsolete, please use a combination of getActionMap()
and getInputMap()
for similiar behavior.
90void registerKeyboardAction(ActionListener anAction, String aCommand, KeyStroke aKeyStroke, int aCondition)
This method is now obsolete, please use a combination of getActionMap()
and getInputMap()
for similiar behavior.
91void removeAncestorListener(AncestorListener listener)
Unregisters listener so that it will no longer receive AncestorEvents.
92void removeNotify()
Notifies this component that it no longer has a parent component.
93void removeVetoableChangeListener(VetoableChangeListener listener)
Removes a VetoableChangeListener from the listener list.
94void repaint(long tm, int x, int y, int width, int height)
Adds the specified region to the dirty region list if the component is showing.
95void repaint(Rectangle r)
Adds the specified region to the dirty region list if the component is showing.
96boolean requestDefaultFocus()
Deprecated.As of 1.4, replaced by FocusTraversalPolicy.getDefaultComponent(Container).requestFocus()
97void requestFocus()
Requests that this Component gets the input focus.
98boolean requestFocus(boolean temporary)
Requests that this Component gets the input focus.
99boolean requestFocusInWindow()
Requests that this Component gets the input focus.
100protected boolean requestFocusInWindow(boolean temporary)
Requests that this Component gets the input focus.
101void resetKeyboardActions()
Unregisters all the bindings in the first tier InputMaps and ActionMap.
102void reshape(int x, int y, int w, int h)
Deprecated.As of JDK 5, replaced by Component.setBounds(int, int, int, int).Moves and resizes this component.
103void revalidate()
Supports deferred automatic layout.
104void scrollRectToVisible(Rectangle aRect)
Forwards the scrollRectToVisible()
message to the JComponent's parent.
105void setActionMap(ActionMap am)
Sets the ActionMap to am.
106void setAlignmentX(float alignmentX)
Sets the the vertical alignment.
107void setAlignmentY(float alignmentY)
Sets the the horizontal alignment.
108void setAutoscrolls(boolean autoscrolls)
Sets the autoscrolls property.
109void setBackground(Color bg)
Sets the background color of this component.
110void setBorder(Border border)
Sets the border of this component.
111void setComponentPopupMenu(JPopupMenu popup)
Sets the JPopupMenu for this JComponent.
112void setDebugGraphicsOptions(int debugOptions)
Enables or disables diagnostic information about every graphics operation performed within the component or one of its children.
113static void setDefaultLocale(Locale l)
Sets the default locale used to initialize each JComponent's locale property upon creation.
114void setDoubleBuffered(boolean aFlag)
Sets whether this component should use a buffer to paint.
115void setEnabled(boolean enabled)
Sets whether or not this component is enabled.
116void setFocusTraversalKeys(int id, Set<? extends AWTKeyStroke> keystrokes)
Sets the focus traversal keys for a given traversal operation for this Component.
117void setFont(Font font)
Sets the font for this component.
118void setForeground(Color fg)
Sets the foreground color of this component.
119void setInheritsPopupMenu(boolean value)
Sets whether or not getComponentPopupMenu should delegate to the parent if this component does not have a JPopupMenu assigned to it.
120void setInputMap(int condition, InputMap map)
Sets the InputMap to use under the condition condition to map.
121void setInputVerifier(InputVerifier inputVerifier)
Sets the input verifier for this component.
122void setMaximumSize(Dimension maximumSize)
Sets the maximum size of this component to a constant value.
123void setMinimumSize(Dimension minimumSize)
Sets the minimum size of this component to a constant value.
124void setNextFocusableComponent(Component aComponent)
Deprecated. As of 1.4, replaced by FocusTraversalPolicy
125void setOpaque(boolean isOpaque)
If true the component paints every pixel within its bounds.
126void setPreferredSize(Dimension preferredSize)
Sets the preferred size of this component.
127void setRequestFocusEnabled(boolean requestFocusEnabled)
Provides a hint as to whether or not this JComponent should get focus.
128void setToolTipText(String text)
Registers the text to display in a tool tip.
129void setTransferHandler(TransferHandler newHandler)
Sets the transferHandler property, which is null if the component does not support data transfer operations.
130protected void setUI(ComponentUI newUI)
Sets the look and feel delegate for this component.
131void setVerifyInputWhenFocusTarget(boolean verifyInputWhenFocusTarget)
Sets the value to indicate whether input verifier for the current focus owner will be called before this component requests focus.
132void setVisible(boolean aFlag)
Makes the component visible or invisible.
133void unregisterKeyboardAction(KeyStroke aKeyStroke)
This method is now obsolete.
134void update(Graphics g)
Calls paint.
135void updateUI()
Resets the UI property to a value from the current look and feel.

Methods inherited

This class inherits methods from the following classes:

  • java.awt.Container

  • java.awt.Component

  • java.lang.Object



Advertisements
Advertisements