Categories: Java

Swing Class in Java

Swing is a Java Foundation Classes [JFC] framework and an Abstract Window Toolkit [AWT] extension. Swing has much-improved functionality over AWT, including additional components, expanded component features, and superior event management with drag-and-drop support. Unlike AWT, Java Swing provides platform-independent and lightweight components.

Features Of Swing Class :
  • Pluggable look and feel
  • Uses MVC architecture
  • Lightweight Components
  • Platform Independent
  • Advance features such as JTable, JTabbedPane, JScollPane etc
  • Lightweight Components
  • Pluggable Look and Feel
The distinction between AWT and Swing:
Java AWTJava Swing
AWT components are platform-dependent.Java swing components are platform-independent.
AWT components are heavyweight.Swing components are lightweight.
AWT doesn’t support pluggable look and feel.Swing supports a pluggable look and feel.
AWT provides fewer components than Swing.Tables, lists, scrollpanes, colorpicker, tabbedpane, and other more powerful components are available in Swing.
AWT does not adhere to the MVC (Model View Controller) paradigm, in which the model represents data, the view provides presentation, and the controller serves as an interface between the model and the view.Swing follows MVC.
Hierarchy of Java Swing classes:
Components of Swing Class:
ComponentA Component is the Abstract base class for SWING’s non-menu user interface controls. Components are objects that have graphical representations.
ContainerA Container is a component that holds SWING Components.
JComponentA JComponent is a foundational class for all swing UI Components. To use a JComponent-derived swing component, the component must be in a containment hierarchy whose root is a top-level Swing container.
JLabelA JLabel is an object component that is used to place text in a container.
JButtonThis class generates a button with a label.
JColorChooserA JColorChooser is a set of controls that allows the user to manipulate and select a color.
JCheckBoxA JCheckBox is a graphical (GUI) component that can be on (true) or off (false).
JRadioButtonThe JRadioButton class is a graphical (GUI) component that can be on (true) or off (false). within the group
JListWith a scrolling list of text objects, a JList component represents the user.
JComboBoxJComboBox is a component. Displays a menu of options to the user.
JTextFieldA JTextField object is a text component that lets you change a single line of text.
JPasswordField                 A JPasswordField object is a text component designed specifically for password entering.
JTextAreaA JTextArea object is a text component that allows you to modify many lines of text at once.
ImagelconAn ImageIcon control is an Icon interface implementation that paints Icons from images.
JScrollbarA JScrollbar control represents a scroll bar component that allows users to select from a range of values.
JOptionPane   JOptionPane is a collection of basic dialogue windows that prompt users to enter a value or Something.
JFileChooser It Controls a JFileChooser, which depicts a dialogue box from which the user can select a file.
JProgressBar             As the task progresses towards completion, the progress bar displays the percentage of the task on its completion
JSlider A JSlider is a class that allows the user to select a value graphically (GUI) by moving a knob within a specified interval.
JSpinner A JSpinner is a single-line input field that allows the user to select from an ordered sequence by using a number or an object value.
A simple example of Swing Frame:
import javax.swing.*;  //importing swing package
import javax.swing.*;  //importing swing package
import java.awt.*;     //importing awt package
public class Coderz
{
 JFrame jf;
 public Coderz() {
  jf = new JFrame("MyWindow");            //Creating a JFrame with name MyWindow
  JButton btn = new JButton("Say Hello");//Creating a Button named Say Hello
  jf.add(btn);                            //adding button to frame
  jf.setLayout(new FlowLayout());        //setting layout using FlowLayout object
  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      //setting close  operation.
  jf.setSize(400, 400);                   //setting size
  jf.setVisible(true);                    //setting frame visibility
 }
 public static void main(String[] args)
 {
  new Coderz();
 }
}

Note: also read about the AWT (Abstract Window Toolkit) in Java- II

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago