A component is an independent visual control. Swing Framework contains a large set of 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:
1. Top level Containers
- It inherits Component and Container of AWT.
- It cannot be contained within other containers.
- Heavyweight
- Example: JFrame, JDialog, JApplet
2. Lightweight Containers
- It inherits JComponent class.
- It is a general purpose container.
- It can be used to organize related components together.
- Example: JPanel
JButton
JButton is in implementation of a push button. It is used to trigger an action if the user clicks on it. JButton can display a text, an icon, or both.
JButton btn=new JButton(“Click Me”);
JLabel
Label is a simple component for displaying text, images or both. It does not react to input events.
JLabel lbl=new Label(“This is a label”);
JTextField
JTextField is a text component that allows editing of a single line of non-formatted text.
JTextField txt=new JTextField();
JCheckBox
JCheckBox is a box with a label that has two states: on and off. If the check box is selected, it is represented by a tick in a box. A check box can be used to show or hide a splashscreen at startup, toggle visibility of a toolbar etc.
JCheckBox chk1=new JCheckBox(“BCA”);
JCheckBox chk2=new JCheckBox(“BBA”);
JRadioButton
JRadioButton allows the user to select a single exclusive choice from a group of options. It is used with the ButtonGroup component.
JRadioButton rad1=new JRadioButton (“Male”);
JRadioButton red2=new JRadioButton (“Female”);
ButtonGroup grp=new ButtonGroup();
grp.add(rad1);
grp.add(rad2);
JComboBox
JComboBox is a component that combines a button or editable field and a drop-down list. The user can select a value from the drop-down list, which appears at the user's request. If you make the combo box editable, then the combo box includes an editable field into which the user can type a value.
String arr[]={“BCA”,”BBA”,”MCA”,”MBA”};
JComboBox cmb=new JComboBox(arr);
//or
JComboBox<String> cmb=new JComboBox<String>();
cmb.addItem(“BCA”);
cmb.addItem(“BBA”);
cmb.addItem(“MCA”);
cmb.addItem(“MBA”);
JList
JList is a component that displays a list of objects. It allows the user to select one or more items.
String arr[]={“BCA”,”BBA”,”MCA”,”MBA”};
JList cmb=new JList(arr);
JTextArea
A JTextArea is a multiline text area that displays plain text. It is lightweight component for working with text. The component does not handle scrolling. For this task, we use JScrollPane component.
JTextArea txt=new JTextArea();
JTable
The JTable class is a part of Java Swing Package and is generally used to display or edit two- dimensional data that is having both rows and columns. It is similar to a spreadsheet. This arranges data in a tabular form.
// Data to be displayed in the JTable
String[][] data = {
{ "Kundan Kumar Jha", "4031", "CSE" },
{ "Anand Jha", "6014", "IT" }
};
// Column Names
String[] columnNames = { "Name", "Roll Number", "Department" };
// Initializing the JTable
JTable j = new JTable(data, columnNames);
JMenu
The JMenuBar class is used to display menubar on the window or frame. It may have several menus.
The object of JMenu class is a pull down menu component which is displayed from the menu bar. It inherits the JMenuItem class.
The object of JMenuItem class adds a simple labeled menu item. The items used in a menu must belong to the JMenuItem or any of its subclass.
JMenuBar mb=new JMenuBar();
JMenu menu1=new JMenu(“Courses”);
JMenuItem item1=new JMenuItem(“BCA”);
JMenuItem item2=new JMenuItem(“BBA”);
menu1.add(item1);
menu1.add(item2);
mb.add(menu1);