Advance Java – Introduction to Swing, MVC Architecture

Advance Java

Introduction to Swing, MVC Architecture, Applets, Applications and Pluggable Look and Feel, Basic swing components: Text Fields, Buttons, Toggle Buttons, Checkboxes, and Radio Buttons

What is Java Swing-

Java Swing – The AWT (Abstract Window Toolkit)  defines a basic set of controls, windows, and dialog boxes that support a usable, but limited graphical interface. To handle this Java Swing came in light which is a part of java foundation classes which is used to design window based application. Swing is a very large subsystem and makes use of many packages. At the time of this writing, these are the packages defined by Swing.

How to use Java Swing classes-

Like other packages of java – java.io, java.util, etc we use java swing as-

import javax.swing.*;

Difference between Java packages and Javax packages-

java.* packages are the core Java language packages, meaning that programmers using the Java language had to use them in order to make any worthwhile use of the java language.

javax.* packages are optional packages, which provides a standard, scalable way to make custom APIs available to all applications running on the Java platform. javax.servlet.*

Packages essential to the API was java, while javax contained the extensions to the API.

The asterisk indicates that all the class names in the package javax.swing are to be imported.

And there are various other Java Standard Packages which are being used in advance java like java.awt, java.awt.event, etc.

MVC Architecture

The design of the Swing component classes is loosely based on something called the Model–View-Controller architecture, or MVC.

MVC is a way of modeling to define a component as three parts-

  1. Model – It represents the data for the application like a button defined as ButtonModel.
  2. View – is the visual representation of that data. It is responsible for all UI dependent issues like layout, drawing, etc.
  3. Controller – takes user input on the view and translates that to changes in the model. It executes in order to a user interaction like mouse move, mouse click, etc means action or action listener.

Applets, Applications and Pluggable Look and Feel

Applets – Java enables us to write small programs called applets. These are the programs that you can embed in web pages to provide some intelligence.

  • Applets are not stand-alone programs.

Applets can’t read or write local files, check for the existence of files, execute programs on the local machine, etc. These restrictions are for security reasons.

There are two standard ways in which we can run an applet:

  1. Executing the applet within a Java-compatible web browser.
  2. Use an applet viewer to test your applet -> type at command line-

appletviewer apletdemo.html

To use applet in browsers we need to change few setting in browsers like google chrome, firefox, etc.

Applications-

The Java Platform consists of two elements – a software implementation of the Java Virtual Machine (JVM) and the Java Application Programming Interface (Java API), which is a set of software components that provides the facilities we need to write a fully fledged interactive application in Java.

When we install java these both components installed automatically.

All the softwares we use are known as applications.

Pluggable Look and Feel- In earlier versions of java the GUI (Graphical User Interface) was implemented as AWT (Abstract Window Toolkit). But AWT is having some limitations of working in different operating systems. To solve this issue Sun developed a system called Swing which having Pluggable Look and Feel Feature means a program on the Macintosh actually looks like a Macintosh program, and a Windows program looks like a Windows program. With a single change in a line of code you can turn a program from one Look and Feel to another.

Basic Swing Components-

Javax.swing provides classes like – JFrame, JPanel, JButton, JTextField, JTextArea, JRadioButton, JCheckBox, JMenu, etc which helps to design Text Fields, Buttons, Check Boxes, Radio Buttons, etc.  All these are known as JComponents.

Share Button

Be the first to comment

Leave a Reply

Your email address will not be published.


*