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.