Table of contents
1.
Introduction
2.
JComponent Class Declaration
3.
Fields of JComponent
4.
Class Constructor of JComponent
5.
Methods of JComponent
6.
Methods Inherited
7.
Example Of JComponent
8.
Frequently Asked Questions
8.1.
What is JComponent used for?
8.2.
What is the use of the method “getBorder()”?
8.3.
What is the use of the method “getAccessibleContext()”?
8.4.
What is the modifier type of “getAncestorListeners()” method?
8.5.
What is the modifier type of the “getWidth()” method?
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

JComponent in Java

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JComponent is a part of Java Swing. It is a primary-class consisting of all Swing components, excluding top-level containers. All swing components whose names start with "J" come under the JComponent class except JFrame and JDialog because they are inherent to top-level containers and don’t come under JComponent. Some commonly used swing components that come under JComponent class are JButton, JScrollPane, JPanel, JTable, JColorChooser, JProgressBar, etc.

JComponent in Java

JComponent extends the container class of Java, which provides more features to the containers. The Container class has features to add components to the container.

JComponent Class Declaration

JComponent class is declared by the given method:

public abstract class JComponent
 extends Container
 implements Serializable

Fields of JComponent

Fields are the JComponent class  that affects the class's behavior at runtime. A class can be declared with any number of fields. Some of the commonly used fields are described below:
 

  • static String TOOL_TIP_TEXT_KEY:  This field is known as “tooltip” or “value tip”. It displays information when a user hovers the mouse or focuses by keyboard over the component.
     
  • static int UNDEFINED_CONDITION: “static int UNDEFINED_CONDITION” is a constant used by some of the API’s of JComponent, which tells that no condition is defined.
     
  • static int WHEN_FOCUSSED: static int WHEN_FOCUSSED is a constant used for registerKeyboardAction. It means that the command should be called for the component which has focus. 
     
  • static int WHEN_ANCESTOR_OF_FOCUSED_COMPONENT: ” static int WHEN_ANCESTOR_OF_FOCUSED_COMPONENT” is a constant used for registerKeyboardAction means the command should be called when the receiving component is an ancestor of the intent component or is itself the intended component.
     
  • static int WHEN_IN_FOCUSED_WINDOW: It is a constant also used for registerKeyboardAction means the command will be called when the receiving component is present on the current window is in focus or is itself an intended component.

 

  • protected ComponentUI ui: This component is the representative for Look & Feel.
     
  • protected EventListenerList listenerList: This component class maintains a complete list of the event listeners. 
     
  • protected AccessibleContext accessibleContext: This JComponent associates the AccessibleContext.

Class Constructor of JComponent

A constructor is a function that creates the objects of a class. In simple words, a constructor is a function that constructs all the features of a class. The constructor of JComponent is Described below:

JComponent(): It is the default constructor of JComponent.

Methods of JComponent

A method is a code that executes only when it is called. We can also pass some data through it, which is known as parameters. Every method performs some specific work. Some of the commonly used methods of JComponent are discussed below:

 

Method Name 

Method Type 

Description

setBackground(Color col)

void

This method sets the background color of JComponent.

setForeground(Color fg)

void

This method sets the foreground color of JComponent.

setMaximumSize(Dimension maxmSize)

void

This method sets the maximum size of JComponent to the given constant value.

setMinimumSize(Dimension minmSize)

void

This method sets the minimum size of JComponent to the given constant value.

setVisible(boolean cFlag)

void

This method makes the components visible or invisible.

setFont(Font ft)

void

This method sets the font for JComponent.

setActionMap(ActionMap bm)

void

This method sets ActionMap bm.

setUI(ComponentUI newJUI)

Protected void

This method sets the look and feel representative for JComponent.

getToolTipText(MouseEvent evt)

String

This method returns the string which is used as a tooltip for the event.

getTopLevelAncestor()

Container

This method returns the top-level ancestor of JComponent (either the containing Window or Applet), or null if this component has not been added to any container.

TransferHandler

getTransferHandler()

This method receives the transferHandler property.

 

Methods Inherited

JComponent inherits properties  of some classes, which are given below:

  • java.awt.Container

 

  • java.awt.Component

 

  • java.lang.Object

Example Of JComponent

In this program we will design a black circle: 

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class DesignCircle
 {
    public static void main(String[] args) 
{
      new DesignCircle();
}
    public DesignCircle()
 {
      JFrame frm = new JFrame(); // creating a new JFrame using new keyword
      frm.setLayout(new BorderLayout()); // setting the layout of the above created frame
      frm.getContentPane().add(new Circle(150,150,30)); // adding the Circle in the frame method
      frm.pack();
      frm.setLocationRelativeTo(null);
      frm.setVisible(true); // setting visibility of frame
      frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frm.setSize(300, 500); // setting the size of frame

}
}
class Circle extends JComponent
{
   private static final long serialVersionUID = 1L;
   public Circle() { }
   public Circle(int x, int y, int dim)
{
      super();
      this.setLocation(x, y);
      this.setSize(dim, dim);
}
@Override
public void paint(Graphics z)
{
      super.paint(z);
      z.setColor(Color.black); // setting the color of circle
      z.fillOval(40, 40, 200, 200);
}
}
You can also try this code with Online Java Compiler
Run Code

 

OUTPUT

In this program with the help of JComponent we designed a black circle whose screenshot is given below:

Also check out swing components in java

Frequently Asked Questions

What is JComponent used for?

JComponent is used for graphics designing.

What is the use of the method “getBorder()”?

The “getBorder()” method returns the border of the JComponent, and it returns null if there is no border set to the JComponent.

What is the use of the method “getAccessibleContext()”?

The “getAccessibleContext” method obtains the accessible context associated with this JComponent.

What is the modifier type of “getAncestorListeners()” method?

The Modified type of “getAncestorListeners()” method is AncestorListener[].

What is the modifier type of the “getWidth()” method?

The Modifier type of the “getWidth()” method is int.

Conclusion

In this Blog, we Know about What is JComponent, the methods of JComponent, the modifiers of JComponent, and its example. JComponent plays a vital role in the field of graphics. JComponent uses many methods and components, so it is important to learn about it.

Also read,

 

Refer to our guided paths on Coding Ninjas Studio to learn more about C programming, DSA, JavaScript, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass