Java Assignment Solutions - Assignment 3
1. Write a program to print text into Text Area using JTextArea.
import [Link].*;
public class JTextAreaExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Example");
JTextArea textArea = new JTextArea("Hello, JTextArea!");
[Link](20, 20, 300, 200);
[Link](textArea);
[Link](400, 300);
[Link](null);
[Link](true);
2. Create this frame using JFrame.
import [Link].*;
public class JFrameExample {
public static void main(String[] args) {
JFrame frame = new JFrame("My JFrame Example");
JLabel label = new JLabel("This is a JFrame.");
[Link](50, 50, 200, 30);
[Link](label);
[Link](400, 300);
[Link](null);
[Link](true);
3. Write a program to display selected item from JComboBox in a label on click of Show
button.
import [Link].*;
import [Link].*;
public class JComboBoxExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JComboBox Example");
String[] items = {"Item 1", "Item 2", "Item 3"};
JComboBox<String> comboBox = new JComboBox<>(items);
[Link](50, 50, 150, 30);
JLabel label = new JLabel("Selected: ");
[Link](50, 150, 200, 30);
JButton button = new JButton("Show");
[Link](50, 100, 80, 30);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link]("Selected: " +
[Link]([Link]()));
});
[Link](comboBox);
[Link](label);
[Link](button);
[Link](400, 300);
[Link](null);
[Link](true);
4. Create a swing application to display a menu bar where each menu item should display a
dialog box to show its working.
import [Link].*;
import [Link].*;
public class MenuBarExample {
public static void main(String[] args) {
JFrame frame = new JFrame("MenuBar Example");
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem open = new JMenuItem("Open");
JMenuItem save = new JMenuItem("Save");
JMenuItem exit = new JMenuItem("Exit");
[Link](e -> [Link](frame, "Open
clicked!"));
[Link](e -> [Link](frame, "Save
clicked!"));
[Link](e -> [Link](0));
[Link](open);
[Link](save);
[Link](exit);
[Link](menu);
[Link](menuBar);
[Link](400, 300);
[Link](null);
[Link](true);
5. Create this frame message that will show according to the radio button clicked.
import [Link].*;
import [Link].*;
public class RadioButtonExample {
public static void main(String[] args) {
JFrame frame = new JFrame("RadioButton Example");
JRadioButton rb1 = new JRadioButton("Option 1");
[Link](50, 50, 100, 30);
JRadioButton rb2 = new JRadioButton("Option 2");
[Link](50, 100, 100, 30);
ButtonGroup group = new ButtonGroup();
[Link](rb1);
[Link](rb2);
JLabel label = new JLabel();
[Link](50, 150, 200, 30);
[Link](e -> [Link]("Option 1 Selected"));
[Link](e -> [Link]("Option 2 Selected"));
[Link](rb1);
[Link](rb2);
[Link](label);
[Link](400, 300);
[Link](null);
[Link](true);
6. Create this food ordering system.
import [Link].*;
import [Link].*;
public class FoodOrderingSystem {
public static void main(String[] args) {
JFrame frame = new JFrame("Food Ordering System");
JLabel label = new JLabel("Select your food:");
[Link](50, 50, 200, 30);
JCheckBox pizza = new JCheckBox("Pizza - $10");
[Link](50, 100, 200, 30);
JCheckBox burger = new JCheckBox("Burger - $5");
[Link](50, 150, 200, 30);
JCheckBox sandwich = new JCheckBox("Sandwich - $7");
[Link](50, 200, 200, 30);
JButton button = new JButton("Order");
[Link](50, 250, 80, 30);
JLabel result = new JLabel("");
[Link](50, 300, 300, 30);
[Link](e -> {
int total = 0;
StringBuilder order = new StringBuilder("You ordered: ");
if ([Link]()) {
[Link]("Pizza ");
total += 10;
}
if ([Link]()) {
[Link]("Burger ");
total += 5;
if ([Link]()) {
[Link]("Sandwich ");
total += 7;
[Link]("- Total: $").append(total);
[Link]([Link]());
});
[Link](label);
[Link](pizza);
[Link](burger);
[Link](sandwich);
[Link](button);
[Link](result);
[Link](400, 400);
[Link](null);
[Link](true);
7. Write a program to implement the concept of threading by extending Thread Class.
class MyThread extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
[Link]("Thread running: " + i);
try {
[Link](500);
} catch (InterruptedException e) {
[Link]();
public class ThreadExample {
public static void main(String[] args) {
MyThread t = new MyThread();
[Link]();
8. Write a program to implement the concept of threading by implementing Runnable
Interface.
class MyRunnable implements Runnable {
public void run() {
for (int i = 1; i <= 5; i++) {
[Link]("Runnable running: " + i);
try {
[Link](500);
} catch (InterruptedException e) {
[Link]();
public class RunnableExample {
public static void main(String[] args) {
Thread t = new Thread(new MyRunnable());
[Link]();
9. Write a Java program that connects to a database using JDBC.
import [Link].*;
public class DatabaseConnection {
public static void main(String[] args) {
try {
Connection conn =
[Link]("jdbc:mysql://localhost:3306/mydatabase", "username",
"password");
[Link]("Connected to the database successfully!");
[Link]();
} catch (SQLException e) {
[Link]("An error occurred: " + [Link]());
10. Write a program to enter the details of an employee in a window and add the record to the
database on button click.
import [Link].*;
import [Link].*;
import [Link].*;
public class EmployeeDetails {
public static void main(String[] args) {
JFrame frame = new JFrame("Employee Details");
JLabel nameLabel = new JLabel("Name:");
[Link](50, 50, 100, 30);
JTextField nameField = new JTextField();
[Link](150, 50, 150, 30);
JButton button = new JButton("Add");
[Link](150, 100, 80, 30);
JLabel result = new JLabel("");
[Link](50, 150, 300, 30);
[Link](e -> {
String name = [Link]();
try (Connection conn =
[Link]("jdbc:mysql://localhost:3306/mydatabase", "username",
"password")) {
String query = "INSERT INTO employees (name) VALUES (?)";
PreparedStatement stmt = [Link](query);
[Link](1, name);
[Link]();
[Link]("Employee added successfully!");
} catch (SQLException ex) {
[Link]("Error: " + [Link]());
});
[Link](nameLabel);
[Link](nameField);
[Link](button);
[Link](result);
[Link](400, 300);
[Link](null);
[Link](true);