Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

STATE BOARD OF TECHNICAL EDUCATION

GOVERNMENT POLYTECHNIC, JALGAON


(0018)

Program Name and Code : Computer Engineering (CO5I)

Course Name : Advance Java Programming


Course Code : [22517]
Academic Year : 2023-2024
Semester : Five
A MICRO PROJECT ON

Event Handling in java

Submitted by the group of 3 students

Sr. No. Roll Name of student Enrollment No. Seat No.


No.
1 5 Saurabh Bapusaheb Bagul 2100180215
2 6 Jitesh Kishor Barhate 2100180216
3 42 Lokesh Jitendra Dhangar 2100180265

Project Guide : Prof. Amol Chaudhari Sir


MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

Certificate

This is to certify that Saurab Bagul, Jitesh Barhate, Lokesh Dhangar Roll No.5,6,42
of 5th Semester of Diploma in Computer Engineering of Government Polytechnic,
Jalgaon (Code:0018) has completed the Micro Project satisfactorily in the subject
Advance Java Programming (22517) for the Academic Year 2023-2024 as prescribed
in the curriculum.

Place : Jalgaon enrolment no:2100180215,16,65.

Subject Teacher Head Of Department Principal

Seal of institution
GOVTERNMENT POLYTECHNIC JALGAON

-SUBMISSION

We are Saurabh Bagul , Jitesh Barhate, Lokesh Dhangar student’s of 5th Semester of
the Programme Computer Engineering humbly submit that we have completed
from time to time the Micro-Project work as described in this report by y our own
skills and study in academic year 2023-2024 as per instructions and guidance Prof.
Amol Chaudhari Sir that following students were associated with me for this work,
however, quantum of my contribution has been approved by the Lecturer. And that
I have not copied the report on its any appreciable part from any other literature in
contravention of the academic ethics.

Date: Signature of Student


WEEKLY PROGRESS REPORT

SR.NO. WEEK ACTIVITY PERFORMED SIGN OF GUIDE DATE

1 Discussion and finalization of topic

2 First Preparation and submission of


Abstract
Week
3 Literature Review

4 Collection of Data

5 Collection of Data

Second
6 Week Discussion and outline of Content

7 Formulation of Content

8 Editing and proof Reading of


Content

9 Compilation of Report And


Presentation
Third
10 Seminar
Week

11 Viva voce

12 Final submission of Micro Project

Sign of student sign of faculty


Evaluation Sheet for the Micro Project
Name of Student : S.B.B., J.K.B., L.J.D..
Enrollment No. :2100180215,2100180216,2100180265.
Semester : Five

Course Title : Advance Java programming


Code : 22517
Title of the Micro-Project : Event Handling In Java

Micro-Project Evaluation Sheet

Marks out of 6 for


performance in Marks out of 4 for
Roll No. Name of student group activity performance In
oral/presentation Total out of 10
(D5 Coi.8)
(D5 Coi.9)

05. Saurabh Bagul

06. Jitesh Barhate

42. Lokesh Dhangar


Acknowledgement

This Micro project would not have been possible without


considerable guidance and support. So, we would like to
acknowledge those who have enable us to complete this project.
Firstly we would like to thank our project guide, Prof.Amol
Chaudhari Sir for providing the guideline with continuous advice
and feedback throughout the duration of finishing this project.
We also thank to Dr P. M. Patil (principal of Government
Polytechnic Jalgaon) for providing us the opportunity to embark
on this project.
Secondly we would also like to thank all other staff members of
Computer department that we may called upon for assistance
since the genesis of this project their opinion and suggestion
have helped us in a realizing these project.
Also not to be forgotten, we would like to thank all our friends
with the report valuable opinion and sharing ideas during the
process of this project.
Finally we would like to thank our families for their
understanding, encouragement and support towards the
completion of project.

Thank You so much.


INDEX

1. Introduction

2. What is Event Handling?

3. Code.

4. Code summary

5. Output.

6. Conclusion.
Introduction:
An event handling project in advanced Java is a dynamic and interactive application
that focuses on capturing and responding to various user interactions and system
events. These events can range from mouse clicks and keyboard input to window
resizing and network communication. Such projects are essential for creating
responsive and user-friendly applications, making them a fundamental aspect of
modern software development.

