Add Expense

addExpenseFrame = new JFrame();
Utils.centeredFrame(addExpenseFrame, windowWidth, windowHeight, "Add Expense");
addExpenseFrame.setResizable(false);
Container frameContainer = addExpenseFrame.getContentPane();
frameContainer.setBackground(Color.gray);
// Changeable According to What size of screen You Want?
int containerWidth = windowWidth*94/100;
int containerHeight = windowHeight*90/100;
int panelWidth = containerWidth*98/100;
int panelHeight = containerHeight*96/100;
int posX = windowWidth / 2 - panelWidth / 2 ;
int posY = windowHeight / 2 - panelHeight / 2 - frameControllerSize / 2;
// frame without control options
expensePanel = new JPanel();
expensePanel.setLayout(null);//new BorderLayout());
//panel.setOpaque(false);
expensePanel.setBackground(Color.decode("#FFFFFF"));
expensePanel.setSize(new Dimension(panelWidth,panelHeight));
expensePanel.setLocation(posX, posY);
frameContainer.add(expensePanel);
// for Rounded Background
posX = windowWidth / 2 - containerWidth / 2 ;
posY = windowHeight / 2 - containerHeight / 2 - frameControllerSize / 2;
JComponent jcomponent = new RoundedBackground(posX, posY, containerWidth, containerHeight);
jcomponent.setLayout(null);
frameContainer.add(jcomponent, BorderLayout.CENTER);
Here we create a Label for our heading.
// for heading Layout
posY = panelHeight * 2/100;
Font headingFont = new Font("Serif", Font.PLAIN, 22);
// header
//borderline = BorderFactory.createLineBorder(Color.black);
JLabel heading_text = new JLabel("Add Expense");
heading_text.setHorizontalAlignment(JLabel.CENTER);
heading_text.setVerticalAlignment(JLabel.CENTER);
heading_text.setBackground(Color.decode("#80c2b2"));
heading_text.setForeground(Color.black);
heading_text.setFont(headingFont);
heading_text.setBounds(0,0, panelWidth, panelHeight / 15);
expensePanel.add(heading_text);
expensePanel.add(Utils.getSeparator(0, panelHeight / 13, panelWidth, 5,"000000"));
Here we create Action Panel and its UI.
// Action table Base panel
Font textFont = new Font("Serif",1,12);
Border borderline = BorderFactory.createLineBorder(Color.black);
int actionPanelWidth = panelWidth*35/100;
int actionPanelHeight = panelHeight*86/100;
actionPanel = new JPanel();
actionPanel.setBorder(Utils.getTitledBorder(10, "Actions"));
//controlPanel.add(new MyCanvas());
actionPanel.setBounds(0,panelHeight*10/100, actionPanelWidth, actionPanelHeight);
actionPanel.setLayout(null);
expensePanel.add(actionPanel);
textFont = new Font("Serif",0,12);
JLabel amountLabel = new JLabel("Amount");
amountLabel.setAlignmentX(amountLabel.LEFT_ALIGNMENT);
amountLabel.setAlignmentY(amountLabel.LEFT_ALIGNMENT);
amountLabel.setForeground(Color.black);
amountLabel.setFont(textFont);
amountLabel.setBounds(actionPanelWidth*15/100,actionPanelHeight*10/100, actionPanelWidth*70/100, actionPanelHeight*7/100);
amountLabel.setBackground(Color.black);
actionPanel.add(amountLabel);
amountField = new JTextField();
amountField.setForeground(Color.black);
amountField.setBounds(actionPanelWidth*15/100,actionPanelHeight*12/100, actionPanelWidth*70/100, actionPanelHeight*10/100);
amountField.setOpaque(false);
amountField.setBorder(BorderFactory.createLineBorder(Color.white, 0));
amountField.setBackground(null);
actionPanel.add(amountField);
actionPanel.add(Utils.getSeparator(actionPanelWidth*15/100,actionPanelHeight*20/100,actionPanelWidth*70/100,5,"000000"));
JLabel categoryLabel = new JLabel("Category");
categoryLabel.setAlignmentX(categoryLabel.LEFT_ALIGNMENT);
categoryLabel.setAlignmentY(categoryLabel.LEFT_ALIGNMENT);
categoryLabel.setForeground(Color.black);
categoryLabel.setFont(textFont);
categoryLabel.setBounds(actionPanelWidth*15/100,actionPanelHeight*23/100, actionPanelWidth*70/100, actionPanelHeight*7/100);
categoryLabel.setBackground(Color.black);
actionPanel.add(categoryLabel);
String country[]={"Select","Food", "Health Care","Insurance","Travel","Others"};
choice=new JComboBox(country);
choice.setBounds(actionPanelWidth*15/100, actionPanelHeight*32/100,actionPanelWidth*70/100,actionPanelHeight*7/100);
actionPanel.add(choice);
JLabel descriptionLabel = new JLabel("Description");
descriptionLabel.setAlignmentX(descriptionLabel.LEFT_ALIGNMENT);
descriptionLabel.setAlignmentY(descriptionLabel.LEFT_ALIGNMENT);
descriptionLabel.setForeground(Color.black);
descriptionLabel.setFont(textFont);
descriptionLabel.setBounds(actionPanelWidth*15/100,actionPanelHeight*42/100, actionPanelWidth*70/100, actionPanelHeight*7/100);
descriptionLabel.setBackground(Color.black);
actionPanel.add(descriptionLabel);
descriptionArea = new JTextArea();
descriptionArea.setBounds(actionPanelWidth*15/100,actionPanelHeight*50/100, actionPanelWidth*70/100,actionPanelHeight*30/100);
actionPanel.add(descriptionArea);
addExpenseBtn(actionPanelWidth, actionPanelHeight);
Here we Define "ADD EXPENSE" Button and its click.
private void addExpenseBtn(int width, int height) {
addExpenseBtn = new JButton(new ImageIcon(((new ImageIcon("images/button38.png")).getImage()).getScaledInstance(width*60/100, height*10/100, java.awt.Image.SCALE_SMOOTH)));
addExpenseBtn.setContentAreaFilled(false);
addExpenseBtn.setBorder(null);
addExpenseBtn.setText("ADD EXPENSE");
addExpenseBtn.setHorizontalTextPosition(JButton.CENTER);
addExpenseBtn.setVerticalTextPosition(JButton.CENTER);
addExpenseBtn.setBounds(width*20/100,height*85/100, width*60/100, height*10/100);
addExpenseBtn.setOpaque(false);
addExpenseBtn.setContentAreaFilled(false);
addExpenseBtn.setBorderPainted(false);
addExpenseBtn.setFont(new java.awt.Font("Serif",1, 15));
addExpenseBtn.setForeground(Color.white);
actionPanel.add(addExpenseBtn);
addExpenseBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String var = "";
String amount = (amountField.getText());
String category = choice.getSelectedItem().toString();
String description = descriptionArea.getText();
//LocalDate credit_date = java.time.LocalDate.now();
String debit_date = java.time.LocalDate.now()+"";
String user_id = AppConstant.USER_ID;
int balance = 0;
int old_credit = 0;
int old_debit = 0;
boolean is_amount_entered = false;
boolean is_descrip_entered = false;
boolean is_category_entered = false;
if(amount.isEmpty()) {
var += "Amount Should not be empty, ";
}else {
is_amount_entered = true;
}
if(category.equals("Select")) {
var += "Category Should not be empty, ";
}else {
is_category_entered = true;
}
if(description.isEmpty()) {
var += "Description Should not be empty, ";
}else {
is_descrip_entered = true;
}
if(is_amount_entered && is_descrip_entered && is_category_entered) {
conn = SqliteConnection.ConnectDb();
try {
insert = conn.prepareStatement("SELECT credit FROM statement where user_id = ?");
insert.setString(1, user_id);
ResultSet rs = insert.executeQuery();
while (rs.next()) {
int value = rs.getInt(1);
old_credit += value;
}
insert = conn.prepareStatement("SELECT debit FROM statement where user_id = ?");
insert.setString(1, user_id);
ResultSet rs1 = insert.executeQuery();
while (rs1.next()) {
int value = rs1.getInt(1);
old_debit += value;
}
int entered_amount = Integer.parseInt(amount);
balance = (old_credit - old_debit) + (entered_amount);
int old_balance = (old_credit - old_debit);
if(old_balance >= entered_amount) {
balance = old_balance - entered_amount;
String credit = "";
String credit_date = "";
insert = conn.prepareStatement("insert into statement(user_id, credit, debit, balance, credit_date, debit_date, description, category)values(?,?,?,?,?,?,?,?)");
insert.setString(1, user_id);
insert.setString(2, amount);
insert.setInt(3, entered_amount);
insert.setInt(4, balance);
insert.setString(5, credit_date);
insert.setString(6, debit_date);
insert.setString(7, description);
insert.setString(8, category);
insert.executeUpdate();
conn.close();
amountField.setText("");
descriptionArea.setText("");
choice.setSelectedIndex(0);
data_table_update();
}else {
JOptionPane.showMessageDialog(null, "Insufficient Balance.");
}
conn.close();
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
var = "Success credited successfully";
JOptionPane.showMessageDialog(null, var);
}else {
JOptionPane.showMessageDialog(null, var);
}
}
});
}
Here we create History Panel and its UI.
// History Table Base panel
int historyPanelWidth = panelWidth*64/100;
int historyPanelHeight = panelHeight*86/100;
historyPanel = new JPanel();
historyPanel.setLayout(new FlowLayout());
historyPanel.setLayout(null);
historyPanel.setBounds(panelWidth*36/100,panelHeight*10/100, historyPanelWidth, historyPanelHeight);
historyPanel.setBorder(Utils.getTitledBorder(10, "History"));
expensePanel.add(historyPanel);
headingFont = new Font("Serif",Font.BOLD,16);
JLabel user_name_L = new JLabel("Name: "+ AppConstant.USER_NAME);
user_name_L.setAlignmentX(JLabel.LEFT);
//searchLabel.setBackground(Color.decode("#80c2b2"));
user_name_L.setForeground(Color.black);
user_name_L.setFont(headingFont);
user_name_L.setBounds(historyPanelWidth*2/100,historyPanelHeight*2/100, historyPanelWidth*40/100, historyPanelHeight*10/100);
historyPanel.add(user_name_L);
data_table(historyPanelWidth, historyPanelHeight);
addExpenseFrame.setVisible(true);
Here we define Data Table and its data updation.
private void data_table(int width, int height) {
// TODO Auto-generated method stub
String[] columnNames = {"Debit",
"Debit Date",
"Balance",
"Category",
"Description"
};
DefaultTableModel model1 = new DefaultTableModel();
sorter2 = new TableRowSorter<>(model1);
model1.setColumnIdentifiers(columnNames);
dataTable = new JTable(){
public boolean isCellEditable(int row, int column) {
return false;
};
};
dataTable.setBounds(width*2/100,height*15/100, width*96/100,height*80/100);
dataTable.setRowSorter(sorter2);
//table.setEnabled(true);
historyPanel.add(dataTable);
dataTable.setModel(model1);
JScrollPane scrollPane1 = new JScrollPane(dataTable);
scrollPane1.setBounds(width*2/100,height*15/100, width*96/100,height*80/100);
dataTable.setFillsViewportHeight(true);
historyPanel.add(scrollPane1);
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment( JLabel.CENTER );
dataTable.getColumnModel().getColumn(0).setCellRenderer( centerRenderer );
dataTable.getColumnModel().getColumn(1).setCellRenderer( centerRenderer );
dataTable.getColumnModel().getColumn(2).setCellRenderer( centerRenderer );
dataTable.getColumnModel().getColumn(3).setCellRenderer( centerRenderer );
dataTable.getColumnModel().getColumn(4).setCellRenderer( centerRenderer );
dataTable.getTableHeader().setFont(new Font("SansSerif", 1, 15));
dataTable.getTableHeader().setBackground(Color.decode("#980AF2"));
dataTable.getTableHeader().setForeground(Color.decode("#FFFFFF"));
dataTable.getTableHeader().setPreferredSize(new Dimension(760, 35));
dataTable.setRowHeight(25);
dataTable.getColumnModel().getColumn(0).setPreferredWidth(30);
dataTable.getColumnModel().getColumn(3).setPreferredWidth(100);
dataTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent event) {
}
});
data_table_update();
}
//Data of Table updation
private void data_table_update() {
conn = SqliteConnection.ConnectDb();
try {
insert = conn.prepareStatement("SELECT debit, debit_date, balance, category, description FROM statement WHERE user_id = ?");
insert.setString(1, AppConstant.USER_ID);
ResultSet rs = insert.executeQuery();
DefaultTableModel dft = (DefaultTableModel)dataTable.getModel();
dft.setRowCount(0);
while(rs.next()) {
Vector v2 = new Vector();
v2.add(rs.getString("debit"));
v2.add(rs.getString("debit_date"));
v2.add(rs.getString("balance"));
v2.add(rs.getString("category"));
v2.add(rs.getString("description"));
dft.addRow(v2);
}
conn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
At last we will manage closing of window
addExpenseFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
addExpenseFrame.dispose();
dashboardFrame.setVisible(true);
DashboardNew.get_account_table_data();
DashboardNew.get_food_data();
DashboardNew.get_health_data();
DashboardNew.get_insurance_data();
DashboardNew.get_travel_data();
DashboardNew.get_other_data();
if (null != DashboardNew.chartPanel && null != DashboardNew.chartPanel1 && null != DashboardNew.chartPanel2) {
DashboardNew.panel.remove(DashboardNew.chartPanel);
DashboardNew.panel.remove(DashboardNew.chartPanel1);
DashboardNew.panel.remove(DashboardNew.chartPanel2);
PieSectionLabelGenerator labelGenerator = new StandardPieSectionLabelGenerator(
" {0} : {2}", new DecimalFormat("0"), new DecimalFormat("0%"));
DashboardNew.getFirstPiechart(panelWidth, panelHeight, DashboardNew.panel, labelGenerator);
DashboardNew.getSecondPiechart(panelWidth, panelHeight, DashboardNew.panel, labelGenerator);
DashboardNew.getThirdPiechart(panelWidth, panelHeight, DashboardNew.panel, labelGenerator);
}
}
});
Programmer Mirta is for learning and training. Projects might be simple to improve learning. Projects are constantly reviewed to avoid errors, but we cannot assure full correctness of all content. While using Programmer Mitra, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2021 by Programmer Mitra. All Rights Reserved.