Updated July 5, 2023
Introduction to Swing Program in JAVA
In this article, we will learn about Swing Program in JAVA. The Java Swing is part of the Java Foundation Classes (JFC) which is used to build widow-based applications. Like AWT, Java Swing offers platform-dependent and lightweight components. The javax.swing package provides JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser, and so on classes for java swing API.
Why Do We Move on To Swing?
Below is the reason why do we move on to swing:
AWT
- Initially, we have a GUI (Graphical User Interface) subsystem like AWT (Abstract Windows Toolkit). This AWT is coverts all graphical interface components into their corresponding platform related specific equivalent. So, it becomes platform dependent.
- AWT is heavyweight because of utilizing native resources.
- Tackle this problem software engineers come up with a new framework called Swing in Java.
Swing
- Swing does not use native resources, so it is light weighted.
- The swing user interface does not depend on any specific platform, so the swing is platform-independent.
- Swing used to develop a standalone desktop application.
Real-Time Example
Installing any application in windows comes under the desktop application. Below some images illustrate the example concept.
Package of Swing: All packages in swing existed within javax.swing.*
Hierarchy of Swing API
Following is a complete hierarchy of swing api:
Components and Containers in Swing
It contains 2 major items components and containers. Below is the detail explanation on both:
Components
- Swing components derived from JComponent as we have seen in the above hierarchy. It supports the look and feels of the application.
- JComponent is compatible with the AWT component as well because it inherits the AWT classes Container and Components.
- Components are: JApplet, JButton, JCheckBox, JComboBox, JComponent, JDailog, JLabel, JFrame, JList, JFileChooser, JMenu, JMenuBar, JMenuItem, JOptionPane, JPanel, JPasswordField, JPopupMenu, JProgressBar, JRadioButton
Containers
It contains 2 types of containers:
- Top-level Containers
- Lightweight Containers
1. Top-Level Containers
- These do not inherit from JComponent. However, these top-level containers inherit from Component and Container as above in the hierarchy.
- JFrame, JApplet, JWindow, and JDailog, etc. are some examples of Top-level containers.
- This is heavyweight components in swing whereas others are lightweight.
2. Lightweight Containers
- These are inherited from JComponent.
- All lightweight components come under lightweight containers.
- JPanel, JButton, JScrollBar, etc. are some of the examples.
Swing Working Principle
- Swing framework is a part of Java Foundation Classes (JFC). JFC is used to create window-based applications in a real-time environment. It is built on the top of the AWT API. Swing is completely developed by using Java language only.
- Swing mainly works on the principle of MVC (Model View Controller).
- The model gives the state of information about the components of swing like JButton, JPasswordField, JLabel, JTextField, etc. Example: In the case of a combo box the model consisting a field that determines if the box is multi-selected or single selected or not selected from the drop-down list. A view shows how the component or container is displayed on the screen.
- The controller tells you how the specified component reacts to the user. Example: If there is a frame consists of a username, password, labels, etc. with the login button as in the below swing example. Once we click on the login button where the user has to go decides(means where the next action has to go) by this controller.
Implementing Swing Program in JAVA
Below is the example to implement swing in java:
Code:
package educba.swing.com;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class SwingIntroduction {
public static void main(String[] args) {
JFrame frame=new JFrame("Login Screen"); //Line 1
JLabel label = new JLabel();//Line 2
label.setBounds(20,150, 200,50); //Line 3
final JPasswordField value = new JPasswordField();//Line 4
value.setBounds(100,75,100,30);
JLabel userNameLabel=new JLabel("User Name:");//Line 5
userNameLabel.setBounds(20,20, 80,30);
JLabel paswordLabel=new JLabel("Password:");//Line 6
paswordLabel.setBounds(20,75, 80,30);
JButton loginButton = new JButton("Login Here");//Line 7
loginButton.setBounds(100,120, 80,30);
final JTextField text = new JTextField();//Line 8
text.setBounds(100,20, 100,30);
frame.add(value); //Line 9
frame.add(userNameLabel); //Line 9
frame.add(label); //Line 9
frame.add(paswordLabel); //Line 9
frame.add(loginButton); //Line 9
frame.add(text); //Line 9
frame.setSize(300,300);
frame.setLayout(null); //Line 10
frame.setVisible(true); //Line 11
loginButton.addActionListener(new ActionListener() { //Line 12
public void actionPerformed(ActionEvent e) {
String userNameDisplay = "You are " + text.getText();//Line 13
label.setText(userNameDisplay); //Line 14
}
});
}
}
Output:
Example Demonstration:
- Line 1 is used to create a Login frame for include required components within it.
- Line 2 is used to create a label in the frame for adding necessary fields like username, password field, etc.
- Line 3 is used for set boundaries (length, width, etc.) with appropriate values by using the setBounds method.
- Line 4 is used for creating a password field (entered values are hidden if we use the password field).
- Line 5 is used for creating the username label.
- Line 6 is used for creating a password label.
- Line 7 is used for creating a button with the button name ‘Login Here’.
- Line 8 is used for creating a text field for adding some text to it.
- Line 9 is used for adding all the components like fields, labels, etc.
- Line 10 is used for making the frame without any layout by setting the null
- Line 11 is used for making the user interface visible by setting the value
- Line 12 is used for the login button action method if we click on login here button what will perform, it decides.
- It displays entered name from the username text field, once we click on the login here button.
Do We Really Need to Memorize All the Swing Components?
No, because, all the components are named with their functionality so if we know the requirement then components almost start with that prefix.
Example:
If we want to create a button then the component name is JButton if we want a Menu bar than JMenuBar if we want Label, Password field, Text field than JLabel, JPasswordField and JTextField respectively are available. So, no need to remember all the component names.
Conclusion – Swing Program in JAVA
Any desktop application if you want to develop with platform-independent and lightweight than you can directly use Swing instead of AWT because as we know AWT is platform-dependent and heavyweight.
Recommended Articles
This is a guide to Swing Program in JAVA. Here we discuss the basic concept, why do we need a swing? types in swing with working principle and program to implement. You can also go through our other related articles to learn more –