Afzal khan
37 20 21
A Laboratory Manual
for
Advanced Java
Programming
(22517)
Semester-V
(CO/CW/CM/IF)
Maharashtra State
Board of Technical Education, Mumbai
(Autonomous) (IS0:9001:2015) (ISO/IEC 27001:2013)
Advanced Java Programming (22517)
Practical No. 13: Write a program to demonstrate the use of
WindowAdapter class
I. Practical Significance:
In listener interfaces implementation it is mandatory to implement all the methods defined
by the particular interface. To overcome this problem and to implement only required
methods java programming supports the concept of Adapter class. It lets the user to
implement only required methods.
This practical focuses on different types of Adapter classes and their practical
implementation.
II. Relevant Program Outcomes (POs)
Basic knowledge: Apply knowledge of basic mathematics, sciences and basic
engineering to solve the computer group related problems.
Discipline knowledge: Apply Computer Programming knowledge to solve the
computer group related problems.
Experiments and practice: Plan to perform experiments and practices to use the
results to solve the computer group related problems.
Engineering tools: Apply relevant Computer programming / technologies and
tools with an understanding of the limitations.
Individual and Team work: Function effectively as a leader and team member in
diverse/multidisciplinary teams.
Communication: Communicate effectively in oral and written form.
III. Competency and Practical skills
The practical is expected to develop the following skills:
1. Understand the concept and different types of adapter classes in java.
2. Develop an application using Adapter Class.
IV. Relevant Course Outcome(s)
Handle events of AWT and Swing Components
V. Practical Outcome (PrOs)
Write a program to demonstrate the use of WindowAdapter class
VI. Relevant Affective domain related Outcome(s)
1. Follow precautionary measures.
2. Follow naming conventions.
3. Follow ethical practices.
VII. Minimum Theoretical Background
An Adapter class is class which will allow the user to simplify the method
implementation. For example consider we are implementing the WindowListener
interface in this case we need to define all the methods of the WindowListener interface
such as listed below
1. void windowActivated(WindowEvent we)
2. void windowClosed(WindowEvent we)
3. void windowClosing(WindowEvent we)
4. void windowDeactivated(WindowEvent we)
Maharashtra state Board of Technical Education 69
Advanced Java Programming (22517)
5. void windowDeiconified(WindowEvent we)
6. void windowIconified(WindowEvent we)
7. void windowOpened(WindowEvent we)
Programmer need to give either empty implementation of the methods defined by the
WindowListener. To overcome this problem and to allow the user to implement only
required methods java provides a facility that is Adapter classes. Some of the Adapter
classes provided by the java language are listed below.
1. ComponentAdapter
2. ContainerAdapter
3. FocusAdapter
4. KeyAdapter
5. MouseAdapter
6. MouseMotionAdapter
7. WindowAdapter
These adapter classes allows the user to implement only required methods.
VIII. Resources required (Additional) –
Nil
IX. Resources used (Additional)
Sr. Name of
Broad Specification Quantity Remarks (If any)
No. Resource
1
X. Program Code: Teacher must assign a separate program statement to group of 3-
4 students.
Debug the following code and write the output of following code.
import java.awt.*;
import java.awt.event.*;
public class WindowDemo
{
Frame f;
WindowDemo()
{
f=new Frame("Window Adapter");
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
f.dispose();
}
Maharashtra state Board of Technical Education 79
Advanced Java Programming (22517)
};
f.setSize(400,400);
f.setLayout(null);
f.setVisible(false);
}
public static void main(String[] args)
{
new WindowDemo();
}
}
XI. Result (Output of Code):
Maharashtra state Board of Technical Education 72
Advanced Java Programming (22517)
XII. Practical Related Questions
Note: Below given are few sample questions for reference. Teacher must design
more such questions so as to ensure the achievement of identified CO.
1. Write the use of Adapter class
2. Write the differences between Adapter class and Listener Interface
3. Write the use of anonymous inner class.
(Space for answer)
Maharashtra state Board of Technical Education 73
Advanced Java Programming (22517)
XIII. Exercise
1. Write a program to demonstrate the use of WindowAdapter class
2. Write a program to demonstrate the use of anonymous inner class
3. Write a program using MouseMotionAdapter class to implement only one method
mouseDragged().
(Space for Answer)
1. Code:-
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.FlowLayout;
public class Exp13_1 extends WindowAdapter
{ JFrame f ;
JLabel l ;
Exp13_1()
{
f = new JFrame("Afzal khan 37");
f.setVisible(true);
f.setSize(400,400);
f.setLayout(new FlowLayout());
f.addWindowListener(this);
f.addWindowFocusListener(this);
}
public void windowLostFocus(WindowEvent we)
{ l = new JLabel("Window Lost Focus");
f.remove(l);
f.add(l);
}
public void windowOpened(java.awt.event.WindowEvent we)
{ l = new JLabel("Window Opened");
f.remove(l);
f.add(l);
}
public void windowActivated(java.awt.event.WindowEvent we)
{ l = new JLabel("Window Activated");
f.remove(l);
f.add(l);
}
public void windowDeactivated(java.awt.event.WindowEvent we)
{
l = new JLabel("Window Deactivated");
f.remove(l);
Maharashtra state Board of Technical Education 74
Advanced Java Programming (22517)
f.add(l);
}
public void windowGainedFocus(java.awt.event.WindowEvent we)
{
l = new JLabel("Window Gained Focus");
f.remove(l);
f.add(l);
}
public static void main(String[] args)
{
Exp13_1 wa = new Exp13_1();
}
}
Output:-
Maharashtra state Board of Technical Education 75
Advanced Java Programming (22517)
2. Code:-
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Exp13_3 extends JFrame
{
public static void main(String[] args) {
Exp13_3 ai = new Exp13_3();
JButton jb = new JButton(" Don't Click Me ");
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
System.out.println("Action Event:"+ae);
}
});
ai.add(jb);
ai.pack();
ai.setTitle("Afzal khan 37");
ai.setSize(400,400);
ai.setVisible(true);
}
}
Output:-
Maharashtra state Board of Technical Education 76
Advanced Java Programming (22517)
3. Code:-
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.applet.Applet;
public class AdapterMouseDrag extends Applet
{
public void init()
{
addMouseMotionListener(new MouseDrag(this));
}
}
class MouseDrag extends MouseMotionAdapter
{
AdapterMouseDrag ad;
public MouseDrag(AdapterMouseDrag ad)
{
this.ad = ad;
}
public void mouseDragged(MouseEvent me)
{
ad.showStatus("Mouse Dragged");
}
/*
<applet code="AdapterMouseDrag" height="400" width="400">
Maharashtra state Board of Technical Education 77
Advanced Java Programming (22517)
</applet>
*/
Output:-
Maharashtra state Board of Technical Education 78
Advanced Java Programming (22517)
XIV. References/ Suggestions for Further Reading
1. https://www.javatpoint.com/java-adapter-classes
2. The complete reference Java 2 by Herbert schildt
XV. Assessment Scheme
Performance Indicators Weightage
Process related (35 Marks) 70%
1. Logic formation 30%
2. Debugging ability 30%
3. Follow ethical practices 10%
Product related (15 Marks) 30%
4. Expected output 10%
5. Timely Submission 10%
6. Answer to sample questions 10%
Total (50 Marks) 100%
List of Students /Team Members
1. …………………………………..
2. …………………………………..
3. …………………………………..
Dated signature
Marks Obtained
of Teacher
Process Product
Total(50)
Related(35) Related(15)
Maharashtra state Board of Technical Education 79