In this project, we will explore the intricacies of event-driven programming in the


context of advanced Java technologies, such as Java Swing, JavaFX, or other relevant
frameworks. Event handling allows us to design applications that can react to user
input, internal states, and external stimuli in real-time, enhancing the user experience
and the overall functionality of the software.

The primary goal of this project is to create a comprehensive event handling system
that efficiently manages and processes events, providing seamless user interaction.
This involves designing a graphical user interface (GUI), defining event listeners,
and implementing event-driven programming logic to respond to these events
effectively.

Throughout this project, we will delve into various aspects of event handling,
including event registration, propagation, handling multiple types of events, and
dealing with user interface components like buttons, sliders, and text fields.
Additionally, we will explore advanced topics like multi-threading for concurrent
event handling, and custom event creation for specific application needs.

Ultimately, by the end of this event handling project, you will gain valuable
experience and insights into the world of advanced Java development, equipping you
with the skills to create robust, interactive, and user-friendly applications that cater to
a wide range of user inputs and system events.
What is Event Handling ?
Event handling is a fundamental concept in computer programming and software
development. It refers to the process of capturing, processing, and responding to
events, which are occurrences or incidents that happen during the execution of a
computer program.
Here are some key aspects of event handling:
1. Event Sources: Events can originate from different sources, including user
interfaces (GUI components like buttons, checkboxes, and text fields), system
notifications, hardware devices, and other software components.
2. Event Types: Events come in various types, such as mouse events (click, move,
drag), keyboard events (key presses, key releases), action events (button clicks),
window events (resizing, closing), and custom events (created by developers to suit
specific needs).
3. Event Listeners: In event-driven programming, developers define event listeners
or handlers to "listen" for specific events. These listeners are responsible for
executing code when the associated event occurs.
4. Event Propagation: Events can propagate through an event hierarchy or a
component hierarchy. For example, if a button is clicked in a GUI, the event may
first be handled by the button itself and then propagate up to its parent containers.
5. Event Processing: Event handlers contain code that responds to events. This code
can perform a wide range of actions, from updating the user interface to executing
specific business logic.
6. Callbacks: Event handling often involves callback functions or methods, where
specific code is executed when an event occurs. This allows for the decoupling of
event sources and event listeners, promoting modularity and maintainability.
7. Event Driven Architecture: Event handling is a core principle in event-driven
architecture, where programs respond to events asynchronously, allowing them to
be more responsive and interactive.
Event handling is commonly used in a variety of applications, including graphical
user interfaces (GUIs), web development, video games, and real-time systems.
Different programming languages and frameworks offer their own mechanisms for
event handling, but the fundamental concept remains the same: capturing and
responding to events to create dynamic and interactive software.
Code
/*Online Java Paper Test*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class OnlineTest extends JFrame implements ActionListener
{
JLabel l;
JRadioButton jb[]=new JRadioButton[5];
JButton b1,b2;
ButtonGroup bg;
int count=0,current=0,x=1,y=1,now=0;
int m[]=new int[10];
OnlineTest(String s)
{
super(s);
l=new JLabel();
add(l);
bg=new ButtonGroup();
for(int i=0;i<5;i++)
{
jb[i]=new JRadioButton();
add(jb[i]);
bg.add(jb[i]);
}
b1=new JButton("Next");
b2=new JButton("Bookmark");
b1.addActionListener(this);
b2.addActionListener(this);
add(b1);add(b2);
set();
l.setBounds(30,40,450,20);
jb[0].setBounds(50,80,100,20);
jb[1].setBounds(50,110,100,20);
jb[2].setBounds(50,140,100,20);
jb[3].setBounds(50,170,100,20);
b1.setBounds(100,240,100,30);
b2.setBounds(270,240,100,30);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setLocation(250,100);
setVisible(true);
setSize(600,350);}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
if(check())
count=count+1;
current++;
set();
if(current==9)
{
b1.setEnabled(false);
b2.setText("Result");
}
}
if(e.getActionCommand().equals("Bookmark"))
{
JButton bk=new JButton("Bookmark"+x);
bk.setBounds(480,20+30*x,100,30);
add(bk);
bk.addActionListener(this);
m[x]=current;
x++;
current++;
set();
if(current==9)
b2.setText("Result");
setVisible(false);
setVisible(true);
}
for(int i=0,y=1;i<x;i++,y++)
{
if(e.getActionCommand().equals("Bookmark"+y))
{
if(check())
count=count+1;
now=current;
current=m[y];
set();
((JButton)e.getSource()).setEnabled(false);
current=now;
}
}
if(e.getActionCommand().equals("Result"))
{
if(check())
count=count+1;
current++;
//System.out.println("correct ans="+count);
JOptionPane.showMessageDialog(this,"correct
ans="+count);
System.exit(0);
}
}
void set()
{
jb[4].setSelected(true);
if(current==0)
{
l.setText("Que1: Which one among these is not a
datatype");

jb[0].setText("int");jb[1].setText("Float");jb[2].setText("boolean");jb
[3].setText("char");
}
if(current==1)
{
l.setText("Que2: Which class is available to all the class
automatically");

jb[0].setText("Swing");jb[1].setText("Applet");jb[2].setText("Object"
);jb[3].setText("ActionEvent");
}
if(current==2)
{
l.setText("Que3: Which package is directly available to
our class without importing it");

jb[0].setText("swing");jb[1].setText("applet");jb[2].setText("net");jb[
3].setText("lang");
}
if(current==3)
{
l.setText("Que4: String class is defined in which
package");

jb[0].setText("lang");jb[1].setText("Swing");jb[2].setText("Applet");j
b[3].setText("awt");
}
if(current==4)
{
l.setText("Que5: Which institute is best for java
coaching");
jb[0].setText("Utek");jb[1].setText("Aptech");jb[2].setText("SSS
IT");jb[3].setText("jtek");
}
if(current==5)
{
l.setText("Que6: Which one among these is not a
keyword");

jb[0].setText("class");jb[1].setText("int");jb[2].setText("get");jb[3].se
tText("if");
}
if(current==6)
{
l.setText("Que7: Which one among these is not a class
");

jb[0].setText("Swing");jb[1].setText("Actionperformed");jb[2].setText
("ActionEvent");jb[3].setText("Button");
}
if(current==7)
{
l.setText("Que8: which one among these is not a function
of Object class");

jb[0].setText("toString");jb[1].setText("finalize");jb[2].setText("equal
s");jb[3].setText("getDocumentBase");
}
if(current==8)
{
l.setText("Que9: which function is not present in Applet
class");
jb[0].setText("init");jb[1].setText("main");jb[2].setText("start");jb[3]
.setText("destroy");
}
if(current==9)
{
l.setText("Que10: Which one among these is not a valid
component");

jb[0].setText("JButton");jb[1].setText("JList");jb[2].setText("JButton
Group");jb[3].setText("JTextArea");
}
l.setBounds(30,40,450,20);
for(int i=0,j=0;i<=90;i+=30,j++)
jb[j].setBounds(50,80+i,200,20); }
boolean check() {
if(current==0)
return(jb[1].isSelected());
if(current==1)
return(jb[2].isSelected());
if(current==2)
return(jb[3].isSelected());
if(current==3)
return(jb[0].isSelected());
if(current==4)
return(jb[2].isSelected());
if(current==5)
return(jb[2].isSelected());
if(current==6)
return(jb[1].isSelected());
if(current==7)
return(jb[3].isSelected());
if(current==8)
return(jb[1].isSelected());
if(current==9)
return(jb[2].isSelected());
return false; }
public static void main(String s[]) {
new OnlineTest("Online Test Of Java"); }
}
Code Summary
In the given Java code for an online test application, event handling is used to
respond to user interactions, such as clicking on buttons and radio buttons. The
ActionListener interface is implemented to handle these events. Here's how
event handling is used in this code:

The OnlineTest class extends JFrame and implements the ActionListener


interface.

Various GUI components like labels, radio buttons, and buttons are created and
added to the frame.

ActionListener interface is implemented in the OnlineTest class, and the


actionPerformed method is overridden to handle events generated by
components that have registered this class as an action listener.

In the actionPerformed method, the source of the event is identified using


e.getSource() or e.getActionCommand(), depending on the type of event.

Depending on the event source, different actions are taken:

If the "Next" button is clicked (e.getSource() == b1), the user's answer is


checked, the count is updated, and the next question is displayed.
If a "Bookmark" button is clicked
(e.getActionCommand().equals("Bookmark")), a bookmark button is added, the
current question is marked for review, and the next question is displayed.
If a bookmark button is clicked
(e.getActionCommand().equals("Bookmark"+y)), the user's answer is checked,
the count is updated, and the bookmarked question is displayed.
If the "Result" button is clicked (e.getActionCommand().equals("Result")), the
user's final score is displayed in a dialog, and the application is exited.
The set method is used to set the question and answer options when a new
question is displayed.
The check method is used to check whether the user's answer is correct for the
current question.

In summary, event handling in this code allows the application to respond to


user actions, update the display with questions and answers, and keep track of
the user's score and bookmarked questions. The actionPerformed method is
central to this event handling, as it contains the logic for responding to different
events and user interactions.
mouse events are used to handle interactions with the frame and the "Next" and
"Bookmark" buttons. Here's how mouse events are used in the code:

The OnlineTest class implements the MouseListener interface, which allows it


to handle various mouse events.

The mouseClicked, mousePressed, mouseReleased, mouseEntered, and


mouseExited methods are overridden to handle different types of mouse events.

The frame and the "Next" and "Bookmark" buttons are registered for mouse
events using addMouseListener and addMouseListener methods, respectively.

Inside the mouseClicked method, you can determine the source of the mouse
event using e.getSource(), and then you can take actions accordingly. Here's
how mouse events are used for specific components:

If the "Next" button is clicked (e.getSource() == b1), you can handle the mouse
click on the "Next" button.
If the "Bookmark" button is clicked (e.getSource() == b2), you can handle the
mouse click on the "Bookmark" button.
For other mouse clicks, you can handle them, for example, by performing
actions when the user clicks on the frame itself.
This allows you to respond to mouse interactions with the frame and buttons in
the application. You can customize the behavior in the mouseClicked method to
perform actions based on the specific mouse events and components involved.
Output
Conclusion :
In Java, event handling is a fundamental aspect of programming, especially in GUI
(Graphical User Interface) applications. Event handling allows developers to respond
to user interactions and system events, making applications interactive and
responsive. Here are some key conclusions about event handling in Java:

Event-Driven Programming: Java applications are typically event-driven, meaning


they respond to events generated by user actions (e.g., mouse clicks, key presses) or
the system (e.g., window resizing, network events). Events are propagated and
handled by event listeners or handlers.

Event Sources and Listeners: In Java, event sources are objects that generate events
(e.g., buttons, text fields), and event listeners are objects that respond to those events.
Event listeners are registered with event sources to handle specific events.

Event Listener Interfaces: Java provides a set of predefined event listener interfaces,
such as ActionListener, MouseListener, and KeyListener, to handle common types of
events. Developers can implement these interfaces and override their methods to
define event handling behavior.

Anonymous Inner Classes: Often, event listeners are implemented as anonymous


inner classes, making the code more concise and localized. This approach is widely
used in Java for event handling.

Event Dispatch Thread (EDT): GUI events in Java must be processed on the Event
Dispatch Thread to ensure thread safety. This is essential to prevent concurrent
access issues in GUI components.

Custom Events: Developers can create custom events and listeners for specific
application needs. This allows for the creation of more complex and customized
event handling systems.
Swing and AWT: Java provides two primary libraries for GUI development—Swing
and AWT. Swing is an extension of AWT and offers a more powerful and flexible
set of components for building GUI applications.

Error Handling: Proper error handling is crucial in event-driven programming.


Handling exceptions and errors gracefully is essential to ensure that the application
remains stable and user-friendly.

Asynchronous Programming: Event handling often involves asynchronous


programming, where the application doesn't wait for events to occur but responds to
them when they do. This asynchronous nature can lead to complex control flow and
requires careful design.

Testing and Debugging: Event-driven code can be challenging to test and debug, as
events may occur at unexpected times. Tools and techniques for event-driven
debugging are valuable in ensuring the reliability of the application.

In summary, event handling is a core concept in Java programming, especially in


GUI development. It enables applications to respond to user input and system events,
creating interactive and dynamic user interfaces. While event handling can introduce
complexity and challenges, it is essential for building modern, user- friendly Java
applications.
Refrence:
https://www.javatpoint.com/event-handling-in-java
https://www.geeksforgeeks.org/event-handling-in-java/

You might also like