Swing
Swing
Swing is a Java Foundation Classes [JFC] library and an extension of the
Abstract Window Toolkit [AWT].
Java Swing offers much-improved functionality over AWT, new components,
expanded components features, and excellent event handling with drag-and-
drop support.
Difference between Java Swing and Java AWT
There are certain points from which Java Swing is different than Java AWT as
mentioned below:
Java AWT Java Swing
Execution Time is more than Swing. Execution Time is less than AWT.
.
What is JFC?
JFC stands for Java Foundation Classes. JFC is the set of GUI components that
simplify desktop Applications. Many programmers think that JFC and Swing are one
and the same thing, but that is not so. JFC contains Swing [A UI component package]
and quite a number of other items:
Features Of Swing Class
Pluggable look and feel.
Uses MVC architecture.
Lightweight Components
Platform Independent
Advanced features such as JTable, JTabbedPane, JScollPane, etc.
Java is a platform-independent language and runs on any client machine, the GUI
look and feel, owned and delivered by a platform-specific O/S, simply does not
affect an application’s GUI constructed using Swing components.
Lightweight Components: Starting with the JDK 1.1, its AWT-supported
lightweight component development. For a component to qualify as lightweight, it
must not depend on any non-Java [O/s based) system classes. Swing components
have their own view supported by Java’s look and feel classes.
Pluggable Look and Feel: This feature enable the user to switch the look and feel
of Swing components without restarting an application. The Swing library
supports components’ look and feels that remain the same across all platforms
wherever the program runs. The Swing library provides an API that gives real
flexibility in determining the look and feel of the GUI of an application
Highly customizable – Swing controls can be customized in a very easy way as
visual appearance is independent of internal representation.
Rich controls– Swing provides a rich set of advanced controls like Tree
TabbedPane, slider, colorpicker, and table controls.
Swing Classes Hierarchy
Java
import java.io.*;
import javax.swing.*;
// Main class
class GFG {
frame.add(button);
frame.setSize(500, 600);
frame.setLayout(null);
frame.setVisible(true);
Output:
Example 2: Write a program to create three buttons with caption OK, SUBMIT,
CANCEL.
Java
import java.awt.*;
class button {
button()
// Button 1 created
// OK button
f.add(b1);
// Button 2 created
// Submit button
f.add(b2);
// Button 3 created
// Cancel button
f.add(b3);
f.setSize(500, 500);
f.setLayout(null);
f.setVisible(true);
Output:
Example 3: Program to Add Checkbox in the Frame
Java
// in the Frame
import java.awt.*;
// Driver Class
class Lan {
// Main Function
Lan()
// Frame Created
Frame f = new Frame();
f.add(l1);
// CheckBox created
f.add(c2);
// CheckBox created
f.add(c3);
// CheckBox created
f.add(c4);
f.setSize(500, 500);
f.setLayout(null);
f.setVisible(true);
Output:
Components of Swing Class the task’s percentage
Class Description
A ImageIcon control is an
Imagelcon implementation of the Icon interface
that paints Icons from Images