How To Create a Banking Application in Java NetBeans
In this Java Tutorial we will see How to Create a simple banking app using Java Swing in Netbeans.
In this java bank project we will create custom panels, buttons and chart .
What We Are Gonna Use In This Project:
- Java Programming Language.- NetBeans Editor.
Project Source Code:
/**
* Method to create a panel with rounded corners
*/
private JPanel createRoundedPanel(Color bgColor, int cornerRadius){
JPanel panel = new JPanel(){
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Dimension arcs = new Dimension(cornerRadius, cornerRadius);
int width = getWidth();
int height = getHeight();
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(bgColor);
g2d.fillRoundRect(0, 0, width - 1, height - 1, arcs.width, arcs.height);
}
};
panel.setOpaque(false);
return panel;
}
/**
* Method to create a styled button with rounded corners
*/
private JButton createButton(String text, Color bgColor, Color fgColor){
JButton button = new JButton(text){
@Override
protected void paintComponent(Graphics g){
if(getModel().isArmed()){ g.setColor(bgColor.darker()); }
else{ g.setColor(bgColor); }
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.fill(new RoundRectangle2D.Double(0,0, getWidth() - 1, getHeight() - 1, 10, 10));
super.paintComponent(g);
}
};
button.setForeground(fgColor);
button.setBackground(bgColor);
button.setFont(new Font("Arial", Font.BOLD, 12));
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setContentAreaFilled(false);
return button;
}
/**
* Creates the transactions history panel
*/
// Method to update the transaction history panel
private void updateHistory(){
historyPanel.removeAll();
// Iterate through transactions and create a panel for each
for(Transaction transaction : transactions){
JPanel transactionPanel = createRoundedPanel(new Color(33,33,33), 5);
transactionPanel.setLayout(new BorderLayout());
transactionPanel.setBorder(new EmptyBorder(10, 15, 10, 15));
// Create and style the transaction type label
JLabel typeLabel = new JLabel(transaction.getType());
typeLabel.setForeground(transaction.getType().equals("deposit") ?
new Color(3,218,198) : new Color(207,102,121));
typeLabel.setFont(new Font("Arial", Font.BOLD, 12));
// Create and style the transaction amount label
JLabel amountLabel = new JLabel(String.format("$%.2f", transaction.getAmount()));
amountLabel.setForeground(Color.WHITE);
amountLabel.setFont(new Font("Arial", Font.PLAIN, 12));
// Create and style the transaction date label
JLabel dateLabel = new JLabel(new SimpleDateFormat("MMM dd, yyyy HH:mm").format(transaction.getDate()));
dateLabel.setForeground(new Color(150,150,150));
amountLabel.setFont(new Font("Arial", Font.PLAIN, 10));
// Create a panel for the left side (type and date)
JPanel leftPanel = new JPanel(new GridLayout(2, 1));
leftPanel.setOpaque(false);
leftPanel.add(typeLabel);
leftPanel.add(dateLabel);
// Create a panel for the right side (amount)
JPanel rightPanel = new JPanel(new BorderLayout());
rightPanel.setOpaque(false);
rightPanel.add(amountLabel, BorderLayout.EAST);
// Add left and right panels to the transaction panel
transactionPanel.add(leftPanel, BorderLayout.WEST);
transactionPanel.add(rightPanel, BorderLayout.EAST);
// Add the transaction panel to the history panel
historyPanel.add(transactionPanel);
historyPanel.add(Box.createRigidArea(new Dimension(0, 5)));
}
// Refresh the history panel
historyPanel.revalidate();
historyPanel.repaint();
}
// Method to customize the scroll bar appearance and behavior
private void customizeScrollBar(JScrollPane scrollpane){
// Set the scroll bar to appear only when needed
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
// Get the vertical scroll bar
JScrollBar verticalBar = scrollpane.getVerticalScrollBar();
// Set a custom UI for the scroll bar
verticalBar.setUI(new ModernScrollBarUI());
// Set the preferred width of the scroll bar
verticalBar.setPreferredSize(new Dimension(8, Integer.MAX_VALUE));
// Make the scroll bar transparent
verticalBar.setOpaque(false);
// Set the amount to scroll for each unit increment
verticalBar.setUnitIncrement(16);
// Adjust the z-order of components to ensure proper layering
scrollpane.setComponentZOrder(verticalBar, 0);
scrollpane.setComponentZOrder(scrollpane.getViewport(), 1);
scrollpane.getVerticalScrollBar().setOpaque(false);
}
// Custom ScrollBarUI class for a modern look
private class ModernScrollBarUI extends BasicScrollBarUI{
private final int THUMB_SIZE = 60;
private final Color THUMB_COLOR = new Color(3,218,198);
private final Color THUMB_HOVER_COLOR = new Color(4,245,233);
private final Color TRACK_COLOR = new Color(30,30,30);
private final int ARC = 10;
private boolean isDragging = false;
private boolean isRollover = false;
@Override
protected JButton createDecreaseButton(int orientation){
return new InvisibleButton();
}
@Override
protected JButton createIncreaseButton(int orientation){
return new InvisibleButton();
}
// Custom painting of the scroll bar track
@Override
protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds){
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(TRACK_COLOR);
g2d.fill(new RoundRectangle2D.Double(trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height, ARC, ARC));
g2d.dispose();
}
// Custom painting of the scroll bar thumb
@Override
protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds){
if(thumbBounds.isEmpty() || !scrollbar.isEnabled()){
return;
}
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Determine the color based on mouse interaction
Color thumbColor = isRollover || isDragging ? THUMB_HOVER_COLOR : THUMB_COLOR;
// Add a glow effect when the mouse is over the thumb or dragging
if(isRollover || isDragging){
for(int i = 0; i < 5; i++){
Color glowColor = new Color(thumbColor.getRed(),thumbColor.getGreen(),thumbColor.getBlue(), 50 - i * 10);
g2d.setColor(glowColor);
g2d.fill(new RoundRectangle2D.Double(thumbBounds.x - i, thumbBounds.y - i, thumbBounds.width + 20 * i, thumbBounds.height + 20 * i, 10 * ARC + i, 10 * ARC + i));
}
}
// Draw the main thumb
g2d.setColor(thumbColor);
g2d.fill(new RoundRectangle2D.Double(thumbBounds.x, thumbBounds.y, thumbBounds.width, thumbBounds.height, ARC, ARC));
// Add grip lines to the thumb
g2d.setColor(new Color(255,255,255, 100));
int lineY = thumbBounds.y + thumbBounds.height / 2 - 3;
for(int i = 0; i < 3; i++)
{
g2d.drawLine(thumbBounds.x + 3, lineY, thumbBounds.x + thumbBounds.width - 3, lineY);
lineY += 3;
}
g2d.dispose();
}
// Update thumb bounds and repaint
@Override
protected void setThumbBounds(int x, int y, int width, int height){
super.setThumbBounds(x, y, width, height);
scrollbar.repaint();
}
// Install custom listeners for mouse interactions
@Override
protected void installListeners(){
super.installListeners();
// Handle mouse enter/exit events
scrollbar.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e){
isRollover = true;
scrollbar.repaint();
}
@Override
public void mouseExited(MouseEvent e){
isRollover = false;
scrollbar.repaint();
}
});
// Handle mouse drag events
scrollbar.addMouseMotionListener(new MouseMotionAdapter(){
@Override
public void mouseDragged(MouseEvent e){
isDragging = true;
scrollbar.repaint();
}
});
// Handle mouse release events
scrollbar.addMouseListener(new MouseAdapter(){
@Override
public void mouseReleased(MouseEvent e){
isDragging = false;
scrollbar.repaint();
}
});
}
// Set preferred size of the scroll bar
@Override
public Dimension getPreferredSize(JComponent c){
return new Dimension(8, THUMB_SIZE);
}
}
// Custom JButton class for invisible buttons
private class InvisibleButton extends JButton{
private InvisibleButton(){
setOpaque(false);
setFocusable(false);
setFocusPainted(false);
setBorderPainted(false);
setContentAreaFilled(false);
}
}
------------------------------------------------------------------
/**
* Create chart to display the transactions
*/
// Draw a financial chart based on transaction data.
private void drawChart(){
// Get the dimensions of the chart panel
int width = chartPanel.getWidth();
int height = chartPanel.getHeight();
// Check if the panel has valid dimensions
if(width <= 0 || height <= 0){
// If not, schedule a repaint and exit
SwingUtilities.invokeLater(chartPanel::repaint);
return;
}
// Check if there are any transactions to display
if(transactions.isEmpty()){
// If not, draw an empty chart and exit
drawEmptyChart(width, height);
return;
}
// Set margins for the chart
int chartMargin = 40;
// Calculate the actual chart area dimensions
int chartWidth = width - 2 * chartMargin;
int chartHeight = height - 2 * chartMargin;
// Create a new image for the chart
BufferedImage chartImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
// Get the graphics context for drawing
Graphics2D g2d = chartImage.createGraphics();
// Enable anti-aliasing for smoother lines
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Set the background color (dark grey)
g2d.setColor(new Color(25, 25, 25));
// Fill the entire image with the background color
g2d.fillRect(0, 0, width, height);
// Initialize variables to track balance range
double minBalance = 0;
double maxBalance = 0;
double runningBalance = 0;
// Iterate through transactions to find min and max balance
for(Transaction transaction : transactions){
// Update running balance based on transaction type
runningBalance += transaction.getType().equals("deposit") ?
transaction.getAmount() : -transaction.getAmount();
// Update min and max balance
minBalance = Math.min(minBalance, runningBalance);
maxBalance = Math.max(maxBalance, runningBalance);
}
// Calculate the range of balances
double balanceRange = Math.max(maxBalance - minBalance, 1);
// Ensure the chart shows some range, even with one transaction
if(balanceRange < 100){
maxBalance = Math.max(maxBalance, maxBalance + 100);
balanceRange = maxBalance - minBalance;
}
// Set color and font for axis labels and grid lines
g2d.setColor(new Color(100,100,100));
g2d.setFont(new Font("Arial", Font.PLAIN, 10));
// Number of Y-axis labels
int yLabelCount = 5;
// Draw Y-axis labels and grid lines
for(int i = 0; i <= yLabelCount; i++){
// Calculate balance for this label
double balance = minBalance + (balanceRange * i / yLabelCount);
// Calculate Y-coordinate for this label
int y = (height - chartMargin - (int)((balance - minBalance) / balanceRange * chartHeight));
// Format balance label
String label = String.format("$%.0f", balance);
// Get font metrics for text positioning
FontMetrics fm = g2d.getFontMetrics();
// Draw balance label
g2d.drawString(label, chartMargin - fm.stringWidth(label) - 5, y + fm.getAscent() / 2);
// Set color for grid lines
g2d.setColor(new Color(40,40,40));
// Set dashed stroke for grid lines
g2d.setStroke(new BasicStroke(1,BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{5},0));
// Draw horizontal grid line
g2d.drawLine(chartMargin, y, width - chartMargin, y);
// Reset color for next iteration
g2d.setColor(new Color(100, 100, 100));
}
// List to store points for the chart line
List<Point2D> points = new ArrayList<>();
runningBalance = 0;
// Calculate points for each transaction
for(int i = 0; i < transactions.size(); i++){
Transaction transaction = transactions.get(i);
// Update running balance
runningBalance += transaction.getType().equals("deposit") ? transaction.getAmount() : -transaction.getAmount();
// Calculate X-coordinate
double x = chartMargin + (i * chartWidth / Math.max(transactions.size() - 1, 1));
// Calculate Y-coordinate
double y = height - chartMargin - ((runningBalance - minBalance) / balanceRange) * chartHeight;
// Add point to list
points.add(new Point2D.Double(x, y));
}
// Check if there's more than one point to draw a line
if(points.size() > 1){
// Create a path for the smooth curve
Path2D curvePath = new Path2D.Double();
// Move to the first point
curvePath.moveTo(points.get(0).getX(), points.get(0).getY());
// Create smooth curve through all points
for(int i = 1; i < points.size(); i++){
// Get current and adjacent points
Point2D p0 = points.get(Math.max(i - 1, 0));
Point2D p1 = points.get(i);
Point2D p2 = points.get(Math.min(i + 1, points.size() - 1));
// Calculate control points for the curve
Point2D ctrl1 = new Point2D.Double((p0.getX() + p1.getX()) / 2, p0.getY());
Point2D ctrl2 = new Point2D.Double((p1.getX() + p2.getX()) / 2, p1.getY());
// Add curve segment to path
curvePath.curveTo(ctrl1.getX(), ctrl1.getY(), ctrl2.getX(), ctrl2.getY(), p1.getX(), p1.getY());
}
// Create a gradient for the chart area
GradientPaint gradient = new GradientPaint(
0, chartMargin, new Color(3, 218, 198, 100),
0, height - chartMargin, new Color(3, 218, 198, 10)
);
// Create a path for the filled area under the curve
Path2D areaPath = new Path2D.Double(curvePath);
areaPath.lineTo(width - chartMargin, height - chartMargin);
areaPath.lineTo(chartMargin, height - chartMargin);
areaPath.closePath();
// Fill the area under the curve with the gradient
g2d.setPaint(gradient);
g2d.fill(areaPath);
// Set color and stroke for the curve line
g2d.setColor(new Color(3, 218, 198));
g2d.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
// Draw the smooth curve
g2d.draw(curvePath);
}
else if(points.size() == 1){
// For a single point, draw a small line segment
Point2D point = points.get(0);
g2d.setColor(new Color(3, 218, 198));
g2d.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.draw(new Line2D.Double(chartMargin, point.getY(), point.getX(), point.getY()));
}
// Draw data points with a glow effect
for(Point2D point : points){
// Create a radial gradient for the glow effect
RadialGradientPaint glowGradient = new RadialGradientPaint(
(float)point.getX(), (float)point.getY(), 10f,
new float[]{0f, 1f},
new Color[]{new Color(3, 218, 198, 100),new Color(3, 218, 198, 0)}
);
// Apply the glow gradient
g2d.setPaint(glowGradient);
// Draw the glow
g2d.fillOval((int)point.getX() - 10, (int)point.getY() - 10, 20, 20);
// Draw the main point
g2d.setColor(new Color(3, 218, 198));
g2d.fillOval((int)point.getX() - 4, (int)point.getY() - 4, 8, 8);
// Draw a white center for the point
g2d.setColor(Color.WHITE);
g2d.fillOval((int)point.getX() - 2, (int)point.getY() - 2, 4, 4);
}
// Draw X-axis labels
g2d.setColor(new Color(100, 100, 100));
g2d.setFont(new Font("Arial", Font.PLAIN, 10));
// Determine number of X-axis labels (max 5)
int xLabelCount = Math.min(5, transactions.size());
for(int i = 0; i < xLabelCount; i++){
// Calculate index of transaction for this label
int transactionIndex = i * (transactions.size() - 1) / Math.max(xLabelCount - 1, 1);
// Calculate X-coordinate for label
int x = chartMargin + (int) (transactionIndex * chartWidth / Math.max(transactions.size() - 1, 1));
// Create label text
String label = String.valueOf(transactionIndex + 1);
FontMetrics fm = g2d.getFontMetrics();
// Draw the label
g2d.drawString(label, x - fm.stringWidth(label) / 2, height - chartMargin + fm.getHeight());
}
// Dispose of the graphics context
g2d.dispose();
// Create a new panel to display the chart image and handle interactions
JPanel interactiveChartPanel = new JPanel(){
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
// Draw the chart image on the panel
g.drawImage(chartImage, 0, 0, null);
}
};
// Set the preferred size of the panel
interactiveChartPanel.setPreferredSize(new Dimension(width, height));
// Add mouse motion listener for interactive tooltips
interactiveChartPanel.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e){
// Check if mouse is near any data point
for(int i = 0; i < points.size(); i++){
Point2D point = points.get(i);
if(point.distance(e.getX(), e.getY()) < 10)
{
// Get transaction data for this point
Transaction transaction = transactions.get(i);
// Create tooltip HTML
String tooltip = String.format("<html><body style='background-color:#1e1e1e; color:#e0e0e0; padding:30px; font-family:Arial;'>"
+ "<div><h3 style='margin-bottom:25px; color:#00ffc6; font-size:1.8em;'"
+ "border-bottom:3px solid #00ffc6; padding-bottom:15px;'> Transaction %d </h3>"
+"<table style='width:100%%; font-size:1em; line-height:1.8;'>"
+"<tr><td style='font-weight:bold; color:#a0a0a0;'>Type: </td>"
+"<td style='text-align:right; color:#ffffff;'>%s</td></tr>"
+"<tr><td style='font-weight:bold; color:#a0a0a0;'>Amount: </td>"
+"<td style='text-align:right; color:#ffffff;'>$%.2f</td></tr>"
+"<tr><td style='font-weight:bold; color:#a0a0a0;'>Date: </td>"
+"<td style='text-align:right; color:#ffffff;'>%s</td></tr>"
+"</table>"
+ "</body></html>",
i + 1,
transaction.getType(),
transaction.getAmount(),
new SimpleDateFormat("MMM dd, yyyy HH:mm").format(transaction.getDate()));
// Set tooltip text
interactiveChartPanel.setToolTipText(tooltip);
return;
}
}
// Clear tooltip if not near any point
interactiveChartPanel.setToolTipText(null);
}
});
// Clear existing content from chart panel
chartPanel.removeAll();
// Add the interactive chart panel
chartPanel.add(interactiveChartPanel);
// Revalidate and repaint the chart panel
chartPanel.revalidate();
chartPanel.repaint();
}
// Method to draw an empty chart when there are no transactions
private void drawEmptyChart(int width, int height){
if(width <= 0 || height <= 0){
SwingUtilities.invokeLater(chartPanel::repaint);
return;
}
// Create a buffered image for the empty chart
BufferedImage chartImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = chartImage.createGraphics();
// Set up the graphics context and draw the background
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(new Color(25,25,25));
g2d.fillRect(0, 0, width, height);
// Draw the "No transactions" message
g2d.setColor(new Color(200,200,200));
g2d.setFont(new Font("Arial", Font.PLAIN, 14));
String message = "No transactions to display";
FontMetrics fm = g2d.getFontMetrics();
g2d.drawString(message, (width - fm.stringWidth(message)) / 2, height / 2);
g2d.dispose();
// Update the chart panel with the empty chart image
JLabel emptyChartLabel = new JLabel(new ImageIcon(chartImage));
chartPanel.removeAll();
chartPanel.add(emptyChartLabel);
chartPanel.revalidate();
chartPanel.repaint();
}
-------------------------------------------------------------------------------
/**
* Create custom error dialog box
*/
// This class creates a custom error dialog box, extending JDialog
public static class ErrorDialog extends JDialog{
// Constructor that takes a parent window and an error message
public ErrorDialog(JFrame parent, String message){
// Call parent constructor with title "Error" and modal=true (blocks input to other windows)
super(parent, "Error", true);
// Remove the default window decorations (title bar, borders) for a custom look
setUndecorated(true);
// Create the main panel with GridBagLayout for centered content
JPanel panel = new JPanel(new GridBagLayout()) {
@Override
protected void paintComponent(Graphics g){
// Call the parent's painting method first
super.paintComponent(g);
// Create a new Graphics2D object for better drawing capabilities
Graphics2D g2d = (Graphics2D) g.create();
// Enable anti-aliasing for smoother edges
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Set the background color to light gray
g2d.setColor(new Color(240,240,240));
// Draw a rounded rectangle that fills the entire panel
g2d.fill(new RoundRectangle2D.Float(0,0,getWidth(),getHeight(),15,15));
// Clean up the graphics object
g2d.dispose();
}
};
// Make the panel transparent to show the custom background
panel.setOpaque(false);
// Create a panel for the content using BoxLayout for vertical arrangement
JPanel contentPanel = new JPanel();
contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
contentPanel.setOpaque(false);
// Add the standard error icon from the system's UI manager
JLabel iconLabel = new JLabel(UIManager.getIcon("OptionPane.errorIcon"));
iconLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
contentPanel.add(iconLabel);
// Add vertical spacing after the icon
contentPanel.add(Box.createRigidArea(new Dimension(0, 20)));
// Create and configure the message label with HTML formatting
JLabel errorMessageLabel = new JLabel("<html><div style='text-align:center;width:250px; font-size:14px;'>" + message +"</div></html>");
errorMessageLabel.setForeground(new Color(33,33,33));
errorMessageLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
contentPanel.add(errorMessageLabel);
// Add vertical spacing after the message
contentPanel.add(Box.createRigidArea(new Dimension(0, 25)));
// Create a custom OK button
JButton okButton = new JButton("Ok"){
{
// Initialize button properties to remove default button styling
setOpaque(false);
setContentAreaFilled(false);
setBorderPainted(false);
setFocusPainted(false);
}
// Override paintComponent to create custom button appearance
@Override
protected void paintComponent(Graphics g){
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Change button color based on its state (pressed, hover, or normal)
if(getModel().isPressed()){
g2d.setColor(new Color(0,150, 136));
}
else if(getModel().isRollover()){
g2d.setColor(new Color(0,170, 156));
}
else{ g2d.setColor(new Color(0,191, 165)); }
// Draw the rounded rectangle button shape
g2d.fill(new RoundRectangle2D.Float(0,0,getWidth(), getHeight(), 10, 10));
super.paintComponent(g);
g2d.dispose();
}
};
// Configure the OK button's appearance
okButton.setFont(new Font("Arial", Font.BOLD, 14));
okButton.setForeground(Color.WHITE);
okButton.setPreferredSize(new Dimension(100, 35));
okButton.setMaximumSize(new Dimension(100, 35));
okButton.setAlignmentX(Component.CENTER_ALIGNMENT);
// Add action listener to close the dialog when clicked
okButton.addActionListener(e -> dispose());
contentPanel.add(okButton);
// Add the content panel to the main panel
panel.add(contentPanel, new GridBagConstraints());
// Set the main panel as the dialog's content pane
setContentPane(panel);
// Set the dialog size
setSize(400,200);
// Center the dialog relative to its parent window
setLocationRelativeTo(parent);
// Make the dialog window have rounded corners
setShape(new RoundRectangle2D.Float(0, 0, 400, 200, 15, 15));
}
// Method to show error messages
public static void showErrorMessage(JFrame parent, String message){
new ErrorDialog(parent, message).setVisible(true);
}
}
------------------------------------------------------------------------------------
/**
* Create necessary classes
*/
// Inner class to represent a transaction
private static class Transaction{
private final String type;
private final double amount;
private final Date date;
public Transaction(String type, double amount){
this.type = type;
this.amount = amount;
this.date = new Date();
}
public String getType(){ return type; }
public double getAmount(){ return amount; }
public Date getDate(){ return date; }
}
// Inner class to handle deposit and withdraw actions
private class TransactionHandler implements ActionListener{
private final String type;
public TransactionHandler( String type ){ this.type = type; }
@Override
public void actionPerformed(ActionEvent e) {
try {
// Parse the amount from the input field
double amount = Double.parseDouble(amountField.getText());
// Validate the amount
if(amount <= 0){
showErrorMessage("Please enter a valid positive number");
return;
}
if(type.equals("withdraw") && amount > balance){
showErrorMessage("Insufficent funds");
return;
}
// Update the balance and add the transaction
balance += type.equals("deposit") ? amount : -amount;
transactions.add(new Transaction(type, amount));
balanceLabel.setText(String.format("$%.2f", balance));
drawChart();
updateHistory();
amountField.setText("");
} catch (NumberFormatException ex) {
showErrorMessage("Please enter a valid number");
}
}
}
if you want the source code click on the download button below
disclaimer: you will get the source code with the database script and to make it work in your machine is your responsibility and to debug any error/exception is your responsibility this project is for the students who want to see an example and read the code not to get and run.
More Java Projects:
Download Projects Source Code