The Component class is the superclass of all components. A component class can be linked with a page, components of web applications. Component clearly shows that is the graphical representation of an Object.
Important methods of Component Class:
- public void add(Component c): This method inserts a component into a Container by taking a component as a parameter.
- public void setSize(int width, int height): This method sets the size of the component by taking height and width as parameters.
- public void setLayout(LayoutManager lm): This method sets the layout to set the components in a particular manner by taking LayoutManager as a parameter.
- public void setVisible(boolean status): This method sets the visibility of a component to visible or not. If it sets to true then the component will be visible in the output else if it sets to false or not defined component won't be visible in the output.
Note: LayoutManager helps us to give the positioning and size of components that should be visible.
Types of Components in Component Class
The components in a component class are as follows :
- Container
- Button
- Label
- Checkbox
- Choice
- List
All these components are present in java.awt package. We can import each of the components individually i.e., import java.awt.Button, import java.awt.Container etc.
Note: If we want to import all components at a time we can do that by importing like import java.awt.*
The hierarchical structure of the above-listed components is below:
Hierarchical Structure of Components
So let us see each component in detail.
1. Container
The Container is a component that will be used to extend other components such as window, panel, Frame, Dialog, and Applet as shown in the above diagram.
- Window: The Window is a Container that doesn't include borders and a menu bar. We must use another window, frames, and dialogue box to create a Window. Creating an instance is the way to create a Window Container.
- Panel: The Panel is also a Container that doesn't include a title bar, menu, or border. It is a container that holds components like buttons, textfield, etc. Creating an instance is the way to create a Panel Container and can add components.
- Frame: The Frame is a container used while creating an AWT application. It can have components like title bar, menu bars, borders and also buttons, scroll bar, etc.
- Dialog: The Dialog box is a container that will display the message that we want to display on the screen.
2. Button
A button is a labeled component when clicked performs an event. It is created by the Button class. When clicked it performs some action by sending an instance of ActionEvent by AWT. ActionEvent calls processEvent on the button and it receives all the events and performs action by calling the processActionEvent method of its own. To do such things it needs to implement ActionListener. The Declaration of Button Class will be
public class Button extends Component implements Accessible
It contains 2 constructors:
- Button() : This constructor will create a button with no label
- Button(String label) : This constructor creates a button with label value as a value when we creates an object
Example:
Java
import java.awt.*;
// Driver Class
class SubmitButton extends Frame {
// main function
public static void main(String[] args)
{
// create instance of frame with label
Frame frame = new Frame("Submit form");
// create instance of button with label
Button button = new Button("Submit");
// set the position for the button in frame
button.setBounds(40, 130, 70, 20);
// adding button to the frame
frame.add(button);
// setting size for the frame
frame.setSize(500, 500);
// setting layout for the frame
frame.setLayout(null);
// visibility of frame to display the output\
// without this output will be blank
frame.setVisible(true);
}
}
We can run it by the following commands:

Output:

3. Label
It is used to show text in the Container. It will displays text in the form of READ-ONLY, which cannot be changed by the user directly. We need to create an instance of Label Class to create a Label. The Declaration of Label Class will be
public class Label extends Component implements Accessible
It has 3 constructors:
- Label() : Creates an Empty Label.
- Label(String labelname) : Creates a Label with labelname as parameter value.
- Label(String labelname, int align) : Creates a Label with labelname as parameter value and proper alignments.
Note: Align parameter aligns the text in proper alignment and it has 3 types of alignment.
- LEFT: specifies that text should be aligned to left.
- RIGHT: specifies that text should be aligned to right.
- CENTER specifies that text should be aligned to the center.
Example:
Java
import java.awt.*;
public class ShowLabelText extends Frame {
public static void main(String args[])
{
// creating objects for Frame and Label class
Frame frame = new Frame("Label Text");
// Creating label One
Label label1 = new Label("Label One", Label.LEFT);
// Creating label Two
Label label2 = new Label("Label Two", Label.RIGHT);
// set the location of label in px
label1.setBounds(50, 100, 100, 50);
label2.setBounds(50, 150, 100, 50);
// adding labels to the frame
frame.add(label1);
frame.add(label2);
// setting size, layout
// and visibility of frame
frame.setSize(500, 500);
frame.setLayout(null);
frame.setVisible(true);
}
}
We can run it by the following commands:

Output:

