Updated March 17, 2023
Introduction on JComponent in Java
As we all are aware that almost all the Swing components like JButton, JPanel, JTable, etc., are the basic building blocks used in the development of Swing application. They extend the JComponent class, extending the Container class, which in turn extends the Container class of Java. Swing components carry the AWT functionality also with themselves as they extend the Container class. In this way, the hierarchy of the Swing classes is followed. Top-level components like JDialog and JFrame do not inherit the JComponent because they are the child of top-level containers.
Syntax:
There is no such syntax of JComponent in Java. There is only one default constructor of JComponent which any class can inherit. Below given is the basic declaration of JComponent:
public abstract class JComponent
extends Container
implements Serializable
JContainer is used in the way mentioned below:
public class Myclass extends JComponent{
public void method()
{ }
}
Types and Modifiers used for JComponent in Java
Below, the table shows the types and modifiers used for JComponent in Java:
Modifier or Type | Field | Description |
static String | TOOL_TIP_TEXT_KEY | It is also known as “value tip”, “flyover label” and is used to display when the cursor is over the component |
static int | UNDEFINED_CONDITION | Some of the APIs basically use this constant in order to mention the situations when no condition is specified/ defined |
static int | WHEN_FOCUSSED | This constant means that the command should be invoked when the component has the focus and is basically used for registerKeyboardAction |
static int | WHEN_ANCESTOR_OF_FOCUSED_COMPONENT | This constant is used in the situation of the focussed component. It is used for registerKeyboardAction, which means that the command will be invoked when it is itself a focussed component or the receiving component is the focus component’s ancestor. |
static int | WHEN_IN_FOCUSED_WINDOW | This constant is generally used for registerKeyboardAction. The command will be invoked if the receiving component is present on the currently focussed window or is itself a focussed component. |
protected ComponentUI | ui | This component is the delegate for the look and feel |
protected EventListenerList | listenerList | This component class is used to hold a complete list of event listeners. |
protected AccessibleContext | accessibleContext | With this JComponent, AccessibleContext is associated. |
Methods of JComponent in Java
Below given are some of the most commonly used methods of JComponent in Java:
Method Name | Modifier type | Description |
setBackground(Color bg) | void | This method is used to set the background of this component |
setForeground(Color fg) | void | This method is used to set the foreground of this component |
setMaximumSize(Dimension maxSize) | void | This method is used to set the maximum size of this component to the maximum size to the value given. |
setMinimumSize(Dimension minSize) | void | This method is used to set the minimum size of this component to the value given. |
addAncestorListener(AncestorListener listener) | void | This method is used to add an AncestorListener so that it will receive all the AncestorEvents when any of its Ancestors are moved or made visible/ invisible |
firePropertyChange(String propertyName, boolean oldValue, boolean newValue) | boolean | This method is used to report a bound property change for boolean properties |
getAccessibleContext() | AccessibleContext | This method is used to get the Accessible context that is associated with the JComponent. |
getAncestorListeners() | AncestorListener[] | This method is used to return an array of all the ancestor listeners who are registered with this component. |
getToolTipText() | String | This method is used to return the string of the tooltip, which has been set by the method setToolTipText() |
getWidth() | int | This method is used to get the current width of the component. |
getTopLevelAncestor() | Container | This method returns the top-level ancestor of this component. If the component is not yet added to any container, it returns null. |
isRequestFocusEnabled() | boolean | This method returns true if this Jcomponent should get focus and false if not. |
processMouseEvent(MouseEvent e) | protected void | This method processes any mouse events occurring on this component by dispatching them to any registered MouseListener objects. |
isLightweightComponent(Component c) | static boolean | This method returns true if this component is a lightweight component and false if not. |
getActionForKeyStroke(KeyStroke, keystroke) | ActionListener | This method basically returns the object that will perform an action for the given keystroke. |
getAccessibleContext() | AccessibleContext | This method is used to get the accessible context associated with this JComponent |
getBorder() | Border | This method returns the border of this component and null if there is no border set the component |
Program to implement JComponent in Java:
The below program implements the JComponent in Java:
Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class DrawCircle {
public static void main(String[] args) {
new DrawCircle();
}
public DrawCircle() {
// creating a new JFrame using new keyword
JFrame fr = new JFrame("This is my first JComponet Program");
// setting the layout of the above created frame
fr.setLayout(new BorderLayout());
// adding the Circle in the frame
fr.getContentPane().add(new Circle(100,100,20));
// We can also get the background of frame using fr.setBackground(Color.blue)
method
fr.pack();
fr.setLocationRelativeTo(null);
// setting visibility of frame
fr.setVisible(true);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setting the size of frame
fr.setSize(500, 300);
}
}
class Circle extends JComponent
{
private static final long serialVersionUID = 1L;
public Circle() { }
public Circle(int b, int c, int dia)
{
// setting the location of Circle in Frame
super();
this.setLocation(b, c);
this.setSize(dia, dia);
}
@Override
public void paint(Graphics g)
{
super.paint(g);
// setting the color of circle
g.setColor(Color.yellow);
g.fillOval(0, 0, 80, 80);
}
}
Output: Executing the above program results in the below-mentioned output.
Conclusion
The above description of the JComponent clearly explains what the JComponent it and the various methods and modifiers used in JComponent. JComponent is one of the most important topics when it comes to working with graphics. Since there are many methods used by JComponent used for different purposes. So before using them, it should be understood thoroughly by the programmer.
Recommended Articles
This is a guide to JComponent in Java. Here we discuss the Types and modified, methods and program implement of JComponent in Java. You can also go through our other related articles to learn more-