0% found this document useful (0 votes)
17 views

AJP PROGRAMFILE Ss

Uploaded by

shahajis3486
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

AJP PROGRAMFILE Ss

Uploaded by

shahajis3486
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 42

AJP PROGRAM FILE

Name: Shahaji S. Suryawanshi

Roll No:64

Batch:B3

1
AJP PROGRAM’s

2
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3

1.Write a advance java code to create Button

62import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonProject {

public static void main(String[] args) {


// Create the main frame (window)
JFrame frame = new JFrame("Button Project");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);

// Create a label to display messages


JLabel label = new JLabel("Press a button to see an action", SwingConstants.CENTER);
// Create the buttons
JButtonbuttonClickMe = new JButton("Click Me!");
JButtonbuttonClear = new JButton("Clear");
JButtonbuttonExit = new JButton("Exit");
// Add action listener for "Click Me!" button
buttonClickMe.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText("Button Clicked!");

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

2.Write a advance java code to create LABEL

import javax.swing.*;
public class JLabelExample {
public static void main(String[] args) {
// Create a JFrame (main window)
JFrame frame = new JFrame("JLabel Example");

// Create a JLabel to display text


JLabel label = new JLabel("Hello, this is a JLabel!", JLabel.CENTER);

// Set the font of the label


label.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD, 20));

// Set the label's foreground color (text color)


label.setForeground(java.awt.Color.RED);

// Add the label to the frame


frame.add(label);

// Set the frame size and make it visible


frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

5
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3

3.Write a advance java code to create CHECKBOX

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
}
});

// Create a JPanel to hold the components


JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(label);
panel.add(checkBox);
panel.add(button);
// Add the panel to the frame
frame.add(panel);
// Make the frame visible
frame.setVisible(true);
}
}

7
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3

4.Write a advance java code to create CHOICE


import java.awt.*;
import java.awt.event.*;
public class ChoiceExample {
public static void main(String[] args) {
// Create a frame (window)
Frame frame = new Frame("AWT Choice Example");
// Create a choice (dropdown)
Choice choice = new Choice();
// Add items to the choice
choice.add("Option 1");
choice.add("Option 2");
choice.add("Option 3");
choice.add("Option 4");
// Create a label to display the selected item
Label label = new Label("Select an option:", Label.CENTER);
// Add a listener to the choice to detect selection
choice.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
// Get the selected item and display it
label.setText("Selected: " + choice.getSelectedItem());
}
});
// Add components to the frame
frame.add(label, BorderLayout.NORTH);
frame.add(choice, BorderLayout.CENTER);
// Set the frame properties

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

5.Write a advance java code to create Jcheckbox


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JCheckBoxGroupExample {
public static void main(String[] args) {
// Create a JFrame (main window)
JFrame frame = new JFrame("Checkbox Group Example");
// Set the layout manager for the frame
frame.setLayout(new FlowLayout());
// Create JCheckBox components
JCheckBox checkBox1 = new JCheckBox("Option 1");
JCheckBox checkBox2 = new JCheckBox("Option 2");
JCheckBox checkBox3 = new JCheckBox("Option 3");
// Create a ButtonGroup to group the checkboxes
ButtonGroup group = new ButtonGroup();
// Add the checkboxes to the group
group.add(checkBox1);
group.add(checkBox2);
group.add(checkBox3);
// Create a label to display the selected checkbox
JLabel label = new JLabel("Selected: None");
// Add ActionListeners to the checkboxes
checkBox1.addActionListener(new ActionListener()

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

6. Write a advance java code to create Scrollbar


import java.awt.*;
import java.awt.event.*;
public class ScrollbarExample {
public static void main(String[] args) {
// Create a Frame
Frame frame = new Frame("Scrollbar Example");
// Create a vertical scrollbar
Scrollbar vScrollbar = new Scrollbar();
vScrollbar.setOrientation(Scrollbar.VERTICAL);
vScrollbar.setBounds(50, 50, 20, 200); // x, y, width, height
// Create a horizontal scrollbar
Scrollbar hScrollbar = new Scrollbar();
hScrollbar.setOrientation(Scrollbar.HORIZONTAL);
hScrollbar.setBounds(50, 250, 200, 20); // x, y, width, height
// Add Scrollbars to the frame
frame.add(vScrollbar);
frame.add(hScrollbar);
// Set layout and size
frame.setLayout(null);
frame.setSize(300, 300);
frame.setVisible(true);
// Add WindowListener to close the program when the window is closed
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
} });

12
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3

7. Write a advance java code to create JScrollPane with JTextArea

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

Event Handling with Button Click


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class EventHandlingExample {
public static void main(String[] args) {
// Create a JFrame (the main window)
JFrame frame = new JFrame("Event Handling Example");
// Create a JButton (the event source)
JButton button = new JButton("Click Me");
// Create a JLabel to display messages
JLabel label = new JLabel("Hello, click the button!", JLabel.CENTER);
// Create an ActionListener to handle button click events
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Update the label text when the button is clicked
label.setText("Button Clicked!");
}
});
frame.setLayout(new BorderLayout());
frame.add(button, BorderLayout.SOUTH); // Button at the bottom
frame.add(label, BorderLayout.CENTER); // Label in the center
// Set frame properties
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);}}

