Swing framework gives us an ability to create window based applications and its easy to use graphical user interface(GUI).
Swing Framework is part of Java Foundation Classes(JFC). Swing extends the capabilities of Abstract Window Toolkit(AWT) but, with a difference.
Difference between Swing and AWT?
AWT provides a rich set of heavyweight components which are converted to the platform on which they are running,
hence a window based application create using AWT's components might look different on different platform,
whereas Swing Framework provides a set of lightweight components which are not just light to use but also also platform independent.
Hence, the look of a window application created using Swing will appear the same on different platforms, giving it a symmetric look.
AWT components are opaque and rectangular at the edges, which may give a window application an old-outdated look, whereas, the Swing
components have well-rounded edges and a transparent look, giving your window application an updated current look.
Swing Framework consists of two main members -
Container- Container is a top level window in a Swing based window Application. The four top-level containers are JFrame, JWindow, JApplet and JDialog. A Swing container is used to hold a set of components to display them as a part of window based application. All the window based application created using Swing have at least one container.
Component- Component is used to hold another several other components and eventually all the components are held in a top-level container. An example of components are JPanel, JMenu, JTable, JLabel JButton, JCheckBox, JScrollBar etc. All Swing components inherit JComponent class.
Creating a simple Swing window application
To avoid the problem of deadlock in Swing based window applications, we must create the GUI of our window application
using an Event Dispatching Thread and not the main thread.
In order to create and update the GUI of our window based application using Event Dispatching Thread, we must use one of the two methods defined by SwingUtilities class.
static void invokeLater(Runnable ob);
static void invokeAndWait(Runnable ob);
Where ob is a Runnable object, representing an Event Dispatching thread. Method run() of this Runnable object, ob, will be called by Event Dispatching Thread.
The difference between the two methods is that invokeLater() method returns immediately whereas, invokeAndWait() method waits for the run method to complete its execution.
In the upcoming code, we have going create a class by implementing Runnable interface and implementing its run() method, in order to create an Event Dispatching Thread.
Method invokeLater() of SwingUtilities class will be called to create an Event Dispatching Thread.
The run() method Event Dispatching Thread is used to create GUI of our Swing window application.
public class SwingEx implements Runnable
public static void main(String...ar)
SwingUtilities.invokeLater(new SwingEx()); //Creating and calling an Event Dispatching Thread
//Event Dispatching Thread is running to create GUI of window application
public void run()
frame = new JFrame("Swing");
label = new JLabel("The is our first program of Swing");
SwingEx class has implemented Runnable interface and has implemented its run() method.
Method invokeLater() method of SwingUtilities is called to create an Event Dispatching Thread.
Method invokeLater() also automatically calls run() method to run the Event Dispatching Thread, which calls the constructor of A class to create GUI for our Swing application.
When you run the code, you are presented a window showing the main container JFrame, which contains a component JLabel displaying a message like Figure1.