AJP PROGRAMFILE Ss
AJP PROGRAMFILE Ss
Roll No:64
Batch:B3
1
AJP PROGRAM’s
2
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3
62import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
3
}
});
// Add action listener for "Clear" button
buttonClear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText("Press a button to see an action");
}
});
// Add action listener for "Exit" button
buttonExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0); // Exit the application
}
});
// Create a panel and add buttons to it
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(buttonClickMe);
panel.add(buttonClear);
panel.add(buttonExit)
// Set the layout for the frame and add components
frame.setLayout(new BorderLayout());
frame.add(label, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
// Make the frame visible
frame.setVisible(true);
}
}
4
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3
import javax.swing.*;
public class JLabelExample {
public static void main(String[] args) {
// Create a JFrame (main window)
JFrame frame = new JFrame("JLabel Example");
5
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JCheckBoxExample {
public static void main(String[] args) {
// Create the main frame (window)
JFrame frame = new JFrame("JCheckBox Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 150);
// Create a label to show the checkbox status
JLabel label = new JLabel("Choose your option:", JLabel.CENTER);
// Create a JCheckBox
JCheckBoxcheckBox = new JCheckBox("I agree to the terms and conditions");
// Create a button to show the checkbox state
JButton button = new JButton("Show Checkbox Status");
// Add ActionListener to the button
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (checkBox.isSelected()) {
label.setText("Checkbox is selected!");
} else {
label.setText("Checkbox is deselected.");
}
6
}
});
7
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3
8
frame.setSize(300, 150);
frame.setVisible(true);
// Handle window closing
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
}
9
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3
10
@Override
public void actionPerformed(ActionEvent e) {
label.setText("Selected: Option 1");
}
});
checkBox2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText("Selected: Option 2");
}
});
checkBox3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText("Selected: Option 3");
}
});
// Add the components to the frame
frame.add(checkBox1);
frame.add(checkBox2);
frame.add(checkBox3);
frame.add(label);
// Set the frame properties
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
11
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3
12
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3
import javax.swing.*;
import java.awt.*;
public class JScrollPaneExample {
public static void main(String[] args) {
// Create a JFrame
JFrame frame = new JFrame("JScrollPane with JTextArea");
// Create a JTextArea (a large text area for demo)
JTextAreatextArea = new JTextArea(20, 30); // 20 rows, 30 columns
textArea.setText("This is a simple example of a JTextArea inside a JScrollPane. \n"
+ "Scroll vertically and horizontally to see the effect.");
// Create a JScrollPane and add the JTextArea to it
JScrollPanescrollPane = new JScrollPane(textArea);
// Add the JScrollPane to the frame
frame.add(scrollPane);
// Set frame properties
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}}
13
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3
14
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3
15
// Set frame properties
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
16
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3
17
label.setText("Key Released: " + KeyEvent.getKeyText(e.getKeyCode()));
}
});
18
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
19
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3
import javax.swing.*;
import java.awt.*;
20
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
21
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3
22
@Override
public void actionPerformed(ActionEvent e) {
// Display the content of the JTextArea in a dialog box
JOptionPane.showMessageDialog(frame, "You entered:\n" + textArea.getText());
}
});
23
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3
24
// Show a message dialog with the selected item
JOptionPane.showMessageDialog(frame, "You selected: " + selectedItem);
}
});
25
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3
26
// Show a message dialog with the selected item
JOptionPane.showMessageDialog(frame, "You selected: " + selectedItem);
}
});
27
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3
28
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3
29
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String selectedOption = "";
// Check which radio button is selected
if (radioButton1.isSelected()) {
selectedOption = "Option 1";
} else if (radioButton2.isSelected()) {
selectedOption = "Option 2";
} else if (radioButton3.isSelected()) {
selectedOption = "Option 3";
} else {
selectedOption = "No option selected";
}
// Show the selected option in a dialog box
JOptionPane.showMessageDialog(frame, "You selected: " + selectedOption);
}
});
// Set the layout manager for the frame
frame.setLayout(new FlowLayout());
30
}
31
// Create a JTree with the root node
JTree tree = new JTree(root);
32
}
33
response.getWriter().println("New cookie has been set.");
}
}
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3
// Set the cookie's max age (in seconds) - this cookie will expire in 1 hour
newCookie.setMaxAge(60 * 60); // 1 hour
34
Name: Shahaji S . Suryawanshi
Roll No:64
Batch:B3
21. Write a advance java code to create Delete a Cookie (Remove a Cookie)
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class CookieDeleteServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// Retrieve the cookies from the request
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("username")) {
// Set the cookie's max age to 0 to delete it
cookie.setMaxAge(0);
response.addCookie(cookie);
break;
}
}
}
35
out.println("<p>Cookie 'username' has been deleted.</p>");
}
}
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3
booleancookieFound = false;
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("username")) {
// Reset the cookie value
cookie.setValue("new_username");
36
}
}
}
37
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3
23. Write a advance java code to create Servlet to Handle Form Submission
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class FormServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// Get form data from request
String username = request.getParameter("username");
String password = request.getParameter("password");
38
Name: Shahaji S . Suryawanshi
Roll No:64
Batch:B3
39
response.getWriter().println("<p>New cookie 'username' created!</p>");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// Delete the cookie on POST request
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if ("username".equals(cookie.getName())) {
cookie.setMaxAge(0); // Delete the cookie
response.addCookie(cookie);
response.getWriter().println("<p>Cookie 'username' deleted.</p>");
break;
}
}
}
}
}
40
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B
if (cookies != null) {
for (Cookie cookie : cookies) {
if ("username".equals(cookie.getName())) {
// Set the cookie's max age to 0 to delete it
cookie.setMaxAge(0);
response.addCookie(cookie); // Send the updated cookie to the client to delete it
response.getWriter().println("<p>Cookie 'username' deleted.</p>");
break; // No need to continue once the cookie is deleted
}
}
} else {
response.getWriter().println("<p>No cookies found.</p>");
}
// Set content type for response
41
response.setContentType("text/html");
}
}
42