You are on page 1of 17

AWT Vs SWING

BY AMIT KUBADE PRN NO. 11012

INDEX
What IS AWT? WHAT IS SWING? DIFFERENCE BETWEEN AWT AND SWING COMPONENT OF AWT COMPONENT OF SWING EX. OF AWT EX. OF SWING

WHAT IS AWT?
The Abstract Window Toolkit is a portable GUI library.

AWT: classes in the java.awt package


AWT provides the connection between your application and the native GUI. AWT provides a high-level abstraction since it hides you from the underlying details of the GUI your program will be running on.

AWT components depend on native code counterparts (called peers) to handle their functionality. Thus, these components are often called heavyweight components.

WHAT IS SWING?
Swing implements GUI components that build on AWT technology. Swing: classes in the javax.swing package Swing is implemented entirely in Java. Swing components do not depend on peers to handle their functionality. Thus, these components are often called lightweight components.

DIFFERENCE BETWEEN AWT AND SWING(1)


AWT is simpler Swing is more flexible and better looking Mixing Swing and AWT components is not recommended Many of the common controls are just renamed
AWT: Button b = new Button ("OK"); Swing: JButton b = new JButton("OK");

Learning the AWT is a good start for learning Swing

DIFFERENCE BETWEEN AWT AND SWING(2)


AWT Buttons vs. Swing JButtons:
A Button is a Component A JButton is an AbstractButton, which is a JComponent, which is a Container, which is a Component Swing uses AWT Containers A Frame is a Window is a Container is a Component A JFrame is a Frame is a Container is a Component Swing uses the AWT layout managers, plus a couple of its own Swing uses many of the AWT listeners, plus a couple of its own

Containers:

AWT Frames vs. Swing JFrames:

Layout managers: Listeners:

Some AWT and Swing Classes


java.lang.Object java.awt.Component

java.awt.Container

javax.swing.JComponent java.awt.Window javax.swing.JPanel java.awt.Frame javax.swing.JFileChooser javax.swing.JPopupMenu javax.swing.JFrame javax.swing.JToolbar javax.swing.JLabel

AWT: Pros and Cons


Pros Speed: native components speed performance. Look and feel: AWT components more closely reflect the look and feel of the OS they run on. Cons Portability: use of native peers creates platform specific limitations. Features: AWT supports only the lowest common denominatore.g. no tool tips or icons.

Swing: Pros and Cons


Pros

Portability: Pure Java implementation. Features: Not limited by native components. Look and Feel: Pluggable look and feel. Components automatically have the look and feel of the OS their running on.
Cons

Performance: Swing components handle their own painting (instead of using APIs like DirectX on Windows). Look and Feel: May look slightly different than native components.

COMPONENTS OF AWT
Labels place text on the screen Buttons are used very often to trigger actions and other events. Check Boxes allow choices

Radio Buttons are a special case of check boxes.


List Boxes are ideal for interacting with arrays of Strings Choice Boxes are similar to list Boxes but conserve Space. Text Areas allow the user to enter data.

COMPONENTS OF SWING(1)

COMPONENTS OF SWING(2)

Lecture 4: Swing

12

COMPONENTS OF SWING(3)
MENUS

JMenuBar

Lecture 4: Swing

13

BASIC EXAMPLE FOR AWT(1)


import java.applet.Applet; import java.awt.*; public class SumNumsInteractive extends Applet { TextField text1, text2; public void init() { text1 = new TextField(10); text2 = new TextField(10); text1.setText("0"); text2.setText("0"); add(text1); add(text2); }

BASIC EXAMPLE FOR AWT(2)


public void paint(Graphics g) { int num1 = 0; int num2 = 0; int sum; String s1, s2, s3; g.drawString("Input a number in each box ", 10, 50); try { s1 = text1.getText(); num1 = Integer.parseInt(s1); s2 = text2.getText(); num2 = Integer.parseInt(s2); } catch(Exception e1) {} sum = num1 + num2; String str = "THE SUM IS: "+String.valueOf(sum); g.drawString (str,100, 125); }

BASIC PROGRAM FOR AWT(3)


public boolean action(Event ev, Object obj) { repaint(); return true; } }

BASIC PROGRAM FOR SWING


import javax.swing.*;
public class HelloWorldSwing { public static void main(String[] args) { JFrame frame = new JFrame("HelloWorldSwing"); final JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }