Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
48 views

Java Swing Tutorial: Difference Between AWT and Swing

The document discusses Java Swing, including that it is a GUI toolkit built on top of AWT that provides platform-independent and lightweight components. It describes some key differences between AWT and Swing, lists common Swing components, shows the hierarchy of Swing classes, and provides examples of creating simple Swing applications by associating components with a JFrame in the main method or constructor.

Uploaded by

shamagondal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Java Swing Tutorial: Difference Between AWT and Swing

The document discusses Java Swing, including that it is a GUI toolkit built on top of AWT that provides platform-independent and lightweight components. It describes some key differences between AWT and Swing, lists common Swing components, shows the hierarchy of Swing classes, and provides examples of creating simple Swing applications by associating components with a JFrame in the main method or constructor.

Uploaded by

shamagondal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Ceh.arvind@gmail.

com JAVA-SWING 9996870071

Java Swing Tutorial


Java Swing tutorial is a part of Java Foundation Classes (JFC) that is used to create window-
based applications. It is built on the top of AWT (Abstract Windowing Toolkit) API and entirely
written in java.

Unlike AWT, Java Swing provides platform-independent and lightweight components.

The javax.swing package provides classes for java swing API such as JButton, JTextField,
JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.

Difference between AWT and Swing


There are many differences between java awt and swing that are given below.

No. Java AWT Java Swing

1) AWT components are platform-dependent. Java swing components are platform-


independent.

2) AWT components are heavyweight. Swing components are lightweight.

3) AWT doesn't support pluggable look and Swing supports pluggable look and
feel. feel.

4) AWT provides less components than Swing. Swing provides more powerful
componentssuch as tables, lists,
scrollpanes, colorchooser, tabbedpane etc.

5) AWT doesn't follows MVC(Model View Swing follows MVC.


Controller) where model represents data,
view represents presentation and controller
acts as an interface between model and view.

What is JFC
The Java Foundation Classes (JFC) are a set of GUI components which simplify the
development of desktop applications.

Hierarchy of Java Swing classes


Ceh.arvind@gmail.com JAVA-SWING 9996870071
The hierarchy of java swing API is given below.

Commonly used Methods of Component class


The methods of Component class are widely used in java swing that are given below.

Method Description

public void add(Component c) add a component on another component.

public void setSize(int sets size of the component.


width,int height)

public void sets the layout manager for the component.


setLayout(LayoutManager m)

public void setVisible(boolean sets the visibility of the component. It is by


b) default false.
Ceh.arvind@gmail.com JAVA-SWING 9996870071
Java Swing Examples
There are two ways to create a frame:

o By creating the object of Frame class (association)

o By extending Frame class (inheritance)

We can write the code of swing inside the main(), constructor or any other method.

Simple Java Swing Example


Let's see a simple swing example where we are creating one button and adding it on the
JFrame object inside the main() method.

File: FirstSwingExample.java

import javax.swing.*;
public class FirstSwingExample {
public static void main(String[] args) {
JFrame f=new JFrame();//creating instance of JFrame

JButton b=new JButton("click");//creating instance of JButton


b.setBounds(130,100,100, 40);//x axis, y axis, width, height

f.add(b);//adding button in JFrame

f.setSize(400,500);//400 width and 500 height


f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible
}
}

Example of Swing by Association inside constructor

You might also like