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 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) where model represents data, view represents presentation and controller acts as an interface between model and view. Swing follows MVC.

What is JFC

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

Do You Know

  1. How to create runnable jar file in java?
  2. How to display image on a button in swing?
  3. How to change the component color by choosing a color from ColorChooser ?
  4. How to display the digital watch in swing tutorial ?
  5. How to create a notepad in swing?
  6. How to create puzzle game and pic puzzle game in swing ?
  7. 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:

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


Flash Soon


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.


Flash Soon

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.


Flash Soon

What we will learn in Swing Tutorial

  1. JButton class
  2. JRadioButton class
  3. JTextArea class
  4. JComboBox class
  5. JTable class
  6. JColorChooser class
  7. JProgressBar class
  8. JSlider class
  9. Digital Watch
  10. Graphics in swing
  11. Displaying image
  12. Edit menu code for Notepad
  13. OpenDialog Box
  14. Notepad
  15. Puzzle Game
  16. Pic Puzzle Game
  17. Tic Tac Toe Game
  18. BorderLayout
  19. GridLayout
  20. FlowLayout
  21. CardLayout


© Sapster 2017