Java Microproject
Java Microproject
PROJECT REPORT ON
“Chat Application Over The Network”
SUBMITTED BY
Ms.ShindeIshwari Sudhakar
Ms.PawarSanskruti Laxman
MR.Bharati.M.B
We would like to express our deep sense of gratitude and special thanks
to our guide MR.Bharati.M.B who not only took a great interest in the project
but also was always ready to help us as and when needed.
Finally we are thankful to all our friends who have helped for presenting
& preparation of our project
INDEX
1.0 Rationale 1
4.0 Methodology 9
5.0 14
Implementation
6.0 Conclusion 15
7.0 Reference 16
Micro–Project Report
Chat Application Over The Network
1.0 Rationale
Layout Managers:
Swing uses layout managers to control how components are arranged within a container.
AWT:
AWT (Abstract Window Toolkit) components are part of Java's original platform-independent
windowing, graphics, and user-interface toolkit. These components provide a way to create
Graphical User Interfaces (GUI) for Java applications. AWT is part of the java.awt package
and includes various classes and interfaces for building basic GUI elements.
Swing:
Swing is a part of Java's Java Foundation Classes (JFC) used for building Graphical User Interfaces
(GUIs). It provides a more flexible and powerful set of GUI components than the original AWT
(Abstract Window Toolkit). Unlike AWT, Swing components are written entirely in Java and are
platform-independent, making them lightweight and consistent across different operating systems.
Swing Components:
• JFrame: The main window container where all other components (buttons, text fields, etc.)
are added.
• JButton: A button that can trigger an action when clicked
• JTextField: A single-line text field for user input.
• JPanel: It is a lightweight container in Swing used to organize and group components within a
GUI.
• JScrollBar: It is Swing is a component that enables vertical or horizontal scrolling through
content or a range of values.
Page | 5
Event handling:
Event handling in Java is the mechanism that manages the interaction between the user and GUI
components, allowing components to respond to user actions like clicks, key presses, and mouse
movements. This is done through event listeners that are attached to components and trigger actions
when specific events occur.
Action Event: Action Event in Java is a type of event that occurs when a user performs an action on a
component, such as clicking a button, selecting a menu item, or pressing the "Enter" key in a text
field. This event is handled by implementing the ActionListener interface
• Action Listener: The interface that responds to the action, typically by overriding the
actionPerformed() method.
Networking:
Networking related to servers involves various concepts and components that facilitate communication
and data exchange between servers and clients over a network. Here are some key aspects:
Classes:
• Calendar class:
The Calendar class, part of the java.util package, is used for manipulating dates and times in a
locale-sensitive manner, which is essential for networking applications like logging events and
scheduling tasks.
▪ getTime() : Returns a Date object representing the time value of this Calendar object.
▪ setTime(Date date) : Sets the calendar's time with the given Date object.
▪ set(int year, int month, int date) : Sets the calendar fields YEAR, MONTH, and
DAY_OF_MONTH to the specified values.
▪ clear() : Clears all the calendar fields.
• SimpleDateFormat:
The SimpleDateFormat class in Java, part of the java.text package, is used to format and parse
dates in a locale-sensitive manner. It allows for the conversion between Date objects and their
string representations.
▪ SimpleDateFormat formatter = new SimpleDateFormat(String pattern);
• DataOutputStream:
The DataOutputStream class in Java is used to write primitive data types to an output stream
in a portable (machine-independent) way. It provides methods for writing data such as int,
long, float, double, boolean, and UTF-8 encoded strings. This class ensures that the data can
be read consistently, regardless of the underlying machine architecture, by using a standard
format.
▪ DataOutputStream(OutputStream out)
Page | 6
▪ writeUTF(String s) : Writes a string encoded in UTF-8 format.
Page | 7
For the network-based chat application project, the data collected can include:
4.2 Implementation/Coding
package project;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
import java.net.*;
import java.io.*;
{ JTextField text;
JPanel a1;
static Box vertical = Box.createVerticalBox();
static JFrame f = new JFrame();
static DataOutputStream dout;
Server() {
f.setLayout(null);
Page | 8
p1.add(status);
a1 = new JPanel();
a1.setBounds(5, 75, 440, 570);
f.add(a1);
f.setSize(450, 700);
f.setLocation(200, 50);
f.setUndecorated(true);
f.getContentPane().setBackground(Color.WHITE);
f.setVisible(true);
}
JPanel p2 = formatLabel(out);
a1.setLayout(new BorderLayout());
a1.add(vertical, BorderLayout.PAGE_START);
dout.writeUTF(out);
text.setText("");
f.repaint();
f.invalidate();
f.validate();
} catch (Exception e) {
e.printStackTrace();
}
}
panel.add(output);
panel.add(time);
return panel;
}
try {
ServerSocket skt = new ServerSocket(2005);
while(true)
{
Socket s = skt.accept();
DataInputStream din = new DataInputStream(s.getInputStream());
dout = new DataOutputStream(s.getOutputStream());
while(true)
{
String msg = din.readUTF();
JPanel panel = formatLabel(msg);
} catch (Exception e)
{
e.printStackTrace();
}
}
}
Page | 10
• Client side program:
package project;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
import java.net.*;
import java.io.*;
JTextField text;
static JPanel a1;
static Box vertical = Box.createVerticalBox();
client() {
f.setLayout(null);
a1 = new JPanel();
a1.setBounds(5, 75, 440, 570);
f.add(a1);
f.setSize(450, 700);
f.setLocation(800, 50);
f.setUndecorated(true);
f.getContentPane().setBackground(Color.WHITE);
f.setVisible(true);
}
JPanel p2 = formatLabel(out);
a1.setLayout(new BorderLayout());
a1.add(vertical, BorderLayout.PAGE_START);
dout.writeUTF(out);
text.setText("");
f.repaint();
f.invalidate();
f.validate();
} catch (Exception e) {
e.printStackTrace();
}
}
public static JPanel formatLabel(String out)
{ JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
Page | 12
return panel;
}
try {
Socket s = new Socket("127.0.0.1",1000);
DataInputStream din = new DataInputStream(s.getInputStream());
dout = new DataOutputStream(s.getOutputStream());
while(true) {
a1.setLayout(new BorderLayout());
String msg = din.readUTF();
JPanel panel = formatLabel(msg);
vertical.add(Box.createVerticalStrut(15));
a1.add(vertical, BorderLayout.PAGE_START);
f.validate();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
4.3 Result & Analysis
We have successfully learned about a Develop a project using server and ServerSockets. The code
provides a basic demonstration of AWT and Swing, Event handling for the Chat Application over
the network.
Page | 13
6.0 Output of Micro-Projects
*********************
Page | 14
Conclusion
Page | 15
Page | 16
Reference
https://www.google.com/search?q=different+types+of+test+cases+with+examples&rlz=1C1VDKB_e
nIN1016IN1016&oq=&aqs=chrome.4.35i39i362l8.697296039j0j15&sourceid=chrome&ie=UTF-8
https://www.valamis.com/hub/learning-outcomes
Page | 17