Here are Java programs to demonstrate the use of AWT and Swing components:
1. AWT Components: Label, TextField, TextArea, Button, Checkbox, RadioButton
import [Link].*;
import [Link].*;
public class AWTComponentsDemo {
public static void main(String[] args) {
Frame frame = new Frame("AWT Components Example");
Label label = new Label("Enter Text:");
[Link](50, 50, 100, 30);
TextField textField = new TextField();
[Link](150, 50, 200, 30);
TextArea textArea = new TextArea();
[Link](50, 100, 300, 100);
Button button = new Button("Submit");
[Link](50, 220, 100, 30);
Checkbox checkbox = new Checkbox("Accept Terms and Conditions");
[Link](50, 260, 200, 30);
CheckboxGroup group = new CheckboxGroup();
Checkbox radioButton1 = new Checkbox("Option 1", group, false);
[Link](50, 300, 100, 30);
Checkbox radioButton2 = new Checkbox("Option 2", group, false);
[Link](150, 300, 100, 30);
[Link](label);
[Link](textField);
[Link](textArea);
[Link](button);
[Link](checkbox);
[Link](radioButton1);
[Link](radioButton2);
[Link](400, 400);
[Link](null);
[Link](true);
[Link](new WindowAdapter() {
public void windowClosing(WindowEvent we) {
[Link](0);
});
2. Button with "OK", "Cancel", and "Retry" Labels
import [Link].*;
import [Link].*;
public class ButtonExample {
public static void main(String[] args) {
Frame frame = new Frame("Button Example");
Button okButton = new Button("OK");
Button cancelButton = new Button("Cancel");
Button retryButton = new Button("Retry");
[Link](50, 100, 80, 30);
[Link](150, 100, 80, 30);
[Link](250, 100, 80, 30);
[Link](okButton);
[Link](cancelButton);
[Link](retryButton);
[Link](400, 300);
[Link](null);
[Link](true);
[Link](new WindowAdapter() {
public void windowClosing(WindowEvent we) {
[Link](0);
});
3. Form with List and Choice
import [Link].*;
import [Link].*;
public class ListChoiceExample {
public static void main(String[] args) {
Frame frame = new Frame("List and Choice Example");
List list = new List();
[Link](50, 50, 150, 100);
[Link]("Item 1");
[Link]("Item 2");
[Link]("Item 3");
Choice choice = new Choice();
[Link](50, 200, 150, 30);
[Link]("Option A");
[Link]("Option B");
[Link]("Option C");
[Link](list);
[Link](choice);
[Link](300, 300);
[Link](null);
[Link](true);
[Link](new WindowAdapter() {
public void windowClosing(WindowEvent we) {
[Link](0);
});
4. Grid of 5x5
import [Link].*;
public class GridExample {
public static void main(String[] args) {
Frame frame = new Frame("5x5 Grid Example");
[Link](new GridLayout(5, 5));
for (int i = 1; i <= 25; i++) {
[Link](new Button("Button " + i));
[Link](400, 400);
[Link](true);
[Link](new WindowAdapter() {
public void windowClosing(WindowEvent we) {
[Link](0);
});
5. CardLayout Example
import [Link].*;
import [Link].*;
public class CardLayoutExample {
public static void main(String[] args) {
Frame frame = new Frame("CardLayout Example");
Panel panel = new Panel();
CardLayout cardLayout = new CardLayout();
[Link](cardLayout);
Button button1 = new Button("Card 1");
Button button2 = new Button("Card 2");
Button button3 = new Button("Card 3");
[Link](button1, "1");
[Link](button2, "2");
[Link](button3, "3");
Button switchButton = new Button("Next Card");
[Link](50, 200, 100, 30);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link](panel);
});
[Link](panel, [Link]);
[Link](switchButton, [Link]);
[Link](400, 400);
[Link](true);
[Link](new WindowAdapter() {
public void windowClosing(WindowEvent we) {
[Link](0);
});
6. AWT Menubar with Submenu
import [Link].*;
import [Link].*;
public class MenuBarExample {
public static void main(String[] args) {
Frame frame = new Frame("AWT Menu Bar Example");
MenuBar menuBar = new MenuBar();
Menu fileMenu = new Menu("File");
MenuItem newItem = new MenuItem("New");
MenuItem openItem = new MenuItem("Open");
[Link](newItem);
[Link](openItem);
Menu editMenu = new Menu("Edit");
Menu viewMenu = new Menu("View");
[Link](fileMenu);
[Link](editMenu);
[Link](viewMenu);
[Link](menuBar);
[Link](400, 300);
[Link](true);
[Link](new WindowAdapter() {
public void windowClosing(WindowEvent we) {
[Link](0);
});
7. JComboBox in a JApplet
import [Link].*;
import [Link];
public class ComboBoxApplet extends Applet {
public void init() {
String[] languages = {"English", "Marathi", "Hindi", "Sanskrit"};
JComboBox<String> comboBox = new JComboBox<>(languages);
add(comboBox);
8. Creating a JTree
import [Link].*;
import [Link].*;
public class JTreeExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JTree Example");
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Node 1");
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Node 2");
[Link](node1);
[Link](node2);
JTree tree = new JTree(root);
[Link](new JScrollPane(tree));
[Link](300, 300);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
9. Creating a JTable
import [Link].*;
import [Link].*;
public class JTableExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JTable Example");
String[] columnNames = {"ID", "Name", "Age"};
Object[][] data = {
{1, "Alice", 23},
{2, "Bob", 30},
{3, "Charlie", 22}
};
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
[Link](scrollPane);
[Link](400, 200);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
10. JProgressBar Example
import [Link].*;
import [Link].*;
public class JProgressBarExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JProgressBar Example");
JProgressBar progressBar = new JProgressBar(0, 100);
[Link](50);
[Link](true);
[Link](progressBar, [Link]);
[Link](400, 100);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
Certainly! Below are the Java programs demonstrating various concepts such as key events, action
events, mouse events, JTextField, window adaptor, InetAddress, URLClass, chat server using
ServerSocket, and JDBC operations.
11. Program to Demonstrate Key Events in Applet (KeyPressed, KeyReleased)
import [Link].*;
import [Link].*;
import [Link].*;
public class KeyEventDemo extends Applet {
String msg = "";
public void init() {
setSize(300, 300);
public void paint(Graphics g) {
[Link](msg, 50, 100);
}
public boolean handleEvent(Event e) {
if ([Link] == Event.KEY_PRESS) {
msg = "Key Pressed: " + (char) [Link];
repaint();
return true;
if ([Link] == Event.KEY_RELEASE) {
msg = "Key Released: " + (char) [Link];
repaint();
return true;
return [Link](e);
12. Program to Demonstrate ActionEvent
import [Link].*;
import [Link].*;
public class ActionEventDemo {
public static void main(String[] args) {
Frame frame = new Frame("ActionEvent Demo");
Button button = new Button("Click Me");
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link]("Button clicked!");
});
[Link](button);
[Link](300, 200);
[Link](new FlowLayout());
[Link](true);
[Link](new WindowAdapter() {
public void windowClosing(WindowEvent we) {
[Link](0);
});
13. Program to Demonstrate Various Mouse Events
import [Link].*;
import [Link].*;
public class MouseEventDemo {
public static void main(String[] args) {
Frame frame = new Frame("MouseEvent Demo");
[Link](new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
[Link]("Mouse Clicked at: " + [Link]() + ", " + [Link]());
public void mousePressed(MouseEvent e) {
[Link]("Mouse Pressed");
public void mouseReleased(MouseEvent e) {
[Link]("Mouse Released");
}
public void mouseEntered(MouseEvent e) {
[Link]("Mouse Entered");
public void mouseExited(MouseEvent e) {
[Link]("Mouse Exited");
});
[Link](400, 400);
[Link](true);
[Link](new WindowAdapter() {
public void windowClosing(WindowEvent we) {
[Link](0);
});
14. Program to Demonstrate Use of JTextField
import [Link].*;
import [Link].*;
public class JTextFieldDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextField Demo");
JTextField textField = new JTextField(20);
[Link](50, 50, 200, 30);
JButton button = new JButton("Submit");
[Link](50, 100, 100, 30);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link]("Text Entered: " + [Link]());
});
[Link](textField);
[Link](button);
[Link](300, 300);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
15. Program to Demonstrate WindowAdapter Class
import [Link].*;
import [Link].*;
public class WindowAdapterDemo {
public static void main(String[] args) {
Frame frame = new Frame("WindowAdapter Demo");
[Link](300, 300);
[Link](true);
[Link](new WindowAdapter() {
public void windowClosing(WindowEvent we) {
[Link]("Window is closing.");
[Link](0);
});
16. Program to Demonstrate Use of InetAddress Class
import [Link].*;
public class InetAddressDemo {
public static void main(String[] args) throws UnknownHostException {
InetAddress localHost = [Link]();
[Link]("Local Host: " + [Link]() + " - " +
[Link]());
InetAddress google = [Link]("[Link]");
[Link]("Google Host: " + [Link]() + " - " + [Link]());
17. Program to Demonstrate Use of URL Class to Retrieve Host, Protocol, Port, and File
import [Link].*;
public class URLDemo {
public static void main(String[] args) throws MalformedURLException {
try {
URL url = new URL("[Link]
[Link]("Protocol: " + [Link]());
[Link]("Host: " + [Link]());
[Link]("Port: " + [Link]());
[Link]("File: " + [Link]());
} catch (MalformedURLException e) {
[Link]();
18. Program to Demonstrate Chat Server using ServerSocket
Chat Server ([Link])
import [Link].*;
import [Link].*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(1234);
[Link]("Server started. Waiting for client...");
Socket socket = [Link]();
[Link]("Client connected: " + [Link]());
BufferedReader input = new BufferedReader(new InputStreamReader([Link]()));
PrintWriter output = new PrintWriter([Link](), true);
String message;
while ((message = [Link]()) != null) {
[Link]("Client: " + message);
[Link]("Server: " + message);
[Link]();
[Link]();
Chat Client ([Link])
import [Link].*;
import [Link].*;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 1234);
BufferedReader input = new BufferedReader(new InputStreamReader([Link]()));
PrintWriter output = new PrintWriter([Link](), true);
BufferedReader console = new BufferedReader(new InputStreamReader([Link]));
String message;
while (true) {
[Link]("Client: ");
message = [Link]();
[Link](message);
[Link]("Server: " + [Link]());
19. Program to Insert and Retrieve Data from Database Using JDBC
Database Table Setup (MySQL)
CREATE DATABASE jdbc_example;
USE jdbc_example;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100)
);
INSERT INTO users (name, email) VALUES ('John Doe', 'john@[Link]');
JDBC Program to Insert and Retrieve Data
import [Link].*;
public class JDBCExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/jdbc_example";
String user = "root";
String password = "password";
try (Connection connection = [Link](url, user, password)) {
// Insert data
String insertQuery = "INSERT INTO users (name, email) VALUES (?, ?)";
try (PreparedStatement insertStmt = [Link](insertQuery)) {
[Link](1, "Alice Smith");
[Link](2, "alice@[Link]");
[Link]();
[Link]("Data inserted successfully.");
// Retrieve data
String selectQuery = "SELECT * FROM users";
try (Statement stmt = [Link]();
ResultSet rs = [Link](selectQuery)) {
while ([Link]()) {
[Link]("ID: " + [Link]("id"));
[Link]("Name: " + [Link]("name"));
[Link]("Email: " + [Link]("email"));
} catch (SQLException e) {
[Link]();
}
import [Link].*;
public class JDBCUpdateDelete {
// JDBC URL, username, and password for MySQL
private static final String URL = "jdbc:mysql://localhost:3306/jdbc_example";
private static final String USER = "root";
private static final String PASSWORD = "password"; // Replace with your MySQL password
// Method to update a record
public static void updateRecord(int id, String newName, String newEmail) {
String updateQuery = "UPDATE users SET name = ?, email = ? WHERE id = ?";
try (Connection connection = [Link](URL, USER, PASSWORD);
PreparedStatement stmt = [Link](updateQuery)) {
// Set parameters for the update query
[Link](1, newName);
[Link](2, newEmail);
[Link](3, id);
// Execute the update
int rowsUpdated = [Link]();
if (rowsUpdated > 0) {
[Link]("Record updated successfully.");
} else {
[Link]("ID " + id + " not found.");
} catch (SQLException e) {
[Link]();
}
// Method to delete a record
public static void deleteRecord(int id) {
String deleteQuery = "DELETE FROM users WHERE id = ?";
try (Connection connection = [Link](URL, USER, PASSWORD);
PreparedStatement stmt = [Link](deleteQuery)) {
// Set parameter for the delete query
[Link](1, id);
// Execute the delete
int rowsDeleted = [Link]();
if (rowsDeleted > 0) {
[Link]("Record deleted successfully.");
} else {
[Link]("ID " + id + " not found.");
} catch (SQLException e) {
[Link]();
// Method to retrieve all records (for verification)
public static void getAllRecords() {
String selectQuery = "SELECT * FROM users";
try (Connection connection = [Link](URL, USER, PASSWORD);
Statement stmt = [Link]();
ResultSet resultSet = [Link](selectQuery)) {
[Link]("Users in the database:");
while ([Link]()) {
int id = [Link]("id");
String name = [Link]("name");
String email = [Link]("email");
[Link]("ID: " + id + ", Name: " + name + ", Email: " + email);
} catch (SQLException e) {
[Link]();
// Main method to demonstrate update and delete operations
public static void main(String[] args) {
// Display all records before update and delete
getAllRecords();
// Update record with ID 1
updateRecord(1, "Johnathan Doe", "[Link]@[Link]");
// Display all records after update
getAllRecords();
// Delete record with ID 2
deleteRecord(2);
// Display all records after delete
getAllRecords();
}
For Question No 20:-
Database Setup (MySQL)
Before running the program, make sure the following database setup is done in MySQL:
sql
Copy code
CREATE DATABASE jdbc_example;
USE jdbc_example;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100)
);
-- Insert sample data
INSERT INTO users (name, email) VALUES ('John Doe', 'john@[Link]');
INSERT INTO users (name, email) VALUES ('Jane Smith', 'jane@[Link]');