Programming Section

This section contains small programs with source code to demonstrate how to use the Java runtime library.

—————————————————————————————————

Java Swing Introduction Tutorial

 

What is Java Swing ?

  • A part of The Java Foundation Classes
  • Swing consists of
    Look and feel
    Accessibility
    Java 2D (Java 2 onwards)
    Drag and Drop, etc
  • Compiling & running programs javac <program.java>’ && ‘java <program>’
    Or JCreator / IDE
  • if you do not explicitly add a GUI component to a container, the GUI component will not be displayed when the container appears on the screen.

Swing, which is an extension library to the AWT, includes new and improved components that enhance the look and functionality of GUIs. Swing can be used to build Standalone swing gui Apps as well as Servlets and Applets. It employs a model/view design architecture. Swing being 100% Java code makes it more portable and more flexible than AWT.

Swing Model/view design: The “view part” of the MV design is implemented with a component object and the UI object. The “model part” of the MV design is implemented by a model object and a change listener object.

Swing is built on top of AWT. Swing is written entirely in Java, using AWT’s lightweight component support. In particular, unlike AWT, the architecture of Swing components makes it easy to customize both their appearance and behavior. Components from AWT and Swing can be mixed, allowing you to add Swing support to existing AWT-based programs. For example, swing components such as JSlider, JButton and JCheckbox could be used in the same program with standard AWT labels, textfields and scrollbars. You could subclass the existing Swing UI, model, or change listener classes without having to reinvent the entire implementation. Swing also has the ability to replace these objects on-the-fly.

  • 100% Java implementation of components
  • Pluggable Look & Feel
  • Lightweight components
  • Uses MVC Architecture

Model represents the data
View as a visual representation of the data
Controller takes input and translates it to changes in data

  • Three parts

Component set (subclasses of JComponent)
Support classes
Interfaces

In Swing, classes that represent GUI components have names beginning with the letter J. Some examples are JButton, JLabel, and JSlider. Altogether there are more than 250 new classes and 75 interfaces in Swing — twice as many as in AWT.

Java Swing class hierarchy

The class JComponent, descended directly from Container, is the root class for most of Swing’s user interface components.

Swing contains components that you’ll use to build a GUI. I am listing you some of the commonly used Swing components that are used in Java swing programs. To learn and understand these swing programs, AWT Programming knowledge is not required.

Java Swing Examples

Below is a java swing code for the traditional Hello World program.

Basically, the idea behind this Hello World program is to learn how to create a java program, compile and run it. To create your java source code you can use any editor( Text pad/Edit plus are my favorites) or you can use an IDE like Eclipse.

import javax.swing.JFrame;
import javax.swing.JLabel;

//import statements

//Check if window closes automatically. Otherwise add suitable code
public class HelloWorldFrame extends JFrame{

	public static void main(String args[]){
		new HelloWorldFrame();
	}

	HelloWorldFrame(){
		JLabel jlbHelloWorld = new JLabel("Hello World");
		add(jlbHelloWorld);
		this.setSize(100, 100);
//		pack();
		setVisible(true);
	}
}
Output

Leave a comment