0% found this document useful (0 votes)
3 views4 pages

swing java

Swing is a Java GUI toolkit that enables the creation of desktop applications with a rich set of components and a pluggable look and feel. It offers advantages over AWT, such as lightweight components and MVC architecture, but has drawbacks like slower performance and higher memory usage. Despite its capabilities, Swing is being gradually replaced by modern frameworks like JavaFX for new Java applications.

Uploaded by

adityapandji1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

swing java

Swing is a Java GUI toolkit that enables the creation of desktop applications with a rich set of components and a pluggable look and feel. It offers advantages over AWT, such as lightweight components and MVC architecture, but has drawbacks like slower performance and higher memory usage. Despite its capabilities, Swing is being gradually replaced by modern frameworks like JavaFX for new Java applications.

Uploaded by

adityapandji1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Swing in Java

Swing is a GUI (Graphical User Interface) toolkit in Java used to create desktop applications.
It is a part of Java Foundation Classes (JFC) and provides more powerful and flexible
components compared to its predecessor, AWT (Abstract Window Toolkit).

Key Features of Swing


1. Lightweight – Unlike AWT, Swing components do not rely on native OS components.
2. Rich UI Components – Provides advanced elements like tables, trees, sliders, and
tabbed panes.
3. Pluggable Look and Feel – You can customize the appearance of Swing components.
4. MVC Architecture – Follows Model-View-Controller architecture, making it more
scalable.
5. Event-Driven Programming – Uses event listeners to handle user interactions.

Swing vs AWT
Feature Swing AWT
Components Lightweight (doesn’t use native OS) Heavyweight (uses native OS components)
Performance Faster Slower
Customization More flexible Less flexible
Look & Feel Can be customized Depends on OS

Common Swing Components


Swing components are part of the javax.swing package.

1. JFrame – Main window


2. JPanel – Container to hold components
3. JButton – Clickable button
4. JLabel – Displays text or images
5. JTextField – Single-line text input
6. JTextArea – Multi-line text input
7. JCheckBox – Checkbox for multiple selections
8. JRadioButton – Radio button for single selection
9. JComboBox – Drop-down list
10. JTable – Table component
Simple Swing Example
Here’s a basic Swing program to display a simple window with a button:

import javax.swing.*;
import java.awt.event.*;

public class SwingExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Swing Example"); // Create a frame
JButton button = new JButton("Click Me"); // Create a button

button.setBounds(100, 100, 150, 40); // Set button position and size

// Add action listener for button click


button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "Button Clicked!");
}
});

frame.add(button); // Add button to frame


frame.setSize(400, 300); // Set frame size
frame.setLayout(null); // No default layout
frame.setVisible(true); // Make frame visible
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Close on exit
}
}

Explanation of Code

 JFrame is used to create a window.


 JButton is used to create a button.
 setBounds(x, y, width, height) sets the button's position.
 addActionListener() handles button clicks.
 JOptionPane.showMessageDialog() shows a message box.

Swing Layout Managers


Swing provides layout managers to arrange components dynamically:

1. FlowLayout – Places components in a row.


2. BorderLayout – Divides into North, South, East, West, Center.
3. GridLayout – Arranges components in a grid.
4. BoxLayout – Stacks components horizontally or vertically.
5. GridBagLayout – Flexible layout with precise control.

Event Handling in Swing


Swing uses event listeners to respond to user actions. Common event listeners:

 ActionListener – Handles button clicks.


 MouseListener – Detects mouse clicks.
 KeyListener – Detects keyboard input.
 WindowListener – Detects window events (open, close, minimize).

Example of ActionListener:

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}
});

Advantages of Swing
✅ More powerful than AWT
✅ Provides a rich set of UI components
✅ Supports pluggable look and feel
✅ Cross-platform compatibility
✅ Supports MVC architecture

Disadvantages of Swing
❌ Slower than native applications
❌ Higher memory usage
❌ Not suitable for mobile development

Conclusion
Swing is a powerful framework for building desktop applications in Java. It provides a rich set
of UI components, flexible layouts, and event-driven programming, making it ideal for
developing interactive applications. However, with modern UI frameworks like JavaFX, Swing
is gradually being replaced for newer Java applications.

You might also like