0% found this document useful (0 votes)
14 views

Unit 5

g

Uploaded by

shreyaaaaa.06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Unit 5

g

Uploaded by

shreyaaaaa.06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Unit – 5

S.NO AWT Swing

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 execution time of AWT is more than


4. The execution time of Swing is less than AWT.
Swing.

The components of Java AWT are The components of Java Swing are platform
5.
platform dependent. independent.

6. MVC pattern is not supported by AWT. MVC pattern is supported by Swing.

AWT provides comparatively less


7. Swing provides more powerful components.
powerful components.

AWT components require java.awt Swing components requires javax.swing


8
package package

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.

 Wrapper Classes in Java


 A Wrapper class in Java is a class whose object wraps or contains primitive data types.
 When we create an object to a wrapper class, it contains a field and in this field, we can
store primitive data types.
 In other words, we can wrap a primitive value into a wrapper class object. Let’s check on the
wrapper classes in Java with examples
 They convert primitive data types into objects. Objects are needed if we wish to modify the
arguments passed into a method.

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.

Check Box Group


4
The CheckboxGroup class is used to group the set of checkbox.

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:

1. It initiates a connection with remote Virtual Machine (JVM),

2. It writes and transmits (marshals) the parameters to the remote Virtual Machine (JVM),

3. It waits for the result

4. It reads (unmarshals) the return value or exception, and

5. It finally, returns the value to the caller.

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:

1. It reads the parameter for the remote method

2. It invokes the method on the actual remote object, and

3. It writes and transmits (marshals) the result to the caller.


In the Java 2 SDK, an stub protocol was introduced that eliminates the need for
skeletons.

Java RMI Example

The is given the 6 steps to write the RMI program.

Create the remote interface

Provide the implementation of the remote interface

Compile the implementation class and create the stub and skeleton objects using the rmic tool

AWT Program in Java


AWT stands for Abstract window toolkit is an Application programming interface (API) for
creating Graphical User Interface (GUI) in Java. It allows Java programmers to develop window-
based applications.

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.

The following image represents the hierarchy for Java AWT.


import java.awt.*;

public class AwtProgram1 {


public AwtProgram1()
{
Frame f = new Frame();
Button btn=new Button("Hello World");
btn.setBounds(80, 80, 100, 50);
f.add(btn); //adding a new Button.
f.setSize(300, 250); //setting size.
f.setTitle("JavaTPoint"); //setting title.
f.setLayout(null); //set default layout for frame.
f.setVisible(true); //set frame visibility true.
}

public static void main(String[] args) {


// TODO Auto-generated method stub

AwtProgram1 awt = new AwtProgram1(); //creating a frame.


}
}
Garbage Collection in Java
 Garbage collection in Java is the process by which Java programs perform automatic
memory management.
 Java programs compile to bytecode that can be run on a Java Virtual Machine, or JVM for
short. When Java programs run on the JVM, objects are created on the heap, which is a
portion of memory dedicated to the program.
 Eventually, some objects will no longer be needed. The garbage collector finds these unused
objects and deletes them to free up memory.
 In C/C++, a programmer is responsible for both the creation and destruction of objects.
Usually, programmer neglects the destruction of useless objects. Due to this negligence, at a
certain point, sufficient memory may not be available to create new objects, and the entire
program will terminate abnormally, causing OutOfMemoryErrors.
 But in Java, the programmer need not care for all those objects which are no longer in use.
Garbage collector destroys these objects.
 The main objective of Garbage Collector is to free heap memory by destroying unreachable
objects. The garbage collector is the best example of the Daemon thread as it is always
running in the background.

Types of Activities in Java Garbage Collection

Two types of garbage collection activity usually happen in Java. These are:

 Minor or incremental Garbage Collection: It is said to have occurred when


unreachable objects in the young generation heap memory are removed.
 Major or Full Garbage Collection: It is said to have occurred when the objects that
