Unit 5
Unit 5
Java AWT is an API to develop GUI Swing is a part of Java Foundation Classes and is
1.
applications in Java used to create various applications.
The components of Java AWT are heavy The components of Java Swing are light
2.
weighted. weighted.
Java AWT has comparatively less Java Swing has more functionality as compared
3.
functionality as compared to Swing. to AWT.
The components of Java AWT are The components of Java Swing are platform
5.
platform dependent. independent.
AWT is a thin layer of code on top of the Swing is much larger swing also has very much
9
operating system. richer functionality.
Awt controls
Label
1
A Label object is a component for placing text in a container.
Button
2
This class creates a labeled button.
Check Box
3
A check box is a graphical component that can be in either an on (true) or off (false) state.
List
5
The List component presents the user with a scrolling list of text items.
Text Field
6
A TextField object is a text component that allows for the editing of a single line of text.
Text Area
7
A TextArea object is a text component that allows for the editing of a multiple lines of text.
Choice
8 A Choice control is used to show pop up menu of choices. Selected choice is shown on the top of the
menu.
Canvas
9 A Canvas control represents a rectangular area where application can draw something or can receive
inputs created by user.
Image
10
An Image control is superclass for all image classes representing graphical images.
Scroll Bar
11 A Scrollbar control represents a scroll bar component in order to enable user to select from range of
values.
Dialog
12 A Dialog control represents a top-level window with a title and a border used to take some form of
input from the user.
File Dialog
13 A FileDialog control represents a dialog window from which the user can select a file.
Layout manager
o What is a Layout Manager in Java?
o The class that is responsible for determining the size and position of each
component within a container based on a set of rules or algorithms is called a layout
manager in java. The layout manager in java takes into account the size of the
container and the preferred size of the components, as well as any constraints that
have been set for the layout.
o Regardless of the size or shape of the container, the main goal of a layout manager
in java is to make sure that the components are arranged in an aesthetically pleasing
and practical way. Complex GUIs (Graphical User Interfaces) with numerous
components arranged in specific ways can be made using layout managers.
o Types of the Layout Manager in Java
o Java provides several built-in layout managers that can be used to arrange
components within a container, which are listed below:
Border Layout: The layout manager BorderLayout divides the container’s five regions into
the north, south, east, west, and center. When a component is added to the container, it is
put in one of these regions and fills the entire region. The old component is replaced by the
new one if a component is added to a region that already has one.
Flow Layout: A layout manager called FlowLayout arranges components in a row, adding
additional rows as needed when the width of the container is exceeded. From left to right,
the components are added, with the next component being added directly to the right of the
one before it.
Grid Layout: The layout manager GridLayout arranges elements in a grid of rows and
columns. The layout manager is created with a specified number of rows and columns, and
components are added one at a time, filling each grid cell from left to right and from top to
bottom.
GridBag Layout: A layout manager called GridBagLayout arranges elements in a versatile grid
of rows and columns. Components, as opposed to GridLayout, can span multiple rows or
columns and be positioned in specific places within the grid. Because of this, GridBagLayout
is not only more capable and flexible than other layout managers but also trickier to use.
Card Layout: CardLayout is a layout manager that enables switching between multiple
components while keeping them in the same container by using functions like next() and
previous (). A single component can be seen at a time, and the CardLayout can be used to
switch between various views of the same data or to create an interface that looks like a
wizard.
Group Layout: Java 6’s GroupLayout layout manager enables programmers to specify a
container’s layout by building a nested hierarchy of groups. In order to regulate the size and
placement of the components within the container, the developer can specify constraints for
each group, which each group can contain other groups or components. Although
GroupLayout is a more capable and adaptable layout manager than other layout managers,
it is also trickier to use.
RMI
The RMI (Remote Method Invocation) is an API that provides a mechanism to create
distributed application in java. The RMI allows an object to invoke methods on an object
running in another JVM.
The RMI provides remote communication between the applications using two objects stub
and skeleton.
Understanding stub and skeleton
RMI uses stub and skeleton object for communication with the remote object.
A remote object is an object whose method can be invoked from another JVM. Let's
understand the stub and skeleton objects:
stub
The stub is an object, acts as a gateway for the client side. All the outgoing requests are routed
through it. It resides at the client side and represents the remote object. When the caller invokes
method on the stub object, it does the following tasks:
2. It writes and transmits (marshals) the parameters to the remote Virtual Machine (JVM),
skeleton
The skeleton is an object, acts as a gateway for the server side object. All the incoming requests
are routed through it. When the skeleton receives the incoming request, it does the following
tasks:
Compile the implementation class and create the stub and skeleton objects using the rmic tool
AWT provides various components like button, label, checkbox, etc. used as objects inside
a Java Program. AWT components use the resources of the operating system, i.e., they are
platform-dependent, which means, component's view can be changed according to the view of
the operating system. The classes for AWT are provided by the Java.awt package for various
AWT components.
Two types of garbage collection activity usually happen in Java. These are:
import java.io.*;
// A test class whose objects are cloned
class Test {
int x, y;
Test()
{
x = 10;
y = 20;
}
}
// Driver Class
class Main {
public static void main(String[] args)
{
Test ob1 = new Test();
- Create a Java interface that defines the methods that can be called remotely.
- Extend the Remote interface and throw RemoteException for each method.
- Test the RMI application by calling the methods on the remote object from the client.
- Verify that the methods are executed on the server machine.
Note: Make sure that the server and client machines are connected to the same network and that
the RMI registry is accessible from both machines.
Remote Interface
Remote Implementation
RMI Server
RMI Client
JAR (Java Archive) files are used to package Java classes, libraries, and resources into a single
file for distribution and deployment. Here are some key points about JAR files:
A JAR file is a compressed file that contains Java classes, libraries, and resources.
How is a JAR file created?
A JAR file can be created using the jar command in the command line or using an IDE like Eclipse or
IntelliJ.
- META-INF directory: contains metadata about the JAR file, such as the manifest file.
- Manifest file (META-INF/MANIFEST.MF): contains information about the JAR file, such as the main
class and dependencies.
- Class files: contains the compiled Java classes.
- Resource files: contains resources, such as images and configuration files.
A JAR file can be executed using the java command with the -jar option, followed by the name of the
JAR file.
jAR files are commonly used for: