Overviewof Swing

 

๐ŸŽจ Swing in Java 

Swing is a GUI toolkit in Java used to create rich desktop applications. It is part of the Java Foundation Classes (JFC) and provides a more powerful and flexible alternative to AWT.


๐Ÿงพ What is Swing?

  • Swing is in the javax.swing package.

  • It builds on top of AWT but does not rely on native GUI components, making it platform-independent in look and behavior.

  • All Swing components are lightweight (they do not depend on OS-specific GUI).

  • Supports pluggable look and feel, MVC architecture, and advanced components like trees, tables, tabbed panes, etc.


๐Ÿงฑ Architecture of Swing

Swing follows a Model-View-Controller (MVC) architecture:

  • Model: Holds data

  • View: Renders the component

  • Controller: Handles user interaction

Though this is more simplified in modern Swing development, the separation helps customize behavior and appearance independently.


๐ŸŽฏ Features of Swing

FeatureDescription
Lightweight                    Doesn’t rely on native system components
Pluggable Look and Feel                    Can change appearance dynamically
Rich Set of Widgets                    Includes advanced components (tables, trees, sliders)
Custom Rendering                    Can override painting behavior easily
Multithreaded                    Uses Event Dispatch Thread (EDT) for UI updates

๐Ÿงฐ Commonly Used Swing Components

Component                Description
JFrame            Main window
JPanel            Generic container
JButton            Push button
JLabel            Text label
JTextField            Single-line input
JTextArea            Multi-line text input
JCheckBox            Checkbox
JRadioButton            Radio button
JComboBox            Drop-down
JList            List box
JTable            Display tabular data
JTabbedPane            Tabbed panels
JScrollPane            Scrollbars
JMenuBar, JMenu, JMenuItem            Menu system

๐Ÿงช Simple Example: Swing Program


import javax.swing.*; import java.awt.event.*; public class MySwingApp { public static void main(String[] args) { JFrame frame = new JFrame("Swing Example"); JLabel label = new JLabel("Enter your name:"); label.setBounds(50, 50, 120, 30); JTextField tf = new JTextField(); tf.setBounds(180, 50, 150, 30); JButton button = new JButton("Click Me"); button.setBounds(150, 100, 100, 30); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String name = tf.getText(); JOptionPane.showMessageDialog(frame, "Hello, " + name + "!"); } }); frame.add(label); frame.add(tf); frame.add(button); frame.setSize(400, 250); frame.setLayout(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }

๐Ÿ“ Layout Managers in Swing

Swing uses layout managers to organize components:

Layout ManagerDescription
FlowLayoutLeft to right flow
BorderLayoutRegions: North, South, East, West, Center
GridLayoutTable-like layout
BoxLayoutComponents stacked either vertically or horizontally
GridBagLayoutAdvanced grid (most flexible)

Example:

frame.setLayout(new FlowLayout());

๐Ÿง  Event Handling in Swing

Uses the Event Delegation Model like AWT.

Common interfaces:

  • ActionListener – Button clicks

  • ItemListener – Checkboxes, radio buttons

  • KeyListener – Keyboard input

  • MouseListener – Mouse events

Example:


button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // action on click } });

๐Ÿ“ฆ Important Swing Containers

ContainerDescription
JFrame            Top-level window
JDialog            Pop-up dialog
JPanel            Generic container
JApplet            Used in applets (older technology)

๐ŸŒˆ Pluggable Look and Feel

Swing supports changing the GUI appearance dynamically:


UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); SwingUtilities.updateComponentTreeUI(frame);

Other options:

  • Metal (default)

  • Nimbus

  • Motif

  • Windows (on Windows OS)


๐Ÿงต Threading in Swing

All GUI code should run on the Event Dispatch Thread (EDT):

SwingUtilities.invokeLater(() -> {
new MySwingApp(); });

Comments

Popular posts from this blog

Object Oriented Programming PBCST304 KTU BTech CS Semester 3 2024 Scheme - Dr Binu V P

Introduction to Java Programming

Inheritance and Polymorphism