4. Checkbox
It is used to create a Checkbox in the Container. It can get a value either true or false by checking and unchecking the checkbox.
- checked - returns true
- unchecked - returns false
It can be created by creating an instance of Checkbox. The Declaration of Label Class will be
public class Checkbox extends Component implements ItemSelectable, Accessible
It has 5 constructors:
- Checkbox() : Creates a checkbox with empty label
- Checkbox(String checkboxlabel) : Creates a Checkbox with checkboxlabel as parameter value.
- Checkbox(String checkboxlabel, boolean status) : Creates a Checkbox with checkboxlabel as parameter value and sets the status either true or false.
- Checkbox(String checkboxlabel, boolean status, CheckboxGroup cbgroup) : Creates a Checkbox with checkboxlabel as parameter value and sets the status to the specified checkbox group.
- Checkbox(String checkboxlabel, CheckboxGroup cbgroup, boolean status) : Creates a Checkbox with checkboxlabel as parameter value for the specified cbgroup and sets the status.
Example 1:
Java
// importing AWT class
import java.awt.*;
public class CourseCheck {
// main method
public static void main(String args[])
{
// creating the frame with the label
Frame frame = new Frame("Courses");
// creating checkbox java
Checkbox java = new Checkbox("Java");
// setting location of checkbox in frame
java.setBounds(100, 100, 50, 50);
// creating checkbox python with status as true
Checkbox python = new Checkbox("Python", true);
// setting location of checkbox in frame
python.setBounds(100, 150, 50, 50);
// adding checkboxes to frame
frame.add(java);
frame.add(python);
// setting size, layout and
// visibility of frame
frame.setSize(400, 400);
frame.setLayout(null);
frame.setVisible(true);
}
}
We can run it by the following commands:

Output:

In the above output, we can select both options.
Example 2:
Java
// importing AWT class
import java.awt.*;
public class GenderCheck {
// main method
public static void main(String args[])
{
// creating the frame with the label
Frame frame = new Frame("Gender");
// creating a CheckboxGroup
CheckboxGroup cbgroup = new CheckboxGroup();
// creating checkbox male with
// status as true for cbgroup
Checkbox male = new Checkbox("Male", cbgroup, true);
male.setBounds(100, 100, 50, 50);
// creating checkbox female with
// status as false for cbgroup
Checkbox female = new Checkbox("Female", cbgroup, false);
// setting location of checkbox in frame
female.setBounds(100, 150, 50, 50);
// adding checkboxes to frame
frame.add(male);
frame.add(female);
// setting size, layout
// and visibility of frame
frame.setSize(400, 400);
frame.setLayout(null);
frame.setVisible(true);
}
}
We can run it by using commands:

Output:

In the above output, we can select any one of the options as it works as a radio button.
5. Choice
It is used to show the popup menu to select any item from the menu items. The selected choice will be shown at the top of the menu bar. We need to create an instance of Choice Class to create a Choice. The Declaration of Choice Class will be
public class Choice extends Component implements ItemSelectable, Accessible
It has 1 constructor:
Choice() : Creates a new Choice menu of items
Example:
Java
import java.awt.*;
public class SelectItems {
// main method
public static void main(String args[])
{
// creating a Frame
Frame frame = new Frame();
// creating a choice component
Choice choice = new Choice();
// setting the bounds of choice menu
choice.setBounds(70, 70, 75, 75);
// adding items to the choice menu
choice.add("--select--");
choice.add("Shampoo");
choice.add("Eggs");
choice.add("Bottles");
// adding choice menu to frame
frame.add(choice);
// setting size, layout
// and visibility of frame
frame.setSize(300, 300);
frame.setLayout(null);
frame.setVisible(true);
}
}
We can run it by the following commands:

Output:

6. List
The List Object creates a list of items in which we can choose one or multiple items at a time. We need to create an instance of List Class to create a List. The Declaration of Label Class will be
public class List extends Component implements ItemSelectable, Accessible
It has 3 constructors:
- List() : Creates a new Scrolling List
- List(int noofrows) : Creates a new Scrolling List which displays the list of items with given no. of rows visible with parameter noofrows.
- List(int noofrows, boolean multi) : Creates a new Scrolling list which displays the list of items with given no. of rows visible and allows to select multiple items at a time.
Example:
Java
import java.awt.*;
public class SelectList {
// main method
public static void main(String args[])
{
// creating frame1
Frame frame1 = new Frame();
// creating list1 with 5 rows
List list1 = new List(5);
// setting the position of list component
list1.setBounds(100, 100, 75, 75);
// adding list items to the list1
list1.add("Shampoo");
list1.add("Conditioner");
list1.add("Eggs");
list1.add("Bottles");
list1.add("Butter");
// adding the list1 to frame1
frame1.add(list1);
// setting size, layout
// and visibility of frame1
frame1.setSize(400, 400);
frame1.setLayout(null);
frame1.setVisible(true);
// creating frame2
Frame frame2 = new Frame();
// creating list2 with 5 rows
// and multi select items as true
List list2 = new List(5, true);
// setting the position of list component
list2.setBounds(100, 100, 75, 75);
// adding list items to the list2
list2.add("Shampoo");
list2.add("Conditioner");
list2.add("Eggs");
list2.add("Bottles");
list2.add("Butter");
// adding the list2 to frame2
frame2.add(list2);
// setting size, layout
// and visibility of frame2
frame2.setSize(400, 400);
frame2.setLayout(null);
frame2.setVisible(true);
}
}
We can run it by the following commands:

