Loading, please wait...

A to Z Full Forms and Acronyms

What is Swing In Java? | Swing Vs AWT

Jun 09, 2020 Java, Java swing, Swing Vs AWT, 3962 Views
In this article you'll learn about swing in Java.

What is SWING?

Swing is a collection of widget tool kits which is used to create a Graphical User Interface (GUI) application. It is a lightweight module. Unlike AWT, the swing is platform-independent. Swing is a fragment of Oracle’s Java Foundation Classes (JFC). Components included are the button, TextField, TextArea, etc. It includes multiple rich set which is used in developing a rich application. The components of the wings are entirely written in Java language. Therefore, it is platform-independent.

What is JFC?

Java Foundation Classes are a graphical framework. It is used for building a portal graphical user interface that is completely based on Java-language. It is comprised of Abstract Window Toolkit, Swing and Java 2D. Collectively, they come up with a consistent user interface which is independent to any of the platform such as window, macOS, and Linux.

Hierarchy of Swing Classes

 

  • Object Class: It is the topmost class which contains all other class along with their components.
  • Component Class: It consists of a container class and JComponent Class.
  • JComponent Class: It consists of a component like JButton, JTextArea, JMenu, Jlist. These components are inherited in the swing by using JComponent class.
  • Container Class: It contains all the necessary components. For developing a GUI application one container class is essentials. Further, it involves windows and panels.
  • Window: Window is the visual representation. It consists of frame and dialog.
  • Frame: It is an operating window with the icons and title.
  • Panel: It is used to assemble the components in a particular manner.
  • Dialog: It is a pop-up window. It doesn’t function like a frame.

Swing Example

Like AWT, there are two ways of creating a frame:

  • By creating, frame class object.
  • By extending, frame class

Swing code can be written in any code of blocks such as inside main(), method, or constructor. The programmer or developer can place the code anywhere according to the need.

Example of creating an object of a frame class

import javax.swing.*; 

public class demo { 

JFrame f; 

demo(){ 

f=new JFrame();//creating instance of JFrame 
JButton b=new JButton("click"); //creating JButton instance 
b.setBounds(200,150,100, 100);  //set the position of the button
f.add(b);//adding JFrame button
f.setSize(600,700);//600 width and 700 height 
f.setLayout(null);//using no layout managers 
f.setVisible(true);//making the frame visible 

} 

public static void main(String[] args) { 
    new demo(); 
} 
}

OUTPUT

Example of extending frame class

import javax.swing.*; 

public class sample extends JFrame{//inheriting JFrame 

JFrame f; 

sample(){ 
JButton b=new JButton("click");//create JButton instance
b.setBounds(130,100,100, 40);  // set the position of the button.
add(b);//adding button on frame 
setSize(400,500); 
setLayout(null); 
setVisible(true); 

} 

public static void main(String[] args) { 
    sample sample = new sample(); 
}} 

OUTPUT:

The difference between creating an instance of frame class and extending a frame class is that we don’t need to create an instance of frame class when we extend the frame class by using extend keyword.

Methods used in Swing

  • add(Component c): - Used to add another component in a component.
  • setSize(int height, int width): - Used to set the size of the component.
  • setLayout(Layout Manager m): - Used to set the layout of the component.
  • setVisible(true/false): - Used to set the visibility of component. By default, it is false.

Difference Between AWT and Swing

AWT

SWING

It is platform dependent.

It is platform-independent.

It doesn’t follow the MVC.

It follows MVC.

It has lesser components.

It has powerful components.

It has heavyweight components.

It has lightweight components.

A to Z Full Forms and Acronyms

Related Article