Programming Technology (PT) Java All Questions Solution
Table of Contents
1. What is event handling in JAVA? Explain mouse event handling with suitable example code.
import javax.swing.*; import java.awt.event.*; public class MouseEventFile implements MouseListener,MouseMotionListener {
JTextField tf= new JTextField();
public void runFrame() {
JFrame frame= new JFrame("Mouse Events"); frame.setSize(600,300); tf.setBounds(200,60,250,20); frame.add(tf); frame.setLayout(null); frame.setVisible(true); frame.addMouseListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null);
}
public void mouseClicked(MouseEvent mouseEvent) {
tf.setText("you clicked"); }
public void mousePressed(MouseEvent mouseEvent) {
}
public void mouseReleased(MouseEvent mouseEvent) {
}
public void mouseEntered(MouseEvent mouseEvent) { tf.setText("In");
}
public void mouseExited(MouseEvent mouseEvent) { tf.setText("Out");
}
public void mouseDragged(MouseEvent mouseEvent) {
}
public void mouseMoved(MouseEvent mouseEvent) {
}
public static void main(String[] args) { MouseEventFile msf=new MouseEventFile(); msf.runFrame();
} }
2. What is event delegation model? Explain button event handling with code example.
import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
/** * Created by ashoknath on 11/3/19. */
public class ButtonMet implements ActionListener { JFrame frame= new JFrame("Click Events"); JTextField tf= new JTextField(); JButton button1= new JButton("Print");
JButton button2= new JButton("clear");
public void runFrame() {
frame.setSize(600,300); tf.setBounds(200,60,250,20); button1.setBounds(200, 150, 100, 40); button2.setBounds(400, 150, 100, 40); frame.add(tf);
frame.add(button1); frame.add(button2); frame.setLayout(null); frame.setVisible(true); button1.addActionListener(this); button2.addActionListener(this); button1.setActionCommand("button1");
button1.setActionCommand("button2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null);
} public void actionPerformed(ActionEvent e) { String comm1= e.getActionCommand(); if(comm1=="button1") {
tf.setText("NCIT");
}else{ tf.setText(" ");
}
if(comm1=="button2") {
tf.setText("NCIT "); }else{
tf.setText(" "); }
}
public static void main(String[] args) { ButtonMet be= new ButtonMet(); be.runFrame();
}
}
3. Write an event driven program in JAVA that closes the application on clicking the close button.
import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
/** * Created by ashoknath on 8/26/19. */
public class CloseButton implements ActionListener { JFrame frame= new JFrame("Background Changer");
JButton button1= new JButton("close");
public void runFrame() { frame.setSize(600, 300); button1.setBounds(200, 150, 100, 40); frame.add(button1); frame.setLayout(null); frame.setVisible(true);
button1.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e) { if(e.getSource()==button1) {
System.exit(0); }
}
public static void main(String[] args) { CloseButton cb=new CloseButton();
cb.runFrame(); }
}
4. Write an event driven program in JAVA that changes color to RED when clicking the
button change color on the application.
package background; import javax.swing.*; import java.awt.*; import java.awt.event.*;
class BackgroundChanger implements ActionListener { JFrame frame= new JFrame("Background Changer");
JButton button1= new JButton("Change Color"); JButton button2= new JButton("close");
public void runFrame() { frame.setSize(600, 300); button1.setBounds(200, 150, 100, 40); frame.add(button1); frame.add(button2); frame.setLayout(null);
frame.setVisible(true);
button1.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e) { if(e.getSource()==button1) {
frame.getContentPane().setBackground(Color.red); } else { frame.getContentPane().setBackground(Color.white);
}
}
public static void main(String[] args) { BackgroundChanger bg= new BackgroundChanger();
bg.runFrame(); }
}
5. Write a swing program that will take input from two text boxes and one button when
clicked will show the words in uppercase in console
package com.ashok.swing.bank; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
/** * Created by ashoknath on 9/26/19. */
public class BoxTestConvert extends JFrame implements ActionListener {
JFrame frame= new JFrame("Click Events"); JTextField textField= new JTextField(); JTextField textField2= new JTextField(); JButton button1= new JButton("Convert"); public BoxTestConvert()
{ frame.setSize(600,300); textField.setBounds(200,60,250,20); textField2.setBounds(200,100,250,20); button1.setBounds(200, 150, 100, 40); frame.add(textField); frame.add(textField2); frame.add(button1); frame.setLayout(null); frame.setVisible(true); button1.addActionListener(this); button1.setActionCommand("button1"); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null);
} public void actionPerformed(ActionEvent e) {
String text1 = textField.getText(); String text2 = textField2.getText(); String comm1= e.getActionCommand(); if(comm1=="button1")
{ textField.setText(text1.toUpperCase()); //If u write this code u will get the uppercase word in textbox textField2.setText(text2.toUpperCase()); //If u write this code u will get the uppercase word in textbox System.out.println(text1.toUpperCase()); // this will give u uppercase in console System.out.println(text2.toUpperCase()); // this will give u uppercase in console }else{
textField.setText(" "); }
}
public static void main(String[] args) { new BoxTestConvert();
} }
6. Write a program that will read character one by one from file. -File Handling
package handlefile; import java.io.*;
import java.util.Scanner;
/** * Created by ashoknath on 8/17/19. */
public class SimpleFile { public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); try {
FileInputStream fin = new FileInputStream("File1.txt");
FileOutputStream fout= new FileOutputStream("File2.txt");
int ch; while ((ch=fin.read())!= -1) {
fout.write(ch);
} System.out.println("File copying successful"); fin.close(); fout.close(); }catch(Exception ex){ System.err.print("The file cannot be created."); } } }
7. Java Servlet /Jsp A jsp file to input the details of student in jsp and process it
using servletProcessName.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%> <!DOCTYPE html > <html> <head> <body> <h1>Registration Form</h1> <form action="RegistrationController" method="post"> <table style="with: 50%"> <tr>
<td>Full Name</td>
<td><input type=”text” name=”fullname” /></
</tr> <tr>
<td>Address</td>
<td><input type=”text” name=”address” /></
</tr> <tr>
<td>Age</td>
<td><input type=”text” name=”age” /></td> </tr>
<tr>
<td>Qualification</td>
<td><input type=”text” name=”qual” /></td>
</tr> <tr>
<td>Percentage</td>
<td><input type=”text” name=”percent” /></
</tr> <tr>
<td>Year Passed</td>
<td><input type=”text” name=”yop” /></td> </tr>
</table>
<input type=”submit” value=”register” /> </form>
</body> </html>
//displaying
display.jsp
<%@ page language=”java” contentType=”text/html; charset=ISO-8859-1″
pageEncoding=”ISO-8859-1″%>
<!DOCTYPE html >
<html>
<head>
<title>Display</title>
</head>
<body>
<% String name = request.getParameter(“fullname”);
<td>
<td>
<td>
String Addr = request.getParameter("address"); String age = request.getParameter("age"); String Qual = request.getParameter("qual"); String Persent = request.getParameter("percent"); String Year = request.getParameter("yop"); %>
<table id =”nat”> <tr>
<td>Full Name</td>
<td><%= name %></td> </tr>
<tr> <td>Address</td> <td><%= Addr %></td>
</tr> <tr>
<td>Age</td>
<td><%= age %></td> </tr>
<tr> <td>Qualification</td> <td><%= Qual %></td>
</tr> <tr>
<td>Percentage</td>
<td><%= Persent %></td> </tr>
<tr> <td>Year of Passout</td> <td><%= Year %></td>
</tr> </table> </body> </html>
//registration controller servlet registrationcontroller.java package com.candidjava;
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class RegistrationController extends HttpServlet { private static final long serialVersionUID = 1L; //ide
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{ PrintWriter out = res.getWriter(); String name = req.getParameter("fullname"); String Addr = req.getParameter("address"); String age = req.getParameter("age"); String Qual = req.getParameter("qual"); String Persent = req.getParameter("percent"); String Year = req.getParameter("yop"); if(name.isEmpty()||Addr.isEmpty()||age.isEmpty()||
Qual.isEmpty()||Persent.isEmpty()||Year.isEmpty()) {
RequestDispatcher rd = req.getRequestDispatcher("processName.jsp"); out.println("<font color=red>Please fill all the fields</font>"); rd.include(req, res); }
else {
RequestDispatcher rd = req.getRequestDispatcher("Display.jsp");
rd.forward(req, res); }
}
}