Wednesday, 13 November 2013

Introduction to Java Swing - Everything You Need To Know

Here is a kickstart introduction to Java swing that lets you learn the swing with ease and fun! Every small chunk of Swing is given a headline so that you could find it easy to learn.

Quick tip: After reading each chunk, Get up, play and eat, and tackle the next chunk.

The javax.swing package

All the swing is tightly packed into a single package called javax.swing. But why is it given a name javax? Well, javax stands for Java's Extended Technology. I call it the Grandson generation of Java because it is advanced. And Oh no!! Are we into an advanced topic? Yes, swing is an advanced topic but you don't need to get frightened of the word because advanced isn't a synonym for harder.

“There are no secrets to success. It is the result of preparation, hard work, and learning from failure.” - Colin Powell

A sub package the javax.swing.event contains event listeners and event classes associated with several swing components. To understand this, you must go through what is event handling. Swing itself uses AWT event handling as said previously for many of its components, so we can conform that Swing doesn't override the AWT.

Disadvantages of AWT

1. Limited set of components. No sliders, progress bars, spinners..
2. Written in native code, looks different on different operating systems.
3. Not highly customizable
4. OS dependent, for each component creation, the JVM has to make calls to the Operating system.
5. AWT components take up more resources, they are slow because they need interaction with the OS.

“Every advantage has its disadvantage” - Proverb

Note: Even the top-level containers in Swing (JFrame, JDialog etc) must depend on the OS because a window (a top-level container) cannot be created without the permission of OS.

Fixing the problems raised in the AWT, Swing was introduced in the year 1997 and became a part of the Java Foundation Classes (JFC). Swing was used as separate library in Java 1.1, but later, from Java 1.2 it was integrated into the Java. All the top-level containers in Swing extend the AWT classes.

But what are top-level containers? These are components that must depend on the operating system to get displayed. They are the ultimate containers, that means nothing can contain them but they contain everything. For example, JFrame can contain any components in it but you cannot add JFrame to another container. Similarly, JDialog too cannot be added to any container. Now did you get that. Here are the list of top-level containers in Swing.


JFrame extends java.awt.Frame


JDialog extends java.awt.Dialog

JApplet extends java.awt.Applet

JWindow extends java.awt.Window


Swing redefines the Look And Feel

The major advantage of Swing is that it's pluggable look and feel. What does that mean? It means that we can change look and feel and that the look and feel is not dependent on the Operating system. You can create transparent and shaped components, apply gradients and customize them to the maximum extent you can just with Java code. In this blog there are a lot of articles written hacking the swing, because I myself am a lover of Swing. So, swing has control over the look and feel and the great thing is that it separates look and feel from the logic of the component.
Next: MVC Architecture in Swing - Kickstart Guide

No comments:

Post a Comment