Swing In Java

This section will describe you about a bit of Swing background and tells you how to create GUI for the Java applications and Java Applets.

Swing In Java

This section will describe you about a bit of Swing background and tells you how to create GUI for the Java applications and Java Applets.

Swing In Java

Swing In Java

In this tutorial we will read about the various aspects of Swing such as, what is swing, features of swing, Swing and AWT, Swing API, Swing example etc.

What Is Swing ?

Swing is a part of JFC also known as Oracle's Java Foundation Classes. JFC covers the group of features that allows to build GUI. This also allows to include the more interactivity and rich GUI feature for Java applications. In early days Netscape Communications Corporation was developed a graphics library for Java known as IFC (Internet Foundation Classes) which was first released on 1996. Later, in 1997 Sun Microsystems (now Oracle Corporation) and Netscape Communications Corporation announced for the Java Foundation Classes which purpose was to integrate the IFC with other technologies. Later, the JFC was renamed to Swing. Before, the Java version 1.2 Standard Edition Swing can be downloaded separately but, since the Java 1.2 Standard Edition releases Swing became a part of the Java Standard Edition. Its component and class hierarchy is defined in the javax.swing package.

Architecture Of Swing

Swing is completely written in Java, it supports the MVC (Model-View-Controller) architecture and the platform independencies. It makes a layer called abstraction layer between code structure and the Swing based GUI's graphical presentation. Swing is a single threaded programming model.

Features Of Swing

Swing has the following features :

  • Extensible : Swing has the feature to "plug" custom specified framework's interfaces implementation.
     
  • Customizable : Swing allows to customize its standard components programmatically for example, assigning specific borders, colors, backgrounds, etc.
     
  • Configurable : Swing is configurable, it allows to change in its fundamental settings at runtime. For example, changing in the look and feel implementation at the runtime.
     
  • Light Weight UI : Swing components are light weight UI. However, all swing components are inherited from the AWT because, the JComponent of Swing package extends the container of AWT that uses the OS-native "heavyweight" widget but the swing uses its own OS-agnostic over the fundamental components.
     
  • Loosely Coupled : Swing framework is designed in such a way that there is no tight coupling between the interfaces, presentation, and controller.
     
  • MVC : Java Swing is a MVC based framework (a Software Design Pattern) that separates the data viewed from the interface using which it is viewed.

Swing And AWT

Before, the Java SE version 1.2 Java AWT (Abstract Window Toolkit) was used to create the User Interfaces components. But, since Java 1.2 Swing is available with the Java SE. The AWT components are rendered and controlled by the native peer component which are based on the implicit windowing system whereas, the Swing component has no need to set apart the native available sources in operating system's. Almost all of the top level Swing components extends an AWT top level container.

Swing API

A Swing API has many packages, these are as follows :

javax.accessibility javax.swing.plaf javax.swing.text
javax.swing javax.swing.plaf.basic javax.swing.text.html
javax.swing.border javax.swing.plaf.metal javax.swing.text.html.parser
javax.swing.colorchooser javax.swing.plaf.multi javax.swing.text.rtf
javax.swing.event javax.swing.plaf.synth javax.swing.tree
javax.swing.filechooser javax.swing.table javax.swing.undo

Swing Example

Here I am giving a simple example where we will use the Swing API. This example will demonstrate you about how to use the Swing components in Java applications. In this example we will create a Java class named SwingExample and create a method for creating user interfaces and then we will create main method. In the main() method we will create an object of the class and call the createUI() method.

SwingExample.java

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
 
public class SwingExample {
 
    public void createUI() {
        
        JFrame frame = new JFrame("Swing Example");
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.setLayout(new FlowLayout());
        
        JLabel label = new JLabel("Hello, World!");
        
        JButton button = new JButton("Click Here");
        
        frame.add(label);
        frame.add(button);        
        frame.pack();        
        frame.setVisible(true);
        frame.setSize(200, 100);
    }
 
    public static void main(String[] args) {
        SwingExample se = new SwingExample();
        se.createUI();
    }
 
}

For reading more in detail you may go through the following link http://www.roseindia.net/java/example/java/swing/index.shtml