UNIT-V
GUI PROGRAMMING WITH SWING
MVC Architecture in Java:
The Model-View-Controller (MVC) is a well-known design pattern in the web development field. It is way to organize
our code. It specifies that a program or application shall consist of data model, presentation information and control
information. The MVC pattern needs all these components to be separated as different objects.
The model designs based on the MVC architecture follow MVC design pattern. The application logic is separated
from the user interface while designing the software using model designs.
The MVC pattern architecture consists of three layers:
Model: It represents the business layer of application. It is an object to carry the data that can also
contain the logic to update controller if data is changed.
View: It represents the presentation layer of application. It is used to visualize the data that the model
contains.
Controller: It works on both the model and view. It is used to manage the flow of application, i.e.
data flow in the model object and to update the view whenever data is changed.
In Java Programming, the Model contains the simple Java classes, the View used to display the data and the
Controller contains the servlets.
Due to this separation the user requests are processed as follows:
1. A client (browser) sends a request to the controller on the server side, for a page.
2. The controller then calls the model. It gathers the requested data.
3. Then the controller transfers the data retrieved to the view layer.
4. Now the result is sent back to the browser (client) by the view.
1|P a ge
Advantages of MVC Architecture:
The advantages of MVC architecture are as follows:
1. MVC has the feature of scalability that in turn helps the growth of application.
2. The components are easy to maintain because there is less dependency.
3. A model can be reused by multiple views that provides reusability of code.
4. The developers can work with the three layers (Model, View, and Controller) simultaneously.
5. Using MVC, the application becomes more understandable.
6. The extending and testing of application is easier.
Applications of MVC Architecture:
1. Web Development
2. Mobile App Development
3. Desktop Applications
4. Enterprise Software
5. Content Management Systems
Components of an MVC Framework:
Here are the components of MVC architecture in Java:
Model:
1. Data Access Object (DAO): Interacts with database.
2. Business Logic: Encapsulates data processing and validation.
3. Data Model: Represents data structures (e.g., User, Product).
4. Service Layer: Provides business logic APIs.
View:
1. User Interface (UI): Renders HTML, CSS, JavaScript.
2. Java Server Pages (JSP): Dynamic web pages.
3. Java Servlets: Handle HTTP requests.
4. Template Engines: Free marker, Velocity.
Controller:
1. Servlets: Handle HTTP requests.
2. Action Classes: Process user input.
3. Controller Classes: Manage Model-View interactions.
4. Request/Response Handling: HttpServletRequest, HttpServletResponse.
Types of Layout Manager in Java
In Java, graphical user interfaces (GUIs) play a vital role in creating interactive applications.
Layout managers define how components are arranged within a container, such as a JFrame or JPanel.
2|P a ge
1. Flow Layout:
Flow Layout is a simple layout manager that arranges components in a row, left to right, wrapping to the
next line as needed. It is ideal for scenarios where components need to maintain their natural sizes and
maintain a flow-like structure.
2. Border Layout:
Border Layout divides the container into five regions: NORTH, SOUTH, EAST, WEST, and CENTER.
Components can be added to these regions, and they will occupy the available space accordingly. This
layout manager is suitable for creating interfaces with distinct sections, such as a title bar, content area, and
status bar.
3. Grid Layout:
Grid Layout arranges components in a grid with a specified number of rows and columns. Each cell in the
grid can hold a component. This layout manager is ideal for creating a uniform grid of components, such as
a calculator or a game board.
4. Card Layout:
Card Layout allows components to be stacked on top of each other, like a deck of cards. Only one
component is visible at a time, and you can switch between components using methods like next() and
previous(). This layout is useful for creating wizards or multi-step processes.
5. Group Layout:
Group Layout is a versatile and complex layout manager that provides precise control over the positioning
and sizing of components. It arranges components in a hierarchical manner using groups. Group Layout is
commonly used in GUI builders like the one in NetBeans IDE.
3|P a ge
6. Grid Bag Layout:
GridBagLayout is a powerful layout manager that allows you to create complex layouts by specifying
constraints for each component. It arranges components in a grid, but unlike GridLayout, it allows
components to span multiple rows and columns and have varying sizes.
EVENT HANDLING
Delegation Event Model in Java:
o The Delegation Event model is defined to handle events in GUI programming languages.
o The GUI stands for Graphical User Interface, where a user graphically/visually interacts with the
system.
o The GUI programming is inherently event-driven.
o whenever a user initiates an activity such as a mouse activity, clicks, scrolling, etc.,
o Each is known as an event that is mapped to a code to respond to functionality to the user.
o This is known as event handling.
Basically, an Event Model is based on the following three components:
1. Events
2. Events Sources
3. Events Listeners
1.Events:
The Events are the objects that define state change in a source.
An event can be generated as a reaction of a user while interacting with GUI elements.
Some of the event generation activities are moving the mouse pointer, clicking on a button,
pressing the keyboard key etc,.
2.Event Sources:
A source is an object that causes and generates an event.
It generates an event when the internal state of the object is changed.
A source must register a listener to receive notifications for a specific event.
Each event contains its registration method.
For example, for a keyboard event listener, the method will be called as
addKeyListener(). For the mouse event listener, the method will be called as
addMouseMotionListener ( ).
4|P a ge
3.Event Listeners:
An event listener is an object that is invoked when an event triggers.
The listeners require two things; first, it must be registered with a source; however, it can be
registered with several resources to receive notification about the events.
Second, it must implement the methods to receive and process the received notifications.
Event classes in java:
Methods inherited:
This class inherits methods from the following classes:
java.lang.Object
Event Classes:
Following is the list of commonly used event classes.
Sr.
Control &Description
No.
AWTEvent:
1 It is the root event class for all AWT events. This class and its subclasses
supercede the original java.awt.Event class.
ActionEvent:
2 The ActionEvent is generated when button is clicked or the item of a list is
double clicked.
5|P a ge
InputEvent:
3
The InputEvent class is root event class for all component-level input events.
KeyEvent:
4
On entering the character the Key event is generated.
MouseEvent:
5
This event indicates a mouse action occurred in a component.
TextEvent:
6
The object of this class represents the text events.
WindowEvent:
7
The object of this class represents the change in state of a window.
AdjustmentEvent:
8 The object of this class represents the adjustment event emitted by
Adjustable objects.
ComponentEvent:
9
The object of this class represents the change in state of a window.
ContainerEvent:
10
The object of this class represents the change in state of a window.
MouseMotionEvent:
11
The object of this class represents the change in state of a window.
PaintEvent:
12
The object of this class represents the change in state of a window.
6|P a ge
Handling Mouse and Keyboard Events:
Handling Mouse Events:
To handle mouse events, we must implement one of the appropriate interfaces as follows:
1) MouseListener
2) MouseMotionListener
3) MouseWheelListener
•If any one of the above interfaces is implemented, we must provide implementations for all the
methods available in that interface.
•Methods available in “MouseListener” interface are:
1) mouseClicked()
2) mousePressed()
3) mouseReleased()
4) mouseEntered()
5) mouseExited()
•Methods available in “MouseMotionListener” interface are:
1) mouseMoved()
2) mouseDragged()
•Methods available in “MouseWheelListener” are:
1) mouseWheelMoved()
•We can get the x-coordinate and y-coordinate where the mouse is clicked by using two methods
available in “MouseEvent” class. Those methods are:
1) int getX() – To get the x-coordinate
2) int getY() – To get the y-coordinate
Handling Keyboard Events:
•To handle keyboard events, we must implement the “KeyListener” interface.
•When the “KeyListener” interface is implemented, we must provide implementations for three
methods available in that interface. They are:
1) keyPressed()
2) keyReleased()
3) keyTyped()
Types of Adopter classes in java:
7|P a ge
Advantages of anonymous Inner classes:
***********
8|P a ge