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.

Example

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(){
      x++;
   }
   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());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(200,200);
      frame.setVisible(true);
      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) {
      label.setText(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) {
            linkBtnAndLabel();
         }
      };
      view.getButton().addActionListener(actionListener);
   }
   private void linkBtnAndLabel() {
      model.setX();
      view.setText(Integer.toString(model.getX()));
   }
}
// main class
public class Main {
   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         @Override
         public void run() {
            try {
// Look and Feel, Java Look and Feel
               UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception ex) { }
            Model model = new Model(0);
            View view = new View("-");
            Controller controller = new Controller(model,view);
            controller.contol();
         }
      });
   }
}

Output

Updated on: 24-Feb-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements