Table of contents
1.
Introduction
2.
Important Methods of Component Class
3.
Types of Components in Component Class
3.1.
1. Container
3.1.1.
Example:
3.2.
2. Button
3.2.1.
Example:
3.3.
3. Label
3.3.1.
Example:
3.4.
4. Checkbox
3.4.1.
Example:
3.5.
5. Choice
3.5.1.
Example:
3.6.
6. List
3.6.1.
Example:
4.
Frequently Asked Questions
4.1.
What is the use of the Component class in Java?
4.2.
How is a button different from a label in Java?
4.3.
What is the difference between a Checkbox and a Choice in Java?
5.
Conclusion
Last Updated: Feb 3, 2025
Easy

Component Class in Java

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The Component class in Java is a fundamental part of the Abstract Window Toolkit (AWT), which is used for building graphical user interfaces (GUIs). It serves as the base class for all AWT components, such as buttons, text fields, and labels. The Component class provides essential functionalities like event handling, drawing, and user interaction.

Component Class in Java

In this article, we will learn about the Component class in Java, its key methods, and how it is used to create interactive GUI applications. We will also discuss examples to understand how different components inherit from this class and function within a graphical interface.

Important Methods of Component Class

The Component class provides several methods that allow developers to control and manipulate GUI components. Here are some of the key methods:

  1. setSize(int width, int height) - Sets the size of the component.
     
  2. setBackground(Color color) - Changes the background color of the component.
     
  3. setForeground(Color color) - Sets the text color of the component.
     
  4. setVisible(boolean visible) - Makes the component visible or hidden.
     
  5. setEnabled(boolean enabled) - Enables or disables the component.
     
  6. addKeyListener(KeyListener listener) - Attaches a key event listener to the component.
     
  7. addMouseListener(MouseListener listener) - Adds a mouse event listener.
     
  8. getWidth() and getHeight() - Retrieves the width and height of the component.
     
  9. repaint() - Refreshes the component to reflect changes.
     
  10. update(Graphics g) - Updates the component’s display.
     

Each of these methods plays a crucial role in designing an interactive GUI in Java.

Types of Components in Component Class

The Component class in Java has several types, which include:

1. Container

Container is a component that can hold other components, such as buttons and labels. It acts as a parent for other GUI elements.

Example:

import java.awt.*;
import javax.swing.*;
public class ContainerExample {
    public static void main(String[] args) {
        Frame frame = new Frame("Container Example");
        Panel panel = new Panel();
        panel.add(new Button("Click Me"));
        frame.add(panel);
        frame.setSize(300, 200);
        frame.setVisible(true);
    }
}


Output: 

A frame with a button inside a panel.

2. Button

Button is a clickable component that triggers an action when pressed.

Example:

import java.awt.*;
import java.awt.event.*;
public class ButtonExample {
    public static void main(String[] args) {
        Frame frame = new Frame("Button Example");
        Button button = new Button("Click Me");
        button.setBounds(50, 100, 80, 30);
        
        button.addActionListener(e -> System.out.println("Button Clicked"));
        
        frame.add(button);
        frame.setSize(300, 200);
        frame.setLayout(null);
        frame.setVisible(true);
    }
}


Output: 

A button that prints "Button Clicked" when pressed.

3. Label

Label is a component used to display text that the user cannot modify.

Example:

import java.awt.*;
public class LabelExample {
    public static void main(String[] args) {
        Frame frame = new Frame("Label Example");
        Label label = new Label("Hello, World!");
        label.setBounds(50, 100, 100, 30);
        frame.add(label);
        frame.setSize(300, 200);
        frame.setLayout(null);
        frame.setVisible(true);
    }
}


Output: 

A label displaying "Hello, World!".

4. Checkbox

Checkbox allows users to select or deselect an option.

Example:

import java.awt.*;
public class CheckboxExample {
    public static void main(String[] args) {
        Frame frame = new Frame("Checkbox Example");
        Checkbox checkbox = new Checkbox("Accept Terms");
        checkbox.setBounds(50, 100, 150, 30);
        frame.add(checkbox);
        frame.setSize(300, 200);
        frame.setLayout(null);
        frame.setVisible(true);
    }
}


Output: 

A checkbox labeled "Accept Terms".

5. Choice

Choice (dropdown menu) allows users to select an option from a list.

Example:

import java.awt.*;
public class ChoiceExample {
    public static void main(String[] args) {
        Frame frame = new Frame("Choice Example");
        Choice choice = new Choice();
        choice.add("Option 1");
        choice.add("Option 2");
        choice.add("Option 3");
        choice.setBounds(50, 100, 100, 30);
        frame.add(choice);
        frame.setSize(300, 200);
        frame.setLayout(null);
        frame.setVisible(true);
    }
}

Output: 

A dropdown menu with three options.

6. List

List component displays multiple selectable items.

Example:

import java.awt.*;
public class ListExample {
    public static void main(String[] args) {
        Frame frame = new Frame("List Example");
        List list = new List();
        list.add("Item 1");
        list.add("Item 2");
        list.add("Item 3");
        list.setBounds(50, 100, 100, 70);
        frame.add(list);
        frame.setSize(300, 200);
        frame.setLayout(null);
        frame.setVisible(true);
    }
}

Output: 

A list containing "Item 1", "Item 2", and "Item 3".

Frequently Asked Questions

What is the use of the Component class in Java?

The Component class in Java is the base class for all GUI components in AWT, such as buttons, labels, and checkboxes. It provides methods for handling events, layout management, and rendering.

How is a button different from a label in Java?

A button is an interactive component that triggers an action when clicked, whereas a label is a non-editable text display component.

What is the difference between a Checkbox and a Choice in Java?

A Checkbox allows selecting multiple options, while a Choice (dropdown) lets the user pick only one option from a predefined list.

Conclusion

In this article, we learned about the Component class in Java, its role in the AWT framework, and its key properties and methods. It serves as the foundation for creating UI elements and handling events. Understanding this class helps developers build interactive and efficient GUI applications, enabling better customization and enhanced functionality in Java-based applications.

Live masterclass