14
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3

8. Write a advance java code to create MouseListener


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MouseEventHandlingExample {
public static void main(String[] args) {
// Create a JFrame
JFrame frame = new JFrame("Mouse Event Handling Example");
// Create a JPanel to capture mouse events
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(300, 200));
// Create a JLabel to display mouse coordinates
JLabel label = new JLabel("Click anywhere on the panel", JLabel.CENTER);
// Add a MouseListener to the panel to detect mouse clicks
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// Get the mouse coordinates and update the label
int x = e.getX();
int y = e.getY();
label.setText("Mouse Clicked at: (" + x + ", " + y + ")");
}
});
// Set up the layout for the frame
frame.setLayout(new BorderLayout());
frame.add(label, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);

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

9. Write a advance java code to create KEY EVENT


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class KeyEventExample {
public static void main(String[] args) {
// Create a JFrame (window)
JFrame frame = new JFrame("Key Event Handling Example");
// Create a JTextField to capture key input
JTextFieldtextField = new JTextField(20);
// Create a JLabel to display key event details
JLabel label = new JLabel("Press any key in the text field", JLabel.CENTER);
// Add a KeyListener to the text field
textField.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// Display the character of the key typed
label.setText("Key Typed: " + e.getKeyChar());
}
@Override
public void keyPressed(KeyEvent e) {
// Display the key code when a key is pressed
label.setText("Key Pressed: " + KeyEvent.getKeyText(e.getKeyCode()));
}
@Override
public void keyReleased(KeyEvent e) {
// Display the key code when the key is released

17
label.setText("Key Released: " + KeyEvent.getKeyText(e.getKeyCode()));
}
});

// Set up the layout for the frame


frame.setLayout(new BorderLayout());
frame.add(label, BorderLayout.NORTH);
frame.add(textField, BorderLayout.CENTER);
// Set frame properties
frame.setSize(400, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

18
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3

10. Write a advance java code to create JBUTTON.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JButtonExample {

public static void main(String[] args) {


// Create a new frame (window)
JFrame frame = new JFrame("JButton Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);

// Create a button with text


JButton button = new JButton("Click Me");

// Add an ActionListener to the button


button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Display a message when the button is clicked
JOptionPane.showMessageDialog(frame, "Button was clicked!");
}
});

// Set the layout manager (optional)


frame.setLayout(new FlowLayout());

// Add the button to the frame


frame.add(button);

// Make the frame visible


frame.setVisible(true);
}
}

19
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3

11. Write a advance java code to create JLEBAL

import javax.swing.*;
import java.awt.*;

public class JLabelExample {

public static void main(String[] args) {


// Create a new frame (window)
JFrame frame = new JFrame("JLabel Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);

// Create a JLabel with some text


JLabel label = new JLabel("Hello, welcome to Java!");

// Set some properties for the JLabel (optional)


label.setFont(new Font("Arial", Font.BOLD, 16)); // Set font
label.setForeground(Color.BLUE); // Set text color

// Set the layout manager


frame.setLayout(new FlowLayout());

// Add the JLabel to the frame


frame.add(label);

// Make the frame visible


frame.setVisible(true);
}
}

20
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3

12. Write a advance java code to create JTEXT

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JTextFieldExample {

public static void main(String[] args) {


// Create a new frame (window)
JFrame frame = new JFrame("JTextField Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);

// Create a JTextField for single-line input


JTextFieldtextField = new JTextField(20); // 20 columns wide

// Create a JButton to display the text entered in the JTextField


JButton button = new JButton("Show Text");

// Add an ActionListener to the button


button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Display the text entered in the JTextField
JOptionPane.showMessageDialog(frame, "You entered: " + textField.getText());
}
});

// Set the layout manager


frame.setLayout(new FlowLayout());

// Add the components to the frame


frame.add(textField);
frame.add(button);

// Make the frame visible


frame.setVisible(true);
}

21
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3

