Skip to content


Java Swing

If you’re learning java & getting used to those console outputs, then you must be desperate to create GUI programs. With java, you can have added advantage of using GUI programs on variety of operating systems. Due to its customized look & feel option,less system resources and more sophisticated components makes swing used in many desktop applications these days. This article is simple introduction to Swing UI.

If you’re new to java, then take a look at this article to learn basics of java, then come back here to learn about Swing.

What is Swing?
Swing is lightweight user interface component, introduced by Sun. With Swing you can create cool & effective desktop applications.

Advantages of Swing

Swing provides rich set of components than AWT. Following are some of the advantages of Swing :

- Swing components follow MVC (Model-View-Controller) architecture, and thus provide much more flexible UI

- Swing offers customized look & feel (Synthetica, Organic)

- Swing gives option of paint debugging support

- Swing provides built-in “double buffering”

- Swing components can connect scrolling to various components

- Swing offers two additional layout managers ( BoxLayout and OverlayLayout)

- Swing allows you to connect keystrokes to components easily.

Difference between AWT & Swing

Each AWT component gets its own operating platform window look,hence it degrades performance when large number of such windows are in operation as it uses great deal of memory. So AWT is called “heavy weight” components. While Swing has its own look & feel, hence it uses far fewer system resources,hence they’re called lightweight components.

Example

Let’s take a look at simple example of Swing :

import javax.swing.JFrame;
public class SwingDemo extends JFrame {   
    public SwingDemo() {
        setSize(550, 400);
        setTitle("Swing Demo");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
   }
    public static void main(String[] args) {
        SwingDemo sd = new SwingDemo();
        sd.setVisible(true);
    }
}

After compiling the program, run it you’ll see the Swing window as shown below:

 

Code explaination:

Lets take a look at line 1 : import javax.swing.JFrame;
With this statement we imported Jframe which is top-level container, which is used for placing other widgets.

At line 3: We have created constructor, inside which we have assigned Size & Title for the Jframe.

At line 6: We used method which allows our window to EXIT when we click on close button of window.

Well that’s all there to this Swing example. You can add more components like button,menus, toottips, internal frames and much more.  If you want to get feel of Swing then come up with some small project  that actually does something, then according to its requirement keep adding component to it till you get your program done.

I hope this article helps. If you’ve any question then please do not hesitate to drop comments here.


One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Continuing the Discussion

  1. Java : Onecore linked to this post on December 2, 2008

    [...] Swing  [...]



Some HTML is OK

or, reply to this post via trackback.