Java Swing Tutorial for Beginners with examples



If you want to write a code in Java that will display anything with Graphical User Interface ( GUI), you need to use some API. Swing is main GUI toolkit and is a part of the JFC (Java Foundation Classes). Completely written in Java it is the main API for GUI.

Prerequisite


You must have basic understanding of the Java. If you are not familiar with Java, you may like to take basic tutorial here. We will start writing the GUI code using command line. Later on we will also show you how to write the code using Eclipse environment.

For command line you do not need to do anything other than instaling the Java SDK as outlined in our basic Java Tutorial .

A bit more about Swing


Swing is platform independent since it is completely written in Java. Swing was Originally distributed as a separate downloadable library, but has been included as part of the Java Standard Edition since release 1.2. The current release 1.7.17 continues this tradition. The Swing classes and components are contained in the javax.swing package hierarchy and can be include using the import statement

import javax.swing.*;

Swing provides familiar GUI components such as buttons, check boxes and labels as also advanced components such as tabbed panel, scroll panes, trees, tables, and lists.

Abstract Window Toolkit (AWT) is another GUI API that precedes Swing. However Swing provides a more sophisticated set of GUI components than the AWT. Unlike AWT components, Swing components are not platform-specific since they are written entirely in Java.

In the next page we will write our first example code that will show a Hello World Window.