Features of Swing in Java with Example

Swing is a library in Java that assists in the programming of GUI interfaces. Swing was introduced in 1997. Swing was initially available for use in Java 1.1 as an independent class library. The latter swing was fully integrated into Java 1.2. Swing is actually a part of a large family of Java products known as Java Foundation Classes(JFC), which is a set of classes that provide core support for GUI-based enterprise applications. In addition to Swing, other parts of JFC include the AWT, Java 2D, Drag and Drop, and the Accessibility API.

Swing provides a large set of components, ranging from the simple components, such as Labels. Textboxes to very complex components such as trees, tables, progress bars, sliders, etc, Swing components are lightweight components except for a few exceptions. This is because the swing components are implemented entirely in the Java language and do not depend upon peer components, as most AWT components do. So a Swing component will look and function identically on all platforms supported by the JVM, such as Windows, Macintosh, Solaris, and Linux etc.)

 Features of Swing in Java are listed below:

  1. Swing provides many standard GUI components, such as JButtons, JLists, JMenus, and JTextareas, which you combine to create your program’s GUI. It also includes containers(that can hold and organize other components), such as windows and toolbars.
  2. Swing provides extra features for components, such as icons on many components, tooltips for components, and a decorative border for components.
  3. Swing works faster than AWT as Swing components are lightweight.
  4. Swing provides built-in double buffering.
  5. Some of the interesting features of Swing are support for icons, actions on many components, Pluggable Look & Feel technology, assistive technologies, and separate models.
  6. Borders affect the layout of Swing GUIs by making Swing components larger.
  7. Swing programs perform all their event handling in the event-dispatch thread.
  8. Swing components contain support for replacing their insets with an arbitrary number of concentric borders.
  9. There is additional debugging support for the rendering of your own lightweight Swing components.
  10. The javax. swing package provides classes for the Java Swing API, such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser, etc.

 

Common Swing Components  in Java

  • JFrame → Top-level window
  • JButton → Button
  • JLabel → Display text/images
  • JTextField → Single-line text input
  • JTextArea → Multi-line text input
  • JCheckBox, JRadioButton → Selection controls
  • JTable → Display tabular data
  • JTree → Hierarchical data

Example: Simple Swing Program

import javax.swing.*;

 

public class SwingExample {

public static void main(String[] argss) {

JFrame framee = new JFrame(“Swing Example”);

framee.setSize(400, 300);

framee.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 

JButton buttoon = new JButton(“Click Me!”);

 

framee.add(buttoon);

 

// Set frame visible

framee.setVisible(true);

}

}

Simple Swing Program
Simple Swing Program

Explanation:

  1. JFrame is the main window.
  2. A JButton is created and added to the frame.
  3. setVisible(true) makes the frame appear on screen.
  4. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ensures the app closes when the window is closed.

 

 

Event Handling Example in Java Swing

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

 

public class ButtonExample

{

public static void main(String[] argss)

{

final JFrame framee = new JFrame(“Event Example”);

JButton button = new JButton(“Click Me”);

 

button.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

JOptionPane.showMessageDialog( framee,”Pls Button Clicked!”);

}

});

 

framee.add(button);

framee.setSize(300, 200);

framee.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

framee.setVisible(true);

}

}

Event Handling Example in Java Swing
Event Handling Example in Java Swing

Here, when the button is clicked, an event listener triggers a message dialog.

Add  Border to  JPanel

How To Take Input From User In Java