Java Swing Tutorial - Java Swing Component








Swing provides many components for us to use to build GUIs.

A Java Swing component inherits from javax.swing.JComponent class and it serves as the base class for all Swing components. JComponent is an abstract class.

JComponent

The JComponent class inherits from the java.awt.Container class, which inherits from the java.awt.Component class.

The JComponent class, as a base class for all Swing components, provides the following basic functionalities for all Swing components.

  • tool tips
  • pluggable look and feel
  • border
  • accessibility
  • double buffering
  • key binding
  • laying out the component




JComponent Methods

The following table lists some of the commonly used methods of the JComponent class that are available to be used in all Swing components.

  • Border getBorder()
    Returns the border of the component or null if the component has no border.
  • void setBorder(Border border)
    Sets the border for the component.
  • Object getClientProperty(Object key)
    Returns the value associated with the specified key. The value must have been set using the putClientProperty (Object key, Object value) method.
  • void putClientProperty(Object key, Object value)
    Adds an arbitrary key-value pair to the component.
  • Graphics getGraphics()
    Returns the graphics context object for the component, which can be used to draw on the component.
  • Dimension getMaximumSize()
    Dimension getMinimumSize()
    Dimension getPreferredSize()
    Dimension getSize(Dimension d)
    void setMaximumSize(Dimension d)
    void setMinimumSize(Dimension d)
    void setPreferredSize(Dimension d)
    void setSize(Dimension d)
    void setSize(int width, int height)
    Gets/sets the maximum, minimum, preferred, and actual size of the component.
  • String getToolTipText()
    Returns the tool tip text for this component.
  • void setToolTipText(String text)
    Sets the tool tip text.
  • boolean isDoubleBuffered()
    Returns true if the component uses double buffering. Otherwise, it returns false.
  • void setDoubleBuffered(boolean db)
    Sets if the component should use double buffering to paint or not.
  • boolean isFocusable()
    Returns true if the component can gain focus. Otherwise, it returns false.
  • void setFocusable(boolean focusable)
    Sets if the component can gain focus or not.
  • boolean isVisible()
    Returns true if the component is visible. Otherwise, it returns false.
  • void setVisible(boolean v)
    Sets the component visible or invisible.
  • boolean isEnabled()
    Returns true if the component is enabled. Otherwise, it returns false.
  • void setEnabled(boolean e)
    Enables or disables the component.
  • boolean requestFocus(boolean temporary)
    boolean requestFocusInWindow()
    boolean requestFocusInWindow(boolean temporary)
    Both requestFocus() and requestFocusInWindow() methods request that the component should get the input focus.
    We should use the requestFocusInWindow() method instead of the requestFocus() method because its behavior is consistent across all platforms.
  • boolean isOpaque()
    Returns true if the JComponent is opaque. Otherwise, it returns false.
  • void setOpaque(boolean opaque)
    Sets the opacity of the JComponent.




JComponent Event

The following table list some Commonly Used Events Available for All Swing Components.

  • ComponentEvent/ComponentListener
    The event occurs when a component's visibility, size, or location is changed.