Curriculum
Course: Complete Java Programming Course
Login

Curriculum

Complete Java Programming Course

Video lesson

Introduction to AWT and Swing Components in Java

In this lesson, you will learn.

  • What is AWT
  • What is Swing
  • Advantages of using Swing Over AWT
  • Key differences b/w AWT and Swings
  • Component Hierarchy of Swing Component

 

What is AWT in Java?

  1. AWT (Abstract Window Toolkit) is a set of classes in Java for creating Graphical User Interfaces (GUIs). It provides basic building blocks such as buttons, text fields, windows, and menus.
  2. AWT is platform-dependent, meaning it uses the native GUI components (called peer components) of the operating system. So, an AWT button looks different on Windows vs Linux.
  3. The AWT classes are contained in the java.awt package.

 

History of AWT

  • 1995 – Introduced with the first version of Java by Sun Microsystems
  • Early goal: Provide cross-platform GUI capability
  • Problem:
    • Heavyweight components (dependent on OS)
    • Limited flexibility and inconsistent look

👉 Due to these limitations, Swing (JFC) was introduced later as a more advanced GUI toolkit.

 

What is Swing in Java?

Swing is a part of the Java Foundation Classes (JFC) and provides a more powerful and flexible GUI toolkit than AWT.

The following form shows the swings component, which contains labels, text fields, buttons, text areas, radio buttons, etc.

 

Here are the advantages of using Swing over AWT, followed by a table highlighting key differences between the two.

 

Advantages of Using Swing Over AWT

  1. Platform Independence:
    • Swing components are written entirely in Java and are platform-independent.
    • They have a consistent look and feel across different operating systems.
  2. Pluggable Look and Feel:
    • Swing supports pluggable look-and-feel, allowing developers to dynamically change the appearance of applications.
    • This means you can use a cross-platform look and feel or one that mimics the native OS.
  3. Rich Set of Components:
    • Swing provides a richer set of GUI components than AWT.
    • It includes advanced components like trees, tables, tabbed panes, sliders, and more.
  4. Customizable Components:
    • Swing components are highly customizable.
    • You can easily create your own components by extending existing ones.
  5. Lightweight Components:
    • Unlike AWT, which relies on native code (heavyweight components), Swing components are lightweight as they do not rely on native peers.
    • This leads to better performance and flexibility.
  6. MVC Architecture:
    • Swing uses the Model-View-Controller (MVC) architecture, which separates the data (model), the UI (view), and the control logic (controller).
    • This separation allows for more modular and maintainable code.

 

Key Differences Between AWT and Swing

FeatureAWTSwing
Look and FeelPlatform-dependent (native look)Pluggable look-and-feel (consistent across platforms)
ComponentsBasic set of componentsRich set of advanced components
CustomizabilityLimited customizationHighly customizable
Component TypeHeavyweight (rely on native peers)Lightweight (purely Java)
Event HandlingOld event handling modelImproved event handling with delegation
Thread SafetyNot thread-safeNot thread-safe, but provides SwingUtilities.invokeLater for thread safety
MVC ArchitectureNo explicit support for MVCBuilt-in support for MVC architecture
PerformanceDependent on the native system performanceGenerally, better performance due to the lightweight nature
AppearanceNative appearanceConsistent appearance can mimic native or custom looks
DependenciesUses native OS componentsPure Java implementation

 

Hierarchy of Swing Component

The following figure shows the hierarchy of Swing components.

 

 

 


 

End of the lesson….enjoy learning.

 

 

Student Ratings and Reviews

 

 

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review