Component Class in Java
Last Updated :
17 Nov, 2023
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.*;
class SubmitButton extends Frame {
public static void main(String[] args)
{
Frame frame = new Frame( "Submit form" );
Button button = new Button( "Submit" );
button.setBounds( 40 , 130 , 70 , 20 );
frame.add(button);
frame.setSize( 500 , 500 );
frame.setLayout( null );
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[])
{
Frame frame = new Frame( "Label Text" );
Label label1 = new Label( "Label One" , Label.LEFT);
Label label2 = new Label( "Label Two" , Label.RIGHT);
label1.setBounds( 50 , 100 , 100 , 50 );
label2.setBounds( 50 , 150 , 100 , 50 );
frame.add(label1);
frame.add(label2);
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
import java.awt.*;
public class CourseCheck {
public static void main(String args[])
{
Frame frame = new Frame( "Courses" );
Checkbox java = new Checkbox( "Java" );
java.setBounds( 100 , 100 , 50 , 50 );
Checkbox python = new Checkbox( "Python" , true );
python.setBounds( 100 , 150 , 50 , 50 );
frame.add(java);
frame.add(python);
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
import java.awt.*;
public class GenderCheck {
public static void main(String args[])
{
Frame frame = new Frame( "Gender" );
CheckboxGroup cbgroup = new CheckboxGroup();
Checkbox male = new Checkbox( "Male" , cbgroup, true );
male.setBounds( 100 , 100 , 50 , 50 );
Checkbox female = new Checkbox( "Female" , cbgroup, false );
female.setBounds( 100 , 150 , 50 , 50 );
frame.add(male);
frame.add(female);
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 {
public static void main(String args[])
{
Frame frame = new Frame();
Choice choice = new Choice();
choice.setBounds( 70 , 70 , 75 , 75 );
choice.add( "--select--" );
choice.add( "Shampoo" );
choice.add( "Eggs" );
choice.add( "Bottles" );
frame.add(choice);
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 {
public static void main(String args[])
{
Frame frame1 = new Frame();
List list1 = new List( 5 );
list1.setBounds( 100 , 100 , 75 , 75 );
list1.add( "Shampoo" );
list1.add( "Conditioner" );
list1.add( "Eggs" );
list1.add( "Bottles" );
list1.add( "Butter" );
frame1.add(list1);
frame1.setSize( 400 , 400 );
frame1.setLayout( null );
frame1.setVisible( true );
Frame frame2 = new Frame();
List list2 = new List( 5 , true );
list2.setBounds( 100 , 100 , 75 , 75 );
list2.add( "Shampoo" );
list2.add( "Conditioner" );
list2.add( "Eggs" );
list2.add( "Bottles" );
list2.add( "Butter" );
frame2.add(list2);
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.
Frequently Asked Questions
1. What are components in Java?
Components are the interactive objects with the graphical representation that can be displayed on the screen.
2.Which components cannot be added to a frame?
Some components in Java Swing can’t be directly added to the frame. Some of the components are mentioned below:
- MenuBar
- Scrollbar
- Dialog
- Applet
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...