Java Swing Tutorial

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Swing in Java is a framework, i.e. a pre-coded toolkit used by Java developers to create Java applications or web applications. All the swing classes are present in javax.swing package. Java Swing Framework is part of Java Foundation Classes (JFC); JFC is a set of libraries that supports programmers in creating applications using Java.

Swing in Java is built on top of AWT (Abstract Windowing Toolkit) Application Program Interface (API) and is entirely written in Java. Java is platform-independent, and Swing is written in Java, which makes Swing platform-independent and lightweight. The javax.swing package provides classes for swing API such as JTextField, JTextArea, JRadioButton, JMenu, JCheckbox, JColorChooser, JButton, JComboBox, JPanel, etc. All these are GUI components. Using Swing, one can develop a standalone desktop application.

Swing java graphics

Difference between Java Swing and AWT

AWTSwing
AWT components are platform-dependent.Swing is platform-independent.
Its components are heavy weighted as they depend on the underlying Operating System.Its components are lightweight.
AWT doesn't follow Model View Controller(MVC).Swing follows MVC. MVC represents data, the view represents presentation, and the controller acts as an interface between model and view.
It has fewer components than Swing.Swing not only has more components compared to AWT, but its components are also powerful such as tables, lists, scroll panes, colour chooser, etc.

Check the detailed comparison between Swing and AWT from here.

What is JFC?

The Java Foundation Classes (JFC) represent a comprehensive toolkit for building desktop applications with Java. While many perceive JFC and Swing as synonymous, JFC encompasses a broader range of functionalities beyond Swing. These include essential features like clipboard support for cut-and-paste operations, accessibility enhancements catering to users with disabilities, and the introduction of desktop colours in Java 1.1.

Moreover, JFC incorporates Java 2D, offering enhanced capabilities for handling colours, images, and text rendering. Thus, JFC is a robust framework for streamlining the development of Java-based desktop applications, offering a rich set of tools and components to facilitate the process.

Features of Java Swing Class

  • It is lightweight, platform-independent, customizable, and configurable.
  • Swing, with its pluggable look and feel and lightweight components, offers a versatile solution for building platform-independent GUI applications
  • Its MVC architecture ensures a clean separation of concerns, making maintaining and extending code easier.
  • With advanced features like JTable, JTabbedPane, JScrollPane, and more, developers have powerful tools for creating sophisticated user interfaces.
  • Swing applications can run seamlessly on any client machine.

Hierarchy of Java Swing Classes

The hierarchy of Java Swing API is given below:

Java Swing Class Hierarchy

The components in a swing, like JButton, JTable, JList, JMenu, JLabel, etc., are inherited from the JComponent class. JComponent class can be added to the container classes. The Container class's components are the main windows like frames and dialog boxes. Essential swing components are the constituents of any GUI application. Containers like JFrame and JDialog can only contain components inside them.

Commonly Used Methods of Component Class

The methods of a component class are widely used in Java Swing, which are given below.

  • public void add(Component c): It adds a component on another component.
  • public void setSize(int width,int height): It sets size of the component.
  • public void setLayout(LayoutManager m): It sets the layout manager for the component.
  • public void setVisible(boolean b): It sets the visibility of the component. It is, by default, false.

Java Swing Example

In the above example, we create a JFrame with Frame Title as the title, and set the frame size to 400X400.

Output:

Output of JFrame

More Examples of Java Swing Programs

Example 1

Using the above code, we create a label named heyImageLabel with no text. We create an ImageIcon object named heyimageIcon to add an image to the label. It takes the address of the image as the parameter. Using the setIcon method, we add an icon to the label heyImageLabel as seen in the output below.

Output:

Output of JLabel

Example 2

In this example, JTextField is created with the text Hey I am TextField, You can edit me!. It is inserted into the panel. This panel exists inside JFrame.

Output:

Output of JTextField

Example 3

We create a country JComboBox. It takes a String array as a parameter. This string array contains all the countries displayed when the user clicks on the arrow. Users can select any option.

Output:

output of JComboBox

Conclusion

  • Swing in java can be imported from javax.swing package.
  • Swing components are platform-independent.
  • Using Swing, building optimized GUI applications is easy.
  • Swing has many constructors such as JFrame, JPanel, JLabel, JTextBox, JPane, JComboBox, etc.
  • Swing allows us to create diverse desktop applications such as IP Finder, Calculator, URL Source Generator, Word Counter, Folder Explorer, NotePad, Tic Tac Toe Game, etc. For instance, Limewire is created using Java Swing.