Java Swing Examples

on 12:03 PM

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

Java Swing class hierarchy

on 12:03 PM

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. To learn and understand these swing programs, AWT Programming knowledge is not required.

SWING IN JAVA

on 12:02 PM

What is Swings in java ?

A part of The JFC
Swing Java consists of
Look and feel
Accessibility
Java 2D
Drag and Drop, etc
Compiling & running programs
‘javac ’ && ‘java
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 is 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 and is entirely written in Java, using AWT’s lightweight component support. In particular, unlike AWT, t he 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.