Introduction to Swing
Swing is the collection of user interface components for Java programs. It is part of Java Foundation classes that are referred to as JFC. In simple words, Swing is the graphical user interface toolkit that is used for developing windows based java applications or programs. It is the successor of AWT, known as the Abstract window toolkit API for Java, and AWT components are mainly heavyweight.
The components are lightweight as compared to AWT components. It provides an excellent interface to the user for all the platforms. It is not specifically for one platform. The components are written in Java and platform-independent as well. The Java foundation classes first appeared in 1997, then later called Swing. To use the swing in java, javax. A swing package needs to be used or imported. It is also known as Java Swing.
Features of Swing
The features of the Swing are as follows:
- Platform Independent: It is platform-independent; the swing components used to build the program are not platform-specific. It can be used on any platform and anywhere.
- Lightweight: Swing components are lightweight, which helps in creating the UI lighter. The swings component allows it to plug into the operating system user interface framework, including the mappings for screens or devices and other user interactions like keypress and mouse movements.
- Plugging: It has a powerful component that can be extended to provide support for the user interface that helps in a good look and feel to the application. It refers to the highly modular-based architecture that allows it to plug into other customized implementations and frameworks for user interfaces. Its components are imported through a package called java.swing.
- Manageable: It is easy to manage and configure. Its mechanism and composition pattern also allows changing the settings at run time. The uniform changes can be provided to the user interface without any changes to the application code.
- MVC: They mainly follow the concept of MVC, which is the Model View Controller. With the help of this, we can make changes in one component without impacting or touching other components. It is known as loosely coupled architecture as well.
- Customizable: Swing controls can be easily customized. It can be changed, and the visual appearance of the component application is independent of its internal representation.
Examples of Swing,
The component class is mainly used, and some of the methods are frequently used, like adding a component in another component (add (Component a)) and setting the size, layout, and visibility of components accordingly.
Below is the example:
import javax.swing.*;
public class Test extends JFrame {
public Test() {
super("Test");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
add(new JLabel("Test, Application!"));
pack();
setVisible(true);
}
public static void main(final String[] args) {
new Test();
}
}
Other examples to show the button:
import javax.swing.*;
public class Swing {
public static void main(String[] args) {
JFrame f=new JFrame();//creating instance of JFrame
JButton b=new JButton("Submit Button");//creating instance of JButton
b.setBounds(120,90,90, 35);//x axis, y axis, width, height
f.add(b);//adding button in JFrame
f.setSize(400,500);//400 width and 500 height
f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible
}
}
Difference between Swing and AWT
The difference between Swing and AWT are as follow:
Basic Comparison | SWING | AWT |
Architecture | It follows the model view controller architecture. | It doesn’t follow the model-view architecture. |
UI | It does support a Pluggable look and feel for UI | It doesn’t support a pluggable look and feel |
Components | It has a lot of components to provide for UI. | It has fewer components as compared to Swing. |
Independent | It is platform Independent. | It is platform-dependent. |
Weight | Its components are lightweight. | Its components are heavyweight. |
Speed | If components are more used, it can be slow. | Its speed would be normal if components were used more. |
Advantages
The advantages are as follows:
- The java swing mainly provides built-in double buffering.
- The new components are built-in swing, providing the support for debugging.
- Swing components mainly change their appearance, the looks, and the feels of the UI based on the package being used.
- It mainly consumes fewer resources than AWT, which is why it is referred to as Lightweight.
- It provides other components like icons, decorative borders, tooltips, etc.
- It mainly provides flexible UI because of following the concept of MVC design patterns.
- It provides additional functionality and other components to replace AWT components.
- The components and applications can be used or run on any platform.
Disadvantages
The disadvantages are as follows:
- The components need version Java 1.2 and other separate jar files to consume.
- It can be slower than AWT.
- To develop the application in swing, the individual has to be very careful with programming.
- Sometimes, the components have not appeared as they should.
Conclusion
It is the framework used for building windows based applications for Java. It was developed to solve the issues that are in AWT. It provides more components to work and uses extensible components to develop the applications. There are many components in the package or library to perform and define the look and feel of the project or application.
It is referred to as the next-generation GUI developed for Java programs. Java Swing is a library of the GUI controls, and classes are not platformed dependent and are lighter in weight because they don’t create the peer components. It mainly provides a consistent appearance or the look and feel of the application across all the platforms.
Recommended Articles
This has been a guide to the What is Swing? Here we discuss the Key Concept, Features, Advantages, and Disadvantages. You can also go through our other suggested articles to learn more –
600+ Online Courses | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6
View Course
Related Courses