Search This Blog

ADVANCED JAVA

C-PROGRAMMING

Wednesday, December 28, 2022

AWT and Swing in Java

 

AWT and Swing in Java

AWT and Swing are used to develop window-based applications in Java. Awt is an abstract window toolkit that provides various component classes like Label, Button, TextField, etc., to show window components on the screen. All these classes are part of the Java.awt package.

On the other hand, Swing is the part of JFC (Java Foundation Classes) built on the top of AWT and written entirely in Java. The javax.swing API provides all the component classes like JButton, JTextField, JCheckbox, JMenu, etc.

The components of Swing are platform-independent, i.e., swing doesn't depend on the operating system to show the components. Also, the Swing's components are lightweight. The main differences between AWT and Swing are given in the following table.

ContextAWTSwing
API PackageThe AWT Component classes are provided by the java.awt package.The Swing component classes are provided by the javax.swing package.
Operating SystemThe Components used in AWT are mainly dependent on the operating system.The Components used in Swing are not dependent on the operating system. It is completely scripted in Java.
WeightinessThe AWT is heavyweight since it uses the resources of the operating system.The Swing is mostly lightweight since it doesn't need any Operating system object for processing. The Swing Components are built on the top of AWT.
AppearanceThe Appearance of AWT Components is mainly not configurable. It generally depends on the operating system's look and feels.The Swing Components are configurable and mainly support pluggable look and feel.
Number of ComponentsThe Java AWT provides a smaller number of components in comparison to Swing.Java Swing provides a greater number of components than AWT, such as list, scroll panes, tables, color choosers, etc.
Full-FormJava AWT stands for Abstract Window Toolkit.Java Swing is mainly referred to as Java Foundation Classes (JFC).
PeersJava AWT has 21 peers. There is one peer for each control and one peer for the dialogue. Peers are provided by the operating system in the form of widgets themselves.Java Swing has only one peer in the form of OS's window object, which provides the drawing surface used to draw the Swing's widgets (label, button, entry fields, etc.) developed directly by Java Swing Package.
Functionality and ImplementationJava AWT many features that are completely developed by the developer. It serves as a thin layer of development on the top of the OS.Swing components provide the higher-level inbuilt functions for the developer that facilitates the coder to write less code.
MemoryJava AWT needs a higher amount of memory for the execution.Java Swing needs less memory space as compared to Java AWT.
SpeedJava AWT is slower than swing in terms of performance.Java Swing is faster than the AWT.

Java Swing Hierarchy

Java defines the class hierarchy for all the Swing Components, which is shown in the following image.

AWT and Swing in Java

Java Swing Example

In the following example, we have created a User form by using the swing component classes provided by the javax.swing package. Consider the example.

  1. import javax.swing.*;  
  2. public class SwingApp {  
  3. SwingApp(){  
  4. JFrame f = new JFrame();  
  5.   
  6. JLabel firstName = new JLabel("First Name");  
  7. firstName.setBounds(20508020);  
  8.   
  9. JLabel lastName = new JLabel("Last Name");  
  10. lastName.setBounds(20808020);  
  11.   
  12. JLabel dob = new JLabel("Date of Birth");  
  13. dob.setBounds(201108020);  
  14.   
  15. JTextField firstNameTF = new JTextField();  
  16. firstNameTF.setBounds(1205010020);  
  17.   
  18. JTextField lastNameTF = new JTextField();  
  19. lastNameTF.setBounds(1208010020);  
  20.   
  21. JTextField dobTF = new JTextField();  
  22. dobTF.setBounds(12011010020);  
  23.   
  24. JButton sbmt = new JButton("Submit");  
  25. sbmt.setBounds(2016010030);  
  26.   
  27. JButton reset = new JButton("Reset");  
  28. reset.setBounds(120,160,100,30);  
  29.   
  30. f.add(firstName);  
  31. f.add(lastName);  
  32. f.add(dob);  
  33. f.add(firstNameTF);  
  34. f.add(lastNameTF);  
  35. f.add(dobTF);  
  36. f.add(sbmt);  
  37. f.add(reset);  
  38.   
  39. f.setSize(300,300);  
  40. f.setLayout(null);  
  41. f.setVisible(true);  
  42. }  
  43.   
  44. public static void main(String[] args) {  
  45. // TODO Auto-generated method stub  
  46. SwingApp s = new SwingApp();  
  47. }  
  48. }  

Output:

AWT and Swing in Java

Java awt Example

To understand the differences between Awt and Swing, we have created the same example in awt as well. Consider the following example.

  1. import java.awt.*;  
  2. public class AwtApp extends Frame {  
  3. AwtApp(){  
  4. Label firstName = new Label("First Name");  
  5. firstName.setBounds(20508020);  
  6.   
  7. Label lastName = new Label("Last Name");  
  8. lastName.setBounds(20808020);  
  9.   
  10. Label dob = new Label("Date of Birth");  
  11. dob.setBounds(201108020);  
  12.   
  13. TextField firstNameTF = new TextField();  
  14. firstNameTF.setBounds(1205010020);  
  15.   
  16. TextField lastNameTF = new TextField();  
  17. lastNameTF.setBounds(1208010020);  
  18.   
  19. TextField dobTF = new TextField();  
  20. dobTF.setBounds(12011010020);  
  21.   
  22. Button sbmt = new Button("Submit");  
  23. sbmt.setBounds(2016010030);  
  24.   
  25. Button reset = new Button("Reset");  
  26. reset.setBounds(120,160,100,30);  
  27.   
  28. add(firstName);  
  29. add(lastName);  
  30. add(dob);  
  31. add(firstNameTF);  
  32. add(lastNameTF);  
  33. add(dobTF);  
  34. add(sbmt);  
  35. add(reset);  
  36.   
  37. setSize(300,300);  
  38. setLayout(null);  
  39. setVisible(true);  
  40. }  
  41. public static void main(String[] args) {  
  42. // TODO Auto-generated method stub  
  43. AwtApp awt = new AwtApp();  
  44.   
  45. }  

Output:

AWT and Swing in Java

0 comments:

Post a Comment

OS

Top