Awt MVC
Awt MVC
Java AWT (Abstract Window Toolkit) is an API to develop Graphical User Interface (GUI) or
windows-based applications in Java.
Java AWT components are platform-dependent i.e. components are displayed according to the
view of operating system. AWT is heavy weight i.e. its components are using the resources of
underlying operating system (OS).
The java.awt package provides classes for AWT API such as TextField, Label, TextArea,
RadioButton, CheckBox, Choice, List etc.
The AWT tutorial will help the user to understand Java GUI programming in simple and easy
steps.
ADVERTISEMENT
Java AWT calls the native platform calls the native platform (operating systems) subroutine for
creating API components like TextField, ChechBox, button, etc.
For example, an AWT GUI with components like TextField, label and button will have different
look and feel for the different platforms like Windows, MAC OS, and Unix. The reason for this
is the platforms have different view for their native components and AWT directly calls the
native subroutine that creates those components.
In simple words, an AWT application will look like a windows application in Windows OS
whereas it will look like a Mac application in the MAC OS.
All the elements like the button, text fields, scroll bars, etc. are called components. In Java AWT,
there are classes for each component as shown in above diagram. In order to place every
component in a particular position on a screen, we need to add them to a container.
Container
The Container is a component in AWT that can contain another components like buttons,
textfields, labels etc. The classes that extends Container class are known as container such
as Frame, Dialog and Panel.
ADVERTISEMENT
ADVERTISEMENT
It is basically a screen where the where the components are placed at their specific locations.
Thus it contains and controls the layout of components.
Note: A container itself is a component (see the above diagram), therefore we can add a
container inside container.
Types of containers:
1. Window
2. Panel
3. Frame
4. Dialog
Window
The window is the container that have no borders and menu bars. You must use frame, dialog or
another window for creating a window. We need to create an instance of Window class to create
this container.
Panel
The Panel is the container that doesn't contain title bar, border or menu bar. It is generic
container for holding the components. It can have other components like button, text field etc.
An instance of Panel class creates a container, in which we can add components.
Frame
The Frame is the container that contain title bar and border and can have menu bars. It can have
other components like button, text field, scrollbar etc. Frame is most widely used container while
developing an AWT application.
Method Description
public void add(Component c) Inserts a component on
this component.
public void setSize(int Sets the size (width and
width,int height) height) of the component.
public void Defines the layout
setLayout(LayoutManager m) manager for the
component.
public void setVisible(boolean Changes the visibility of
status) the component, by
default false.
Java AWT Example
To create simple AWT example, you need a frame. There are two ways to create a GUI using
Frame in AWT.
ADVERTISEMENT
ADVERTISEMENT
1. By extending Frame class (inheritance)
2. By creating the object of Frame class (association)
ADVERTISEMENT
Let's see a simple example of AWT where we are inheriting Frame class. Here, we are showing
Button component on the Frame.
AWTExample1.java
The setBounds(int x-axis, int y-axis, int width, int height) method is used in the above example
that sets the position of the awt button.
ADVERTISEMENT
Output:
Let's see a simple example of AWT where we are creating instance of Frame class. Here, we are
creating a TextField, Label and Button component on the Frame.
ADVERTISEMENT
ADVERTISEMENT
AWTExample2.java
Output:
The MVC design pattern is a software architecture pattern that separates an application into three
main components: Model, View, and Controller, making it easier to manage and maintain the
codebase. It also allows for the reusability of components and promotes a more modular
approach to software development.
Important Topics for the MVC Design Pattern
What is the MVC Design Pattern?
Components of the MVC Design Pattern
Communication between the components
Example of the MVC Design Pattern
Advantages of the MVC Design Pattern
Disadvantages of the MVC Design Pattern
What is the MVC Design Pattern?
The Model View Controller (MVC) design pattern specifies that an application consists of a
data model, presentation information, and control information. The pattern requires that each of
these be separated into different objects.
The MVC pattern separates the concerns of an application into three distinct components, each
responsible for a specific aspect of the application’s functionality.
This separation of concerns makes the application easier to maintain and extend, as changes to
one component do not require changes to the other components.
Components of the MVC Design Pattern
1. Model
The Model component in the MVC (Model-View-Controller) design pattern represents the data
and business logic of an application. It is responsible for managing the application’s data,
processing business rules, and responding to requests for information from other components,
such as the View and the Controller.
2. View
Displays the data from the Model to the user and sends user inputs to the Controller. It is passive
and does not directly interact with the Model. Instead, it receives data from the Model and sends
user inputs to the Controller for processing.
3. Controller
Controller acts as an intermediary between the Model and the View. It handles user input and
updates the Model accordingly and updates the View to reflect changes in the Model. It contains
application logic, such as input validation and data transformation.
Communication between the components
This below communication flow ensures that each component is responsible for a specific aspect
of the application’s functionality, leading to a more maintainable and scalable architecture
User Interaction with View:
o The user interacts with the View, such as clicking a button or entering text into a
form.
View Receives User Input:
o The View receives the user input and forwards it to the Controller.
Controller Processes User Input:
o The Controller receives the user input from the View.
o It interprets the input, performs any necessary operations (such as updating the
Model), and decides how to respond.
Controller Updates Model:
o The Controller updates the Model based on the user input or application logic.
Model Notifies View of Changes:
o If the Model changes, it notifies the View.
View Requests Data from Model:
o The View requests data from the Model to update its display.
Controller Updates View:
o The Controller updates the View based on the changes in the Model or in response to
user input.
View Renders Updated UI:
o The View renders the updated UI based on the changes made by the Controller.
Example of the MVC Design Pattern
Below is the code of above problem statement using MVC Design Pattern:
Let’s break down into the component wise code:
1. Model (Student class)
Represents the data (student’s name and roll number) and provides methods to access and
modify this data.
Java
class Student {
return rollNo;
this.rollNo = rollNo;
return name;
this.name = name;
class StudentView {
System.out.println("Student:");
System.out.println("Name: " +
studentName);
class StudentController {
this.model = model;
this.view = view;
model.setName(name);
return model.getName();
}
public void setStudentRollNo(String rollNo) {
model.setRollNo(rollNo);
return model.getRollNo();
view.printStudentDetails(model.getName(), model.getRollNo());
class Student {
return rollNo;
return name;
this.name = name;
class StudentView {
System.out.println("Student:");
class StudentController {
this.view = view;
model.setName(name);
return model.getName();
model.setRollNo(rollNo);
return model.getRollNo();
view.printStudentDetails(model.getName(), model.getRollNo());
}
public class MVCPattern {
controller.updateView();
controller.setStudentName("Vikram Sharma");
controller.updateView();
student.setName("Lokesh Sharma");
student.setRollNo("15UCS157");
return student;
Output
Student:
Name: Lokesh Sharma
Student:
gridx, gridy
Specify the row and column at the upper left of the component. The leftmost
column has address gridx=0, and the top row has address gridy=0.
Use GridBagConstraints.RELATIVE (the default value) to specify that the
component be placed just to the right of (for gridx) or just below (for gridy)
the component that was added to the container just before this component
was added.
gridwidth, gridheight
Specify the number of columns (for gridwidth) or rows (for gridheight) in the
component's display area. These constraints specify the number of cells the
component uses, not the number of pixels it uses. The default value is 1.
Use GridBagConstraints.REMAINDER to specify that the component be the last
one in its row (for gridwidth) or column (for gridheight).
Use GridBagConstraints.RELATIVE to specify that the component be the next
to last one in its row (for gridwidth) or column (for gridheight).
Note: Due to a bug in the the 1.0 release of Java, GridBagLayout doesn't allow
components to span multiple rows unless the component is in the leftmost
column.
fill
Used when the component's display area is larger than the component's
requested size to determine whether and how to resize the component. Valid
values are GridBagConstraints.NONE (the
default), GridBagConstraints.HORIZONTAL (make the component wide enough
to fill its display area horizontally, but don't change its
height), GridBagConstraints.VERTICAL (make the component tall enough to fill
its display area vertically, but don't change its width),
and GridBagConstraints.BOTH (make the component fill its display area
entirely).
ipadx, ipady
Specifies the internal padding: how much to add to the minimum size of the
component. The default value is zero. The width of the component will be at
least its minimum width plus ipadx*2 pixels (since the padding applies to both
sides of the component). Similarly, the height of the component will be at
least its minimum height plus ipady*2 pixels.
insets
Used when the component is smaller than its display area to determine where
(within the area) to place the component. Valid values
are GridBagConstraints.CENTER (the
default), GridBagConstraints.NORTH, GridBagConstraints.NORTHEAST, GridBagCo
nstraints.EAST, GridBagConstraints.SOUTHEAST, GridBagConstraints.SOUTH, Gr
idBagConstraints.SOUTHWEST, GridBagConstraints.WEST,
and GridBagConstraints.NORTHWEST.
weightx, weighty
Unless you specify at least one nonzero value for weightx or weighty, all the
components clump together in the center of their container. This is because
when the weight is 0.0 (the default), the GridBagLayout puts any extra space
between its grid of cells and the edges of the container.
Generally weights are specified with 0.0 and 1.0 as the extremes, with numbers
in between used as necessary. Larger numbers indicate that the component's
row or column should get more space. For each column, the weight is related to
the highest weightx specified for a component within that column (with each
multi-column component's weight being split somehow between the columns
the component is in). Similarly, each row's weight is related to the
highest weighty specified for a component within that row. Extra space tends to
go toward the rightmost column and bottom row.