Swings in Java

Swings in Java with Examples

In this article, I am going to discuss Swings in Java with Examples. Please read our previous article, where we discussed Event Handling in Java. At the end of this article, you will understand the following pointers in detail.

  1. What are Swings in Java
  2. Why do we need Swings in Java?
  3. Difference Between AWT package and SWING package
  4. Common Methods of Swing Component
  5. Understanding WindowPane & JFrame
  6. Multiple Programs to understand Swings in Java
Swings in Java:

Swings are used to develop a better efficient GUI. The Swing component is part of JFC (Java Foundation Classes) which are developed in Java. The Swing components are called lightweight components which will improve the performance of the application because the amount of resources required is very minimum. There are no peer classes for the swing components to interact with the operating system. The swing component supports a pluggable look and feels using which the component can be presented in various fat forms having the same behavior.

Note: Swing is not a replacement for AWT but is an extension of AWT.

Swing is an advanced package comparing to AWT package and also Swing doesn’t depend on peer classes. The swing package follows the MVC architecture model. Here, MVC stands for Model View Controller Architecture.

MVC architecture is for separating presentation logic from controlling and business logic. Here, business logic is nothing but a model. For example, we can take a button as an example if we click on the button the state of the button is changed, i.e. on the button, we will see dotted lines. Internally controller class is responsible to communicates with the model to change a state of the button component and it communicates with a view to updating that state. Here the view is nothing but UI (User Interface).

Difference Between AWT package and SWING package
AWT package
  1. AWT package based classes must depend on peer classes for creating UI components.
  2. It makes Java as platform dependent.
  3. It doesn’t follow the MVC architecture pattern.
SWING package
  1. It doesn’t depend on peer classes.
  2. It makes Java platform-independent.
  3. It follows the MVC architecture style.
Features of Swing
  1. It provides a lightweight architecture.
  2. It provides support for the look and feels management.
  3. It allows us to add images on the background of UI Components.
  4. It provides advanced classes that support fro latest UI Components creation like Dialog boxes, etc.
Common Methods of Swing Component
  1. public void add(Component c): It is used to add a component on another component.
  2. public void setSize(int width, int height): It is used to set the size of the component.
  3. public void setLayout(LayoutManager m): It is used to set the layout manager for the component.
  4. public void setVisible(boolean b): It is used to set the visibility of the component. By default, the value is set to be false.
Sample Program to understand swing in Java:
import javax.swing.*;
import java.awt.event.*;

public class SwingDemo1
{
    public static void main (String[]args)
    {
        JFrame jf = new JFrame ();
        jf.setSize (100, 100);
        JButton jb = new JButton ("click");
        jb.setMnemonic ('C');
        MyListener ob = new MyListener ();
        jb.addActionListener (ob);
        jf.add (jb);
        jf.setVisible (true);
    }
}
class MyListener implements ActionListener
{
    public void actionPerformed (ActionEvent ae)
    {
        System.out.println ("Button Clicked");
    }
}

Output:

Program to understand swing in Java

In the above application, we defined a keyboard shortcut key for the button component using setMnemonics().

Note: The keyboard shortcut key is an advanced feature provided by Java Swing.

WindowPane

The empty area that is available in a container is which is used for displaying the components is called WindowPane. The windowpane is internally divided into multiple panes.

  1. GlassPane: This is the first pane closer to the window(screen) and it is used for displaying foreground components.
  2. ContentPane: This pane is available behind GlassPane and it is used for displaying individual components.
  3. LayeredPane: This pane is available behind the content pane and it is used to display a group of components.
  4. RootPane: This pane is available behind the Layered pane and it is used to display background components.

Note: All the four panes are placed on top of one another and they are transpired and All the swing components are available in javax.swing package.

JFrame

The javax.swing.JFrame class may be a sort of container which inherits the java.awt.Frame class. JFrame works just like the main window where components like labels, buttons, text fields are added to make a GUI. Unlike Frame, JFrame has the choice to cover or close the window with the assistance of the setDefaultCloseOperation(int) method.

Syntax : JFrame jf = new JFrame();

JFrame Constructors
  1. JFrame(): It is used to construct a new Frame that is visible initially.
  2. JFrame(GraphicsConfiguration gc): It is used to create a Frame in the specified GraphicsConfiguration of a screen device and a blank title.
  3. JFrame(String title): It is used to create a new, initially invisible Frame with the specified title.
  4. JFrame(String title, GraohicsConfiguration gc): It is used to create a JFrame with the specified title and the specified GraphicsConfiguration of a screen device.
Sample Program to understand JFrame in Java:
import javax.swing.*;
public class JFrameDemo
{
    public static void main (String[]args)
    {
        JFrame jf = new JFrame ();
        jf.setVisible (true);
        jf.setSize (400, 500);
        jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    }
}

Output:

Program to understand JFrame in Java

In the next article, I am going to discuss Swing Controls in Java with examples. Here, in this article, I try to explain Swings in Java with Examples and I hope you enjoy this Swings in Java with Examples article.

Leave a Reply

Your email address will not be published. Required fields are marked *