survived the minor garbage collection are copied into the old generation or
permanent generation heap memory are removed.
Clone() Method in Java
Object cloning refers to the creation of an exact copy of an object. It creates a new
instance of the class of the current object and initializes all its fields with exactly the
contents of the corresponding fields of this object.
Methods for Performing Object Cloning in Java
There are 3 methods for creating Object Cloning in Java that are mentioned below:
1. Using Assignment Operator to create a copy of the reference variable
2. Creating a copy using the clone() method
3. Usage of clone() method – Deep Copy
1. Using Assignment Operator to create a copy of the reference variable
In Java, there is no operator to create a copy of an object. Unlike C++, in Java, if we
use the assignment operator then it will create a copy of the reference variable and
not the object. This can be explained by taking an example. The following program
demonstrates the same.
// Java program to demonstrate that assignment operator
// only creates a new reference to same object

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();

System.out.println(ob1.x + " " + ob1.y);

// Creating a new reference variable ob2


// pointing to same address as ob1
Test ob2 = ob1;

// Any change made in ob2 will


// be reflected in ob1
ob2.x = 100;

System.out.println(ob1.x + " " + ob1.y);


System.out.println(ob2.x + " " + ob2.y);
}
}
2. Creating a copy using the clone() method
 The class whose object’s copy is to be made must have a public clone method in it or in one
of its parent classes.
 Every class that implements clone() should call super.clone() to obtain the cloned object
reference.
 The class must also implement java.lang.Cloneable interface whose object clone we want to
create otherwise it will throw CloneNotSupportedException when the clone method is called
on that class’s object.
3. Usage of clone() method – Deep Copy
 If we want to create a deep copy of object X and place it in a new object Y then a new copy
of any referenced objects fields are created and these references are placed in object Y.
 This means any changes made in referenced object fields in object X or Y will be reflected
only in that object and not in the other.
 A deep copy copies all fields and makes copies of dynamically allocated memory pointed to
by the fields. A deep copy occurs when an object is copied along with the objects to which it
refers.
steps to create rmi application that can communicate with different
computer system
Here are the steps to create an RMI (Remote Method Invocation) application that can communicate
with different computer systems:
Step 1: Define the Remote Interface

- Create a Java interface that defines the methods that can be called remotely.
- Extend the Remote interface and throw RemoteException for each method.

Step 2: Implement the Remote Interface

- Create a Java class that implements the remote interface.


- Provide the implementation for each method defined in the remote interface.

Step 3: Create the RMI Server

- Create a Java class that acts as the RMI server.


- Create an instance of the remote object and export it to the RMI registry.
- Bind the remote object to a name in the RMI registry.

Step 4: Create the RMI Client

- Create a Java class that acts as the RMI client.


- Get a reference to the remote object from the RMI registry.
- Call the methods on the remote object.

Step 5: Start the RMI Registry

- Start the RMI registry on the server machine.


- Use the rmiregistry command to start the registry.

Step 6: Run the RMI Server

- Run the RMI server on the server machine.


- Use the java command to run the server.

Step 7: Run the RMI Client

- Run the RMI client on the client machine.


- Use the java command to run the client.

Step 8: Test the RMI Application

- 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.

Here is a simple example of an RMI application:

Remote Interface

public interface MyRemote extends Remote {


String sayHello(String name) throws RemoteException;
}

Remote Implementation

public class MyRemoteImpl implements MyRemote {


public String sayHello(String name) {
return "Hello, " + name + "!";
}
}

RMI Server

public class MyRMIServer {


public static void main(String[] args) {
try {
MyRemoteImpl remoteObj = new MyRemoteImpl();
Registry registry = LocateRegistry.createRegistry(1099);
registry.bind("MyRemote", remoteObj);
System.out.println("RMI server started");
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}

RMI Client

public class MyRMIClient {


public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry("localhost", 1099);
MyRemote remoteObj = (MyRemote) registry.lookup("MyRemote");
System.out.println(remoteObj.sayHello("John"));
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}

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:

What is a JAR file?

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.

What is the structure of a JAR file?

A JAR file has the following structure:

- 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.

How is a JAR file executed?

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:

- Distributing Java libraries and frameworks.


- Packaging Java applications for deployment.
- Creating executable JAR files for standalone applications.
- Deploying Java web applications to s

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy