Explain the architecture of Java Swing in Java?

Java Swing is a set of APIs that provides a graphical user interface (GUI) for the java programs. The Java Swing was developed based on earlier APIs called Abstract Windows Toolkit (AWT). The Java Swing provides richer and more sophisticated GUI components than AWT. The GUI components are ranging from a simple level to complex tree and table. The Java Swing provides the pluggable look and feels to allow look and feel of Java programs independent from the underlying platform.

Features of Java Swing

The Java Swing is platform independent and follows the MVC (Model View and Controller) framework.

  • Pluggable look and feel − The Java Swing supports several looks and feels and currently supports Windows, UNIX, Motif, and native Java metal look and feel and allows users to switch look and feel at runtime without restarting the application. By doing this, users can make their own choice to choose which look and feel is the best for them instantly.
  • Lightweight components − All Java swing components are lightweight except for some top-level containers. A Lightweight means component renders or paints itself using drawing primitives of the Graphics object instead of relying on the host operating system (OS). As a result, the application presentation is rendered faster and consumed less memory than previous Java GUI applications like AWT.
  • Simplified MVC − The Java Swing uses a simplified Model-View-Controller architecture (MVC) as the core design behind each of its components called the model-delegate. Based on this architecture, each Java Swing component contains a model and a UI delegate and wraps a view and a controller in MVC architecture. The UI delegate is responsible for painting screen and handling GUI events. Model is in charge of maintaining information or states of the component.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// model part
class Model {
   private int x;
   public Model() {
      x = 0;
   public Model(int x) {
      this.x = x;
    public void setX(){
   public int getX() {
      return x;
// view part
class View {
   private JFrame frame;
   private JLabel label;
   private JButton button;
   public View(String text) {
      frame = new JFrame("View");
      frame.getContentPane().setLayout(new BorderLayout());
      label = new JLabel(text);
      frame.getContentPane().add(label, BorderLayout.CENTER);
      button = new JButton("Button");
      frame.getContentPane().add(button, BorderLayout.SOUTH);
   public JButton getButton() {
      return button;
   public void setText(String text) {
// controller part
class Controller {
   private Model model;
   private View view;
   private ActionListener actionListener;
   public Controller(Model model, View view) {
      this.model = model;
      this.view = view;
   public void contol() {
      actionListener = new ActionListener() {
         public void actionPerformed(ActionEvent actionEvent) {
   private void linkBtnAndLabel() {
// main class
public class Main {
   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            try {
// Look and Feel, Java Look and Feel
            } catch (Exception ex) { }
            Model model = new Model(0);
            View view = new View("-");
            Controller controller = new Controller(model,view);