13.Write a advance java code to create JTEXTAREA


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JTextAreaExample {

public static void main(String[] args) {


// Create a new JFrame (window)
JFrame frame = new JFrame("JTextArea Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);

// Create a JTextArea for multi-line text input


JTextAreatextArea = new JTextArea(10, 30); // 10 rows, 30 columns
textArea.setLineWrap(true); // Enable line wrapping
textArea.setWrapStyleWord(true); // Wrap at word boundaries

// Add a JScrollPane to make the JTextArea scrollable


JScrollPanescrollPane = new JScrollPane(textArea);

// Create a JButton to show the text entered in the JTextArea


JButton button = new JButton("Show Text");

// Add an ActionListener to the button


button.addActionListener(new ActionListener() {

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());
}
});

// Set the layout manager for the frame


frame.setLayout(new BorderLayout());

// Add the JScrollPane (with the JTextArea inside) to the frame


frame.add(scrollPane, BorderLayout.CENTER);

// Add the button to the bottom of the frame


frame.add(button, BorderLayout.SOUTH);

// Make the frame visible


frame.setVisible(true);
}
}

23
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3

14. Write a advance java code to create JButtonComboBox


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JButtonComboBoxExample {

public static void main(String[] args) {


// Create the main frame
JFrame frame = new JFrame("JButton and JComboBox Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);

// Create a JComboBox with several options


String[] options = {"Apple", "Banana", "Orange", "Grapes"};
JComboBox<String>comboBox = new JComboBox<>(options);

// Create a JButton that will show a message based on the selection


JButton button = new JButton("Show Selection");

// Add an ActionListener to the button


button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Get the selected item from the JComboBox
String selectedItem = (String) comboBox.getSelectedItem();

24
// Show a message dialog with the selected item
JOptionPane.showMessageDialog(frame, "You selected: " + selectedItem);
}
});

// Set a layout manager for the frame


frame.setLayout(new FlowLayout());

// Add the JComboBox and JButton to the frame


frame.add(comboBox);
frame.add(button);

// Make the frame visible


frame.setVisible(true);
}
}

25
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3

15. Write a advance java code to create JCheckBox


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JButtonComboBoxExample {

public static void main(String[] args) {


// Create the main frame
JFrame frame = new JFrame("JButton and JComboBox Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);

// Create a JComboBox with several options


String[] options = {"Apple", "Banana", "Orange", "Grapes"};
JComboBox<String>comboBox = new JComboBox<>(options);

// Create a JButton that will show a message based on the selection


JButton button = new JButton("Show Selection");

// Add an ActionListener to the button


button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Get the selected item from the JComboBox
String selectedItem = (String) comboBox.getSelectedItem();

26
// Show a message dialog with the selected item
JOptionPane.showMessageDialog(frame, "You selected: " + selectedItem);
}
});

// Set a layout manager for the frame


frame.setLayout(new FlowLayout());
// Add the JComboBox and JButton to the frame
frame.add(comboBox);
frame.add(button);
// Make the frame visible
frame.setVisible(true);
}
}

27
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3

16. Write a advance java code to create JFRAME


import javax.swing.*;
public class SimpleSwingApp {
public static void main(String[] args) {
// Create a JFrame (window)
JFrame frame = new JFrame("Simple Swing App");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Create a JLabel to display the text


JLabel label = new JLabel("Hello, this is a Swing application!", SwingConstants.CENTER);

// Add the label to the frame


frame.add(label);

// Make the frame visible


frame.setVisible(true);
}
}

28
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3

17. Write a advance java code to create JREDIOBUTTON


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JRadioButtonExample {
public static void main(String[] args) {
// Create the main frame (window)
JFrame frame = new JFrame("JRadioButton Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
// Create the radio buttons
JRadioButton radioButton1 = new JRadioButton("Option 1");
JRadioButton radioButton2 = new JRadioButton("Option 2");
JRadioButton radioButton3 = new JRadioButton("Option 3");

// Group the radio buttons so only one can be selected at a time


ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
group.add(radioButton3);

// Create a button that will show the selected radio button


JButton button = new JButton("Show Selected Option");

// Add an ActionListener to the button

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());

// Add the radio buttons and button to the frame


frame.add(radioButton1);
frame.add(radioButton2);
frame.add(radioButton3);
frame.add(button);

// Make the frame visible


frame.setVisible(true);
}

30
}

Name: Shahaji S. Suryawanshi


Roll No:64
Batch:B3

18. Write a advance java code to create JTREE


import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

