ADV.JAVA micro
ADV.JAVA micro
MySql Database.
User Interface: The program features a graphical user interface (GUI) created using
Swing components, including text fields, buttons, tables, and labels for displaying
income and expense data.
Tabbed Layout: The GUI is organized into a tabbed pane with three tabs: Income,
Expense, and Comparison. This structure allows users to navigate easily between
different functionalities.
Adding Income: Users can input income details (description, amount, and date) and
click the "Add Income" button to store the data in the database. The income is
categorized as "Income" in the database.
Adding Expense: Similar to adding income, users can input expense details and click the
"Add Expense" button to save the data. The expense is categorized as "Expense" in the
database.
Data Validation: The program validates user inputs for amount fields to ensure they are
numeric. If an invalid input is detected, an error message is displayed using a dialog box.
Displaying Records: The income and expense records are displayed in separate tables
within their respective tabs, allowing users to view all entries at a glance.
Deleting Records: Users can select a row from the income or expense table and click the
"Delete" button to remove the selected record from the database and the table.
Clearing Input Fields: The program provides options to clear input fields after adding or
deleting records, enhancing usability and preventing accidental re-entry.
Resetting All Data: A "Reset All" button allows users to delete all records from the
database, giving them a fresh start if desired.
Calculating Totals: The program calculates and displays total income, total expenses,
and the balance (income - expenses) at the bottom of the window, updating these values
automatically whenever records are added or deleted.
Comparison Progress Bar: The program features a progress bar in the Comparison tab,
visually representing the percentage of expenses relative to total income, helping users
understand their financial situation at a glance.
Database Interaction: The program uses prepared statements to interact with the
database securely, preventing SQL injection attacks and ensuring data integrity.
• About the Swing Components which are used in project : -
1. JFrame:
o Description: Represents the main window of the application. It serves as the
container for all other GUI components and defines the overall layout and
behavior of the application.
o Functionality: It can be customized with titles, dimensions, and default close
operations.
2. JTabbedPane:
o Description: A component that allows for tabbed navigation between multiple
panels or views within the same window.
o Functionality: Users can switch between different sections (like Income, Expense,
Comparison) by clicking on the tabs, making the interface more organized and
user-friendly.
3. JPanel:
o Description: A generic container used to group other components. It can have
different layouts to arrange its child components.
o Functionality: Helps in organizing the user interface into logical sections,
enhancing layout management.
4. JTextField:
o Description: A single-line text input field where users can enter data.
o Functionality: Useful for collecting user inputs like descriptions, amounts, and
dates for income or expenses.
5. JLabel:
o Description: Displays a short string or an image icon. It's generally used for text
annotations.
o Functionality: Provides descriptive text next to input fields or buttons, enhancing
user understanding of the UI.
6. JButton:
o Description: A push-button that triggers an action when clicked.
o Functionality: Commonly used for performing actions like adding, deleting, or
saving entries in the application.
7. JTable:
o Description: Displays data in a tabular format, organizing it into rows and
columns.
o Functionality: Provides a clear view of income and expense records, making it
easier to read and manipulate data.
8. JScrollPane:
o Description: A container that adds scroll bars to another component, allowing for
navigation through large amounts of content.
o Functionality: Used with JTable to ensure that if there are many records, users can
scroll to see all entries.
9. JProgressBar:
o Description: Visually indicates the progress of a task or process.
o Functionality: Used to show a comparison between income and expenses, giving
a visual cue of financial status.
10. GridLayout:
o Description: A layout manager that arranges components in a grid format, defined
by rows and columns.
o Functionality: Useful for organizing input fields and buttons systematically within
the application.
11. BorderLayout:
o Description: A layout manager that organizes components in five regions: north,
south, east, west, and center.
o Functionality: Helps in structuring the main window effectively, allowing for a
flexible arrangement of components.
12. JOptionPane:
o Description: A standard dialog box for displaying messages, warnings, or asking
for user input.
o Functionality: Used for notifications, confirmations, or alerts to the user regarding
actions performed in the application.
13. ActionListener:
o Description: An interface for receiving action events (like button clicks).
o Functionality: Allows defining specific actions that should occur when a user
interacts with components like buttons.
14. DefaultTableModel:
o Description: A data model for JTable that allows adding, removing, and
managing rows and columns of data.
o Functionality: Facilitates easy manipulation of the table's data, supporting
dynamic updates as users add or remove records.
15. GridBagLayout:
o Description: A flexible layout manager that allows for complex arrangements of
components using a grid of cells.
o Functionality: Provides greater control over component positioning, sizes, and
alignment, useful for creating sophisticated UIs.
Program :-
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public ExpenseTrackerGUI() {
setTitle("Expense Tracker");
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
getContentPane().setBackground(new Color(230, 230, 250)); // Light lavender
background
incomeInputPanel.add(new JLabel("Amount:"));
incomeAmountField = new JTextField();
incomeInputPanel.add(incomeAmountField);
// Income table
incomeTableModel = new DefaultTableModel(new String[]{"ID", "Description",
"Amount", "Date"}, 0);
incomeTable = new JTable(incomeTableModel);
incomeTable.setFillsViewportHeight(true);
incomeTable.setBackground(new Color(240, 248, 255)); // Alice blue
incomeTable.setForeground(new Color(0, 0, 128)); // Navy blue
JScrollPane incomeScrollPane = new JScrollPane(incomeTable);
incomePanel.add(incomeScrollPane, BorderLayout.CENTER);
tabbedPane.addTab("Income", incomePanel);
// Expense panel
JPanel expensePanel = new JPanel();
expensePanel.setLayout(new BorderLayout());
expensePanel.setBackground(new Color(255, 255, 255)); // White background for
expense panel
expenseInputPanel.add(new JLabel("Description:"));
expenseDescriptionField = new JTextField();
expenseInputPanel.add(expenseDescriptionField);
expenseInputPanel.add(new JLabel("Amount:"));
expenseAmountField = new JTextField();
expenseInputPanel.add(expenseAmountField);
expensePanel.add(expenseInputPanel, BorderLayout.NORTH);
// Expense table
expenseTableModel = new DefaultTableModel(new String[]{"ID", "Description",
"Amount", "Date"}, 0);
expenseTable = new JTable(expenseTableModel);
expenseTable.setFillsViewportHeight(true);
expenseTable.setBackground(new Color(240, 248, 255)); // Alice blue
expenseTable.setForeground(new Color(0, 0, 128)); // Navy blue
JScrollPane expenseScrollPane = new JScrollPane(expenseTable);
expensePanel.add(expenseScrollPane, BorderLayout.CENTER);
tabbedPane.addTab("Expense", expensePanel);
// Comparison tab
JPanel comparisonPanel = new JPanel(new BorderLayout());
comparisonPanel.setBackground(new Color(255, 255, 255)); // White background
tabbedPane.addTab("Comparison", comparisonPanel);
// Total labels
JPanel totalPanel = new JPanel(new GridLayout(4, 1));
totalPanel.setBackground(new Color(255, 255, 255)); // White background
add(tabbedPane, BorderLayout.CENTER);
add(totalPanel, BorderLayout.SOUTH);
setVisible(true);
}
loadRecords();
clearIncomeFields();
}
}
pstmt.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(ExpenseTrackerGUI.this, "Error adding
expense!", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
loadRecords();
clearExpenseFields();
}
}
loadRecords();
}
}
}
loadRecords();
}
}
}
if ("Income".equals(type)) {
incomeTableModel.addRow(new Object[]{id, description, amount, date});
totalIncome += amount;
} else {
expenseTableModel.addRow(new Object[]{id, description, amount, date});
totalExpense += amount;
}
}
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error loading records!", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
updateComparisonBar(totalIncome, totalExpense);
}
{
ex.printStackTrace();
JOptionPane.showMessageDialog(ExpenseTrackerGUI.this, "Error resetting
data!", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
loadRecords();
}
}
}
Output :-
Conclusion :-
The Expense Tracker application is a comprehensive tool that utilizes various
Swing components to provide a user-friendly interface for managing income and
expense.