0% found this document useful (0 votes)
7 views12 pages

Java Swing Tutorial - Javatpoint

The Java Swing tutorial explains how to create window-based applications using Java Foundation Classes (JFC) built on AWT. It highlights the differences between AWT and Swing, noting that Swing components are platform-independent and lightweight, while AWT components are heavyweight and platform-dependent. The document also provides examples of creating GUI components like buttons and frames in Swing, along with a list of commonly used methods and classes.

Uploaded by

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

Java Swing Tutorial - Javatpoint

The Java Swing tutorial explains how to create window-based applications using Java Foundation Classes (JFC) built on AWT. It highlights the differences between AWT and Swing, noting that Swing components are platform-independent and lightweight, while AWT components are heavyweight and platform-dependent. The document also provides examples of creating GUI components like buttons and frames in Swing, along with a list of commonly used methods and classes.

Uploaded by

manimaransa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Home Java Programs OOPs String Exception Multithreading Collections

ADVERTISEMENT

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 [Link] 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 feel. Swing supports pluggable look and
feel.

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

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


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.

Do You Know

How to create runnable jar file in java?

How to display image on a button in swing?

How to change the component color by choosing a color from ColorChooser ?

How to display the digital watch in swing tutorial ?

How to create a notepad in swing?

How to create puzzle game and pic puzzle game in swing ?

How to create tic tac toe game in swing ?

Hierarchy of Java Swing classes

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 width,int height) sets size of the component.

public void setLayout(LayoutManager m) sets the layout manager for the component.

public void setVisible(boolean b) sets the visibility of the component. It is by default false.

Java Swing Examples


There are two ways to create a frame:

ADVERTISEMENT ADVERTISEMENT
By creating the object of Frame class (association)
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.
ADVERTISEMENT ADVERTISEMENT

File: [Link]

import [Link].*;
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


[Link](130,100,100, 40);//x axis, y axis, width, height

[Link](b);//adding button in JFrame

[Link](400,500);//400 width and 500 height


[Link](null);//using no layout managers
[Link](true);//making the frame visible
}
}

Example of Swing by Association inside constructor

We can also write all the codes of creating JFrame, JButton and method call inside the java constructor.

File: [Link]

import [Link].*;
public class Simple {
JFrame f;
Simple(){
f=new JFrame();//creating instance of JFrame
JButton b=new JButton("click");//creating instance of JButton
[Link](130,100,100, 40);

[Link](b);//adding button in JFrame

[Link](400,500);//400 width and 500 height


[Link](null);//using no layout managers
[Link](true);//making the frame visible
}

public static void main(String[] args) {


new Simple();
}
}

The setBounds(int xaxis, int yaxis, int width, int height)is used in the above example that sets the position of
the button.

Simple example of Swing by inheritance

We can also inherit the JFrame class, so there is no need to create the instance of JFrame class explicitly.

File: [Link]

import [Link].*;
public class Simple2 extends JFrame{//inheriting JFrame
JFrame f;
Simple2(){
JButton b=new JButton("click");//create button
[Link](130,100,100, 40);

add(b);//adding button on frame


setSize(400,500);
setLayout(null);
setVisible(true);
}
public static void main(String[] args) {
new Simple2();
}}

download this example


What we will learn in Swing Tutorial

JButton class

JRadioButton class

JTextArea class

JComboBox class

JTable class

JColorChooser class

JProgressBar class

JSlider class

Digital Watch

Graphics in swing

Displaying image

Edit menu code for Notepad

OpenDialog Box

Notepad

Puzzle Game

Pic Puzzle Game

Tic Tac Toe Game

BorderLayout

GridLayout

FlowLayout

CardLayout
← Prev Next →

ADVERTISEMENT

For Videos Join Our Youtube Channel: Join Now

Feedback

Send your Feedback to feedback@[Link]

Help Others, Please Share

Learn Latest Tutorials

Splunk SPSS Swagger Transact-SQL

Tumblr ReactJS Regex Reinforcement


Learning
R Programming RxJS React Native Python Design
Patterns

Python Pillow Python Turtle Keras

Preparation

Aptitude Reasoning Verbal Ability Interview Questions

Company Questions

Trending Technologies

Artificial AWS Selenium Cloud Computing


Intelligence

Hadoop ReactJS Data Science Angular 7


Blockchain Git Machine Learning DevOps

ADVERTISEMENT

[Link] / MCA

DBMS Data Structures DAA Operating System

Computer Network Compiler Design Computer Discrete


Organization Mathematics

Ethical Hacking Computer Graphics Software Web Technology


Engineering

Cyber Security Automata C Programming C++


Java .Net Python Programs

Control System Data Mining Data Warehouse

ADVERTISEMENT ADVERTISEMENT

You might also like