Java Swing Components and Containers
A component is an independent visual control and Java Swing Framework contains a large set of these components which provide rich functionalities and allow high level of customization. They all are derived from JComponent class. All these components are lightweight components. This class provides some common functionality like pluggable look and feel, support for accessibility, drag and drop, layout, etc.
A container holds a group of components. It provides a space where a component can be managed and displayed. Containers are of two types:
- Top level Containers
- It inherits Component and Container of AWT.
- It cannot be contained within other containers.
- Heavyweight.
- Example: JFrame, JDialog, JApplet
- Lightweight Containers
- It inherits JComponent class.
- It is a general purpose container.
- It can be used to organize related components together.
- Example: JPanel
Swing JButton
JButton class provides functionality of a button. It is used to create button component. JButton class has three constuctors,
Â
Example of JButton
In this example, we are creating two buttons using Jbutton class and adding them into Jframe container.
JTextField
JTextField is used for taking input of single line of text. It is most widely used text component. It has three constructors,
cols represent the number of columns in text field.
Example using JTextField
In this example, we are creating text field using JtextField class and adding into the jframe container.
Â
JCheckBox
The JcheckBox class is used to create chekbox in swing framework. In this example, we are creating three checkboxes to get user response.
using JCheckBox
JRadioButton
Radio button is a group of related button in which only one can be selected. JRadioButton class is used to create a radio button in Frames. Following is the constructor for JRadioButton,
Example using JRadioButton
To create radio button in swing, we used jradiobutton class. It is used to get single user response at a time.
JComboBox
Combo box is a combination of text fields and drop-down list.JComboBox component is used to create a combo box in Swing. Following is the constructor for JComboBox,
Example using JComboBox
Lets create an example to add combobox to the jframe . Combo box is used to create a drop-down menu. See the below example.
A program to change background color of a frame (Using Action Event)
Output:
JLabel
In Java, Swingtoolkit contains a JLabel Class. It is under package javax.swing.JLabel class. It is used for placing text in a box. Only Single line text is allowed and the text can not be changed directly.
Declaration
public class JLabel extends JComponent implements SwingConstants, Accessible
The JLabel Contains 4 constructors. They are as following:
1. JLabel()
2. JLabel(String s)
3. JLabel(Icon i)
4. JLabel(String s, Icon i, int horizontalAlignment)
Example:
Â
JTextArea
In Java, Swing toolkit contains a JTextArea Class. It is under package javax.swing.JTextArea class. It is used for displaying multiple-line text.
Declaration
public class JTextArea extends JTextComponent
The JTextArea Contains 4 constructors. They are as following:
1. JTextArea()
2. JTextArea(String s)
3. JTextArea(int row, int column)
4. JTextArea(String s, int row, int column)
Example:
Lets take an example to create text area in swing. We are using JtextArea class to create text area and adding to Jframe container.
Â
JPasswordField
In Java, Swing toolkit contains a JPasswordField Class. It is under package javax.swing.JPasswordField class. It is specifically used for password and it can be edited.
Declaration
public class JPasswordField extends JTextField
The JPasswordFieldContains 4 constructors. They are as following:
1. JPasswordField()
2. JPasswordField(int columns)
3. JPasswordField(String text)
4. JPasswordField(String text, int columns)
Example:
To generate a password component, swing provides Jpasswordfield that takes user input in encrypted format.
JTable
In Java, Swing toolkit contains a JTable Class. It is under package javax.swing.JTable class. It used to draw a table to display data.
The JTableContains 2 constructors. They are as following:
1. JTable()
2. JTable(Object[][] rows, Object[] columns)
Example:
We are creating an example to create a table using Jtable class and then add it to the Jframe container.
Â
JList
In Java, Swing toolkit contains a JList Class. It is under package javax.swing.JList class. It is used to represent a list of items together. One or more than one item can be selected from the list.
Declaration
public class JList extends JComponent implements Scrollable, Accessible
The JListContains 3 constructors. They are as following:
1. JList()
2. JList(ary[] listData)
3. JList(ListModel<ary> dataModel)
Example:
In this example, we are creating a list of items using Jlist class. this list is used to show the items in a list format and get user input from the list of items. See the below example.
Â