Gone are the days, when softwares had command line interface. Now everyone likes to have a user friendly Graphical User Interface (GUI). Java also provides support to design and develop Graphical User Interface (GUI). To create a Java program with a graphical user interface (GUI), you'll want to learn about Java Swing.

The Swing toolkit includes a rich set of components for building GUIs and adding interactivity to Java applications. Swing includes all the components you would expect from a modern toolkit: table controls, list controls, tree controls, buttons, and labels.

Swing is part of the Java Foundation Classes (JFC). The JFC also include other features important to a GUI program, such as the ability to add rich graphics functionality and the ability to create a program that can work in different languages and by users with different input devices.

Java Swing – Features

Java Swing has many features which makes it ideal for Graphical User Interface (GUI) development. Few of those are:

  • Lightweight
  • Built-in controls like image buttons, tabbed panes, sliders, toolbars, color choosers, tables, text areas
  • Customizable (change border, text alignment, visual appearance of almost any control and can change)
  • Pluggable look and feel
  • New features like tool tips, tool bars, keyboard accelerators, custom cursors, etc. are also available
  • Has arbitrary keyboard event binding
  • Debugging support is also available

Java Swing compared to Java AWT

Java AWT and Java Swing are both part of a group of Java class libraries called the Java Foundation Classes (JFC). Java AWT is older than Java Swing. At first, Java provided Abstract Windowing Toolkit (AWT) for GUI interface designing. AWT is based on the underlying native system components.

The Abstract Windowing Toolkit (AWT) is the original GUI toolkit shipped with the Java Development Kit (JDK) and Java Swing is built on Java AWT. Few benefits of choosing Java Swings compared to Java AWT are:

  • The look and feel and behavior of Java Swing components is consistent across platforms but AWT components's provides different look and feel and behavior for different platform.
  • Event model is more efficient in Java Swing as compared to AWT which means Swing components can run more quickly than their AWT counterparts.

A drawback of Java Swing as compared to Java AWT is that Java Swing components can take longer to load than AWT components.

Most of the Java application developers prefer Java Swing now a days for Graphic User Interfaces though Java AWT is still in use.

Definitive guide to Swing for Java 2

If you are new to Java Swing, you surely need a definitive guide to Swing for Java 2. You should go through a comprehensive Java Swing tutorial. Sun’s Java Swing tutorial can be found at

Trail: Creating a GUI with JFC/Swing (The Java™ Tutorials)

Best was to learn from Java Swing tutorial is to code Java Swing examples after learning a concept.

The following list shows some of the features that Java Swing and the Java Foundation Classes provide.

Java Swing Components

The Java Swing toolkit includes a rich array of components: from basic components, such as buttons and check boxes, to rich and complex components, such as tables and text. Even deceptively simple components, such as text fields, offer sophisticated functionality, such as formatted text input or password field behavior. These components are commonly referred as Java Swing components.

Java 2D API

To make your application stand out; convey information visually; or add figures, images, or animation to your GUI, you'll want to use the Java 2D API. Because Swing is built on the 2D package, it's trivial to make use of 2D within Swing components. Adding images, drop shadows, composing is easy with Java 2D.

Pluggable Look-and-Feel Support

Any program that uses Swing components has a choice of look and feel.

A program can specify the look and feel of the platform it is running on, or it can specify to always use the Java look and feel, and without recompiling, it will just work. Or, you can ignore the issue and let the UI manager sort it out.

Data Transfer

Data transfer, via cut, copy, paste, and drag and drop is essential to almost any application. Support for data transfer is built into Java Swing and works between Java Swing components within an application, between Java applications, and between Java and native applications.


This feature allows developers to build applications that can interact with users worldwide in their own languages and cultural conventions. Applications can be created that accept input in languages that use thousands of different characters, such as Japanese, Chinese, or Korean.

Java Swing's layout managers make it easy to honor a particular orientation required by the UI. For example, the UI will appear right to left in a locale where the text flows right to left. This support is automatic: You need only code the UI once and then it will work for left to right and right to left, as well as honor the appropriate size of components that change as you localize the text.

Accessibility API

People with disabilities use special software technologies that mediates the user experience for them. Such software needs to obtain a wealth of information about the running application in order to represent it in alternate media: for a screen reader to read the screen with synthetic speech or render it via a Braille display, for a screen magnifier to track the caret and keyboard focus, for on-screen keyboards to present dynamic keyboards of the menu choices and toolbar items and dialog controls, and for voice control systems to know what the user can control with his or her voice. The accessibility API enables these technologies to get the information they need, and to programmatically manipulate the elements that make up the graphical user interface.

Undo Framework API

Java Swing's undo framework allows developers to provide support for undo and redo. Undo support is built in to Swing's text component. For other components, Swing supports an unlimited number of actions to undo and redo, and is easily adapted to an application. For example, you could easily enable undo to add and remove elements from a table.

Flexible Deployment Support

