Tasks For Final Project
Tasks For Final Project
1. Explore the Swing library for graphic design. You need to redesign your project using
graphic design. Your project should have functions such as creating, adding, changing
and deleting data.
2. Connect your project to a database for storing and processing data. I suggest using
MySQL.
Tasks:
Creating a Simple GUI Using Components
Swing.
Handling events that interact with user interface elements.
Work with layouts to organize the layout of components.
Project setup
Create a new Java project in your IDE.
Make sure the project is configured to use JDK 8 or higher.
Creating a GUI
Create a Main class that will contain the main method.
Import the required packages:
Example 1
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public GUI() {
setTitle("Sample app by Swing");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
1
label = new JLabel("Text");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText("Button is clicked!");
}
});
add(button);
add(label);
setVisible(true);
}
Example 2
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public AdvancedGUI() {
setTitle("Advanced Swing Application");
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
2
inputField = new JTextField(15);
submitButton = new JButton("Submit");
topPanel.add(inputLabel);
topPanel.add(inputField);
topPanel.add(submitButton);
add(topPanel, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
bottomPanel.add(selectLabel);
bottomPanel.add(comboBox);
add(bottomPanel, BorderLayout.SOUTH);
submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String inputText = inputField.getText();
outputArea.append("Input: " + inputText + "\n");
inputField.setText("");
}
});
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String selectedOption = (String) comboBox.getSelectedItem();
outputArea.append("Selected: " + selectedOption + "\n");
}
});
setVisible(true);
}
3
}
}
Database
Objective: To familiarize students with the basics of working with databases in Java using the
MySQL database management system. The assignment includes creating a database, writing a
Java application to connect to the database, execute basic queries (SELECT, INSERT, UPDATE,
DELETE), and testing the application.
Steps to be followed:
1. Creating the database and table:
Create a MySQL database named "university" and a table named "students". The table should
have the following fields:
id (type INT, auto-increment) - student ID
name (type VARCHAR) - student name
age (type INT) - student age
major (type VARCHAR) - student's major
4
Sample Code:
Below is a sample Java code for connecting to the MySQL database and executing queries:
import java.sql.*;
// Delete a student
String deleteQuery = "DELETE FROM students WHERE name = 'John'";
int rowsDeleted = statement.executeUpdate(deleteQuery);
if (rowsDeleted > 0) {
System.out.println("Student deleted successfully.");
}
5
} catch (SQLException e) {
e.printStackTrace();
}
}
}