«Back to Home

Core Java

Topics

Introduction Of Swing In Java

Swing

In Java, Swing is a part of Java Foundation Classes (JFC), which is used to create Window-based Applications. It is built on the top of AWT API and fully written in Java. Swing provides the platform-independent and lightweight components.
The javax.swing package gives many classes for swing API such as JButton, JTextField, JRadioButton, JCheckbox, JColorChooser etc.
 
Difference between AWT and Swing

 AWT  Swing
 AWT components are platform-dependent.  Swing components are platform-independent.
 Its components are heavyweight.  Its components are lightweight.
 AWT doesn't support pluggable look and feel.  Swing supports pluggable look and feel.
 AWT gives less components than Swing.  Swing gives more powerful components than AWT.
 AWT doesn't follow MVC (Model View Controller).  Swing follows MVC (Model View Controller).
 
Java Foundation Classes (JFC) is a set of GUI components, which makes it simple for the development of desktop Applications.
 
Hierarchy of Swing classes

The hierarchy of Swing API is given below.

21
 
Methods of Component class

The methods of Component class are generally used in Swing, which are,
 
public void add(Component c)

This method is used to add a component on another component.
 
public void setSize(int width,int height)

This method is used to set the size of the component.
 
public void setLayout(LayoutManager m)

This method is used to set the layout manager for the component.
 
public void setVisible(boolean b)

This method is used to set the visibility of the component. It is by default false.
 
In Swing, there are 2 ways to create a frame, which are,
  • By creating the object of Frame class (association)

  • By extending Frame class (inheritance)
In Swing, we can write the code inside the main(), constructor or any other method.
 
Let’s see an example of Swing, given below.
 
Code
  1. import javax.swing.*;  
  2. public class SwingExample {  
  3.     public static void main(String[] args) {  
  4.         JFrame jf = new JFrame(); //create object of JFrame  
  5.         JButton jb = new JButton("Press"); //create object of JButton  
  6.         jb.setBounds(10010010030);  
  7.         jf.add(jb); //add button in JFrame  
  8.         jf.setSize(300300); // width and height  
  9.         jf.setLayout(null);  
  10.         jf.setVisible(true);  
  11.     }  
  12. }  
11

Output

23
 
In the example, shown above, we create one button and add it on the JFrame object inside the main() method.
 
Let’s see an example of Swing through Association inside the constructor.
 
Code
  1. import javax.swing.*;  
  2.   
  3. public class SwingExample {  
  4.     JFrame jf;  
  5.     SwingExample() {  
  6.         jf = new JFrame(); //create object of JFrame  
  7.         JButton jb = new JButton("Enter"); //create object of JButton  
  8.         jb.setBounds(10010010030);  
  9.         jf.add(jb); //add button in JFrame  
  10.         jf.setSize(400400);  
  11.         jf.setLayout(null);  
  12.         jf.setVisible(true);  
  13.     }  
  14.     public static void main(String[] args) {  
  15.         new SwingExample();  
  16.     }  
  17. }  
24

Output

25
 
In the example, shown above, the setBounds(int xaxis, int yaxis, int width, int height) are used to set the position of the button. We can also write all the codes to create JFrame, JButton and the method is called inside Java constructor.
 
Let’s see an example of Swing through an inheritance.
 
Code
  1. import javax.swing.*;  
  2. public class SwingExample extends JFrame {  
  3.     JFrame jf;  
  4.     SwingExample() {  
  5.         JButton jb = new JButton("OK");  
  6.         jb.setBounds(13010010040);  
  7.         add(jb);  
  8.         setSize(400400);  
  9.         setLayout(null);  
  10.         setVisible(true);  
  11.     }  
  12.     public static void main(String[] args) {  
  13.         new SwingExample();  
  14.     }  
  15. }  
26

Output

27
 
We can also inherit JFrame class. Thus, there is no need to create the object of JFrame class explicitly.
 
Summary

Thus, we learnt that Java Swing is a part of Java Foundation Classes (JFC), which is used to create Window-based Applications and also learnt how we can use it in Java.