If you want your program to run within a browser window, you can create it as an applet and run it using Java Plug-in, which supports a variety of browsers, such as Internet Explorer, Firefox, and Safari. If you want to create a program that can be launched from a browser, you can do this with Java Web Start. Of course, your application can also run outside of browser as a standard desktop application.

The example below creates a simple frame. This is an empty frame created importing package javax.swing. Class Jframe of this package is used to create this frame.

Java Code:
import javax.swing.JFrame;

public class SwingDemo{
	public static void main(String[] args){
		JFrame frame = new JFrame("Java Swing Demo");
		frame.setSize(400, 400);

Java Swing Programming

Java Swing programming can be done in any text editor like notepad, wordpad, textpad etc. For beginners, it is recommented to do Java Swing programming in simple text editor which does not provide any features to ease programming.

However, today there are a number of advanced IDEs (Integerated Development Environments), that can really simplify Java Swing programming. These advanced IDEs (Integerated Development Environments) provide drag and drop support for creating graphical user interfaces using Java Swing components.

An integrated development environment (IDE) is a computer software that helps computer programmers in developing software. Programmign becomes easier and less time consuming using IDEs. Normally IDE include code editor, a compiler/interpreter, build-automation tools, code generators, auto-completion, refactoring, and a debugger. Few good IDEs also include version control systems, GUI tools and serer management tools. I will breifly tlak about popular IDEs.

Java Swing Programming – Using Jbuilder

JBuilder is a very powerful Java IDE which provides cross platform support. JBuilder X and JBuilder 2005 are the most popular versions. JBuilder 2007 will be available soon this year. JBuilder provides backward compatibility with previous versions which makes life easier for software companies working in old versions and intending to switch to latest version. JBuilder includes many feature which makes job of programmer/developer easier. GUI designing is very easy merarly drag and drop in Jbuiler. You are not supposed to learn how to code Java Swing components while using Jbuilder.

Java Swing Programming – Using Eclipse

Eclipse is a very easy to use IDE. It provides binaries for operating systems such as Windows, Linux, Solaris, HP-UX, and AIX to allow for platform independence. Installing it is very easy. After installing, even you can move the installed Eclipse folder to other machine and it will work properly. Eclipse provides programmers smart feature, which makes programming easier and simple. There are countless Eclipse plugins available, so the developers will reuse those and will concentrate on the code problem rather than developing basic features again and gain. Eclipse has a giant development community. Many individuals and big software companies are working on Eclipse custom plugin development projects. Most of such plugins are free and you can download and embed it in your Eclipse programming environment. For example there are many plugins for GUI development which can be used to make Java Swing programming very simple for programmers. Plugins are usually available in JAR files. You just have to copy that java file in Eclipse Plugins folder.

For Eclipse plugins, Visit
Eclipse Plugin Central :: Eclipse plugin resource center and marketplace for Eclipse and Plugin Ecosystem

Java Swing Programming – Using Netbeans

NetBeans is another popular Java IDE which is free and open source and it also provides drag and drop Java Swing GUI development.

Java Swing Programming – Event Handling

The Java Swing event model is quite powerful and flexible. Any number of event listener objects can listen for all kinds of events from any number of event source objects. For example, a program might create one listener per event source. Or a program might have a single listener for all events from all sources. A program can even have more than one listener for a single kind of event from a single event source.

Multiple listeners can register to be notified of events of a particular type from a particular source. Also, the same listener can listen to notifications from different objects. Each event is represented by an object that gives information about the event and identifies the event source. Event sources are often components or models, but other kinds of objects can also be event sources.

The most important rule to keep in mind about event listeners is that they should execute very quickly. Because all drawing and event-listening methods are executed in the same thread, a slow event-listener method can make the program seem unresponsive and slow to repaint itself. If you need to perform some lengthy operation as the result of an event, do it by starting up another thread (or somehow sending a request to another thread) to perform the operation.

Every event-listener method has a single argument an object that inherits from the EventObject class. Although the argument always descends from EventObject, its type is generally specified more precisely. For example, the argument for methods that handle mouse events is an instance of MouseEvent, where MouseEvent is an indirect subclass of EventObject.

Events can be divided into two groups: low-level events and semantic events. Low-level events represent window-system occurrences or low-level input. Everything else is a semantic event. Examples of low-level events include mouse and key events both of which result directly from user input. Examples of semantic events include action and item events. Whenever possible, you should listen for semantic events rather than low-level events. That way, you can make your code as robust and portable as possible.

To help you avoid implementing empty method bodies, the API generally includes an adapter class for each listener interface with more than one method. For example, the MouseAdapter class implements the MouseListener interface. An adapter class implements empty versions of all its interface's methods.

Inner classes can also be useful for event listeners that implement one or more interfaces directly. You can create an inner class without specifying a name this is known as an anonymous inner class.

An EventHandler class supports dynamic generation of simple, one-statement event listeners. Probably the most common use of EventHandler is to extract a property value from the source of the event object and set this value as the value of a property of the target object.