Output 1:

In this output List, we can select any one item at a time.
Output 2:

In this output List, we can select multiple items at a time. These are all the components present in the Component class and about component class.
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Basics
Introduction to JavaJava is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android
4 min read
Java Programming BasicsJava is one of the most popular and widely used programming language and platform. A platform is an environment that helps to develop and run programs written in any programming language. Java is fast, reliable and secure. From desktop to web applications, scientific supercomputers to gaming console
4 min read
Java MethodsJava Methods are blocks of code that perform a specific task. A method allows us to reuse code, improving both efficiency and organization. All methods in Java must belong to a class. Methods are similar to functions and expose the behavior of objects.Example: Java program to demonstrate how to crea
7 min read
Access Modifiers in JavaIn Java, access modifiers are essential tools that define how the members of a class, like variables, methods, and even the class itself, can be accessed from other parts of our program. They are an important part of building secure and modular code when designing large applications. In this article
6 min read
Arrays in JavaIn Java, an array is an important linear data structure that allows us to store multiple values of the same type. Arrays in Java are objects, like all other objects in Java, arrays implicitly inherit from the java.lang.Object class. This allows you to invoke methods defined in Object (such as toStri
9 min read
Java StringsIn Java, a String is the type of object that can store a sequence of characters enclosed by double quotes and every character is stored in 16 bits, i.e., using UTF 16-bit encoding. A string acts the same as an array of characters. Java provides a robust and flexible API for handling strings, allowin
8 min read
Regular Expressions in JavaIn Java, Regular Expressions or Regex (in short) in Java is an API for defining String patterns that can be used for searching, manipulating, and editing a string in Java. Email validation and passwords are a few areas of strings where Regex is widely used to define the constraints. Regular Expressi
7 min read
OOPs & Interfaces
Classes and Objects in JavaIn Java, classes and objects are basic concepts of Object Oriented Programming (OOPs) that are used to represent real-world concepts and entities. A class is a template to create objects having similar properties and behavior, or in other words, we can say that a class is a blueprint for objects.An
10 min read
Java ConstructorsIn Java, constructors play an important role in object creation. A constructor is a special block of code that is called when an object is created. Its main job is to initialize the object, to set up its internal state, or to assign default values to its attributes. This process happens automaticall
10 min read
Java OOP(Object Oriented Programming) ConceptsBefore Object-Oriented Programming (OOPs), most programs used a procedural approach, where the focus was on writing step-by-step functions. This made it harder to manage and reuse code in large applications.To overcome these limitations, Object-Oriented Programming was introduced. Java is built arou
10 min read
Java PackagesPackages in Java are a mechanism that encapsulates a group of classes, sub-packages and interfaces. Packages are used for: Prevent naming conflicts by allowing classes with the same name to exist in different packages, like college.staff.cse.Employee and college.staff.ee.Employee.They make it easier
8 min read
Java InterfaceAn Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
11 min read
Collections
Exception Handling
Java Exception HandlingException handling in Java is an effective mechanism for managing runtime errors to ensure the application's regular flow is maintained. Some Common examples of exceptions include ClassNotFoundException, IOException, SQLException, RemoteException, etc. By handling these exceptions, Java enables deve
8 min read
Java Try Catch BlockA try-catch block in Java is a mechanism to handle exceptions. This make sure that the application continues to run even if an error occurs. The code inside the try block is executed, and if any exception occurs, it is then caught by the catch block.Example: Here, we are going to handle the Arithmet
4 min read
Java final, finally and finalizeIn Java, the final, finally, and finalize keywords play an important role in exception handling. The main difference between final, finally, and finalize is listed below:final: The final is the keyword that can be used for immutability and restrictions in variables, methods, and classes.finally: The
4 min read
Chained Exceptions in JavaChained Exceptions in Java allow associating one exception with another, i.e. one exception describes the cause of another exception. For example, consider a situation in which a method throws an ArithmeticException because of an attempt to divide by zero.But the root cause of the error was an I/O f
3 min read
Null Pointer Exception in JavaA NullPointerException in Java is a RuntimeException. It occurs when a program attempts to use an object reference that has the null value. In Java, "null" is a special value that can be assigned to object references to indicate the absence of a value.Reasons for Null Pointer ExceptionA NullPointerE
5 min read
Exception Handling with Method Overriding in JavaIn Java, an exception is an unwanted or unexpected event that occurs during a program's execution, i.e., at runtime, and disrupts the normal flow of the programâs instructions. Exception handling in Java handles runtime errors and helps maintain the program's normal flow by using constructs like try
5 min read
Java Advanced
Java Multithreading TutorialThreads are the backbone of multithreading. We are living in the real world which in itself is caught on the web surrounded by lots of applications. With the advancement in technologies, we cannot achieve the speed required to run them simultaneously unless we introduce the concept of multi-tasking
15+ min read
Synchronization in JavaIn multithreading, synchronization is important to make sure multiple threads safely work on shared resources. Without synchronization, data can become inconsistent or corrupted if multiple threads access and modify shared variables at the same time. In Java, it is a mechanism that ensures that only
10 min read
File Handling in JavaIn Java, with the help of File Class, we can work with files. This File Class is inside the java.io package. The File class can be used to create an object of the class and then specifying the name of the file.Why File Handling is Required?File Handling is an integral part of any programming languag
6 min read
Java Method ReferencesIn Java, a method is a collection of statements that perform some specific task and return the result to the caller. A method reference is the shorthand syntax for a lambda expression that contains just one method call. In general, one does not have to pass arguments to method references.Why Use Met
9 min read
Java 8 Stream TutorialJava 8 introduces Stream, which is a new abstract layer, and some new additional packages in Java 8 called java.util.stream. A Stream is a sequence of components that can be processed sequentially. These packages include classes, interfaces, and enum to allow functional-style operations on the eleme
15+ min read
Java NetworkingWhen computing devices such as laptops, desktops, servers, smartphones, and tablets and an eternally-expanding arrangement of IoT gadgets such as cameras, door locks, doorbells, refrigerators, audio/visual systems, thermostats, and various sensors are sharing information and data with each other is
15+ min read
JDBC TutorialJDBC stands for Java Database Connectivity. JDBC is a Java API or tool used in Java applications to interact with the database. It is a specification from Sun Microsystems that provides APIs for Java applications to communicate with different databases. Interfaces and Classes for JDBC API comes unde
12 min read
Java Memory ManagementJava memory management is the process by which the Java Virtual Machine (JVM) automatically handles the allocation and deallocation of memory. It uses a garbage collector to reclaim memory by removing unused objects, eliminating the need for manual memory managementJVM Memory StructureJVM defines va
4 min read
Garbage Collection in JavaGarbage collection in Java is an automatic memory management process that helps Java programs run efficiently. Java programs compile to bytecode that can be run on a Java Virtual Machine (JVM). When Java programs run on the JVM, objects in the heap are created, which is a portion of memory dedicated
7 min read
Memory Leaks in JavaIn programming, a memory leak happens when a program keeps using memory but does not give it back when it's done. It simply means the program slowly uses more and more memory, which can make things slow and even stop working. Working of Memory Management in JavaJava has automatic garbage collection,
3 min read
Practice Java
Java Interview Questions and AnswersJava is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java Programs - Java Programming ExamplesIn this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Exercises - Basic to Advanced Java Practice Programs with SolutionsLooking for Java exercises to test your Java skills, then explore our topic-wise Java practice exercises? Here you will get 25 plus practice problems that help to upscale your Java skills. As we know Java is one of the most popular languages because of its robust and secure nature. But, programmers
7 min read
Java Quiz | Level Up Your Java SkillsThe best way to scale up your coding skills is by practicing the exercise. And if you are a Java programmer looking to test your Java skills and knowledge? Then, this Java quiz is designed to challenge your understanding of Java programming concepts and assess your excellence in the language. In thi
1 min read
Top 50 Java Project Ideas For Beginners and Advanced [Update 2025]Java is one of the most popular and versatile programming languages, known for its reliability, security, and platform independence. Developed by James Gosling in 1982, Java is widely used across industries like big data, mobile development, finance, and e-commerce.Building Java projects is an excel
15+ min read