public class JTreeExample {

public static void main(String[] args) {


// Create the root node
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");

// Create child nodes


DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Child 1");
DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Child 2");

// Add children to the root node


root.add(child1);
root.add(child2);

// Create sub-child nodes for "Child 1"


DefaultMutableTreeNode subChild1 = new DefaultMutableTreeNode("Sub Child 1");
DefaultMutableTreeNode subChild2 = new DefaultMutableTreeNode("Sub Child 2");
child1.add(subChild1);
child1.add(subChild2);

31
// Create a JTree with the root node
JTree tree = new JTree(root);

// Add a button to show the selected node


JButton button = new JButton("Show Selected Node");
button.addActionListener(e -> {
// Get the selected node
TreePathselectedPath = tree.getSelectionPath();
if (selectedPath != null) {
// Show the selected node in a message dialog
JOptionPane.showMessageDialog(null, "Selected Node: " +
selectedPath.getLastPathComponent());
} else {
JOptionPane.showMessageDialog(null, "No node selected.");
}
});

// Create a panel to add the tree and button


JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(new JScrollPane(tree)); // Add the tree to the panel inside a scroll pane
panel.add(button); // Add the button below the tree

// Create the main frame


JFrame frame = new JFrame("JTree Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.add(panel); // Add the panel to the frame
frame.setVisible(true);
}

32
}

Name: Shahaji S . Suryawanshi


Roll No:64
Batch:B3

19.Sample Servlet Code to Read and Set Cookies:


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class CookieExampleServlet extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// Retrieve cookies sent by the client
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("userSession")) {
response.getWriter().println("Session ID: " + cookie.getValue());
}
}
} else {
response.getWriter().println("No cookies found.");
}

// Set a new cookie


Cookie newCookie = new Cookie("userSession", "12345");
newCookie.setMaxAge(60 * 60); // 1 hour
response.addCookie(newCookie);

33
response.getWriter().println("New cookie has been set.");
}
}
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3

20.Create a Cookie (Set a New Cookie)


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class CookieCreateServlet extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// Create a new cookie
Cookie newCookie = new Cookie("username", "john_doe");

// Set the cookie's max age (in seconds) - this cookie will expire in 1 hour
newCookie.setMaxAge(60 * 60); // 1 hour

// Add the cookie to the response


response.addCookie(newCookie);

// Send a response to the client


response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>Cookie Created!</h1>");
out.println("<p>Cookie 'username' set to 'john_doe'.</p>");
}
}

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;
}
}
}

// Send a response to the client


response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>Cookie Deleted!</h1>");

35
out.println("<p>Cookie 'username' has been deleted.</p>");
}
}
Name: Shahaji S. Suryawanshi
Roll No:64
Batch:B3

22.Reset a Cookie (Update an Existing Cookie)


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class CookieResetServlet extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// Retrieve the cookies from the request
Cookie[] cookies = request.getCookies();

booleancookieFound = false;
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("username")) {
// Reset the cookie value
cookie.setValue("new_username");

// Set the cookie's max age (optional)


cookie.setMaxAge(60 * 60); // 1 hour

// Add the updated cookie to the response


response.addCookie(cookie);
cookieFound = true;
break;

36
}
}
}

// Send a response to the client


response.setContentType("text/html");
PrintWriter out = response.getWriter();
if (cookieFound) {
out.println("<h1>Cookie Reset!</h1>");
out.println("<p>Cookie 'username' has been reset to 'new_username'.</p>");
} else {
out.println("<h1>No cookie found!</h1>");
out.println("<p>Cookie 'username' was not found in your request.</p>");
}
}
}

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");

// Set response content type


response.setContentType("text/html");

// Write response message


PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>Form Submission</h1>");
out.println("<p>Username: " + username + "</p>");
out.println("<p>Password: " + password + "</p>");
out.println("</body></html>");
}
}

38
Name: Shahaji S . Suryawanshi
Roll No:64
Batch:B3

24. Write a advance java code to create Servlet to Set Cookies


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class CookieServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// Check if there is an existing cookie
Cookie[] cookies = request.getCookies();
booleancookieFound = false;
if (cookies != null) {
for (Cookie cookie : cookies) {
if ("username".equals(cookie.getName())) {
cookieFound = true;
response.getWriter().println("<p>Cookie value: " + cookie.getValue() + "</p>");
}
}
}

// If no cookie, create one


if (!cookieFound) {
Cookie newCookie = new Cookie("username", "john_doe");
newCookie.setMaxAge(60 * 60); // 1 hour
response.addCookie(newCookie);

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

25. Write a advance java code to create Servlet to Destroy a Cookie


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class DestroyCookieServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// Retrieve all cookies sent by the client
Cookie[] cookies = request.getCookies();

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

You might also like