I am programming a GUI (Bank Account). I have done the necessary frames, but facing problems in adding information in the JTextArea. I have tried many different ways but still unsuccessful.
When I select a radio button, for example "Savings or Current", the other panels will be visible. And there in the a/c TextField i will input the ID and name in the two TextField. After that I have to select another RadioButton either Deposit or Withdraw and then need to input, how much balance to deposit and withdraw.
And after inputting all the information I need to click the submit button. When I click submit, All the information should display in the JTextArea, and wherever I deposit or withdraw, it should update the information. And this is where I am facing the problem, I have tired many different ways, but failed.....Please help
Please see my coding below:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class BankAccount implements ActionListener,ItemListener
{
private String id;
private double balance;
private String name;
private double withdraw;
private double deposit;
public BankAccount(String id, double balance, String name, double withdraw, double deposit)
{
this.id = id;
this.balance = balance;
this.name = name;
this.withdraw = withdraw;
this.deposit = deposit;
}
public void deposit(double sum)
{
this.balance = this.balance + sum;
}
public void withdraw(double sum)
{
this.balance = this.balance - sum;
}
public String getId()
{
return this.id;
}
public double getBalance()
{
return this.balance;
}
public String getName()
{
return this.name;
}
public double getWithdraw()
{
return this.withdraw;
}
DefaultListModel listModel = new DefaultListModel();
JList list = new JList(listModel);
FlowLayout flow = new FlowLayout();
ButtonGroup group = new ButtonGroup();
JFrame frame = new JFrame("Lexus Bank");
JPanel p = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JRadioButton a = new JRadioButton("Savings");
JRadioButton b = new JRadioButton("Current");
JRadioButton c = new JRadioButton("Deposit");
JRadioButton d = new JRadioButton("withdraw");
JLabel l1 = new JLabel("A/C No:");
JLabel l2 = new JLabel("A/C Name:");
JTextField accID = new JTextField(10);
JTextField accName = new JTextField(10);
JLabel l3 = new JLabel();
JLabel l4 = new JLabel();
JLabel l5 = new JLabel("Amount: " );
JLabel l6 = new JLabel("Current \n Amount: ");
JLabel l7 = new JLabel();
JTextField amount = new JTextField(10);
JButton button = new JButton("Submit");
JTextArea area = new JTextArea(10,30);
public BankAccount()
{
//Setting values for JFrame
frame.setSize(800,600);
frame.add(p);
frame.add(p2);
frame.add(p3);
frame.add(p4);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Adding the buttons in group
group.add(a);
group.add(b);
group.add(c);
group.add(d);
//Setting value for panel 1
frame.getContentPane().setLayout(flow);
p.setPreferredSize(new Dimension(100,100));
p.setLayout(new GridLayout(2,1));
p.add(a);
p.add(b);
p.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(),"A/C Type"));
//Setting value for panel 2
p2.setPreferredSize(new Dimension(300,100));
p2.setLayout(new GridLayout(4,3));
p2.add(l1);
p2.add(accID);
p2.add(l2);
p2.add(accName);
p2.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(),"Account Details"));
p2.setVisible(false);
//Setting value for panel 3
p3.setPreferredSize(new Dimension(300,150));
p3.setLayout(new FlowLayout());
p3.add(l3);
p3.add(c);
p3.add(l4);
p3.add(d);
p3.add(l5);
p3.add(amount);
p3.add(button);
p3.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(),"Transaction"));
p3.add(l6);
p3.setVisible(false);
//Setting value for panel 4
p4.setLayout(new GridLayout(1,2));
p4.add(area);
p4.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(),"Transaction History"));
p4.setVisible(false);
//Creating Actions
a.addItemListener(this);
b.addItemListener(this);
c.addActionListener(this);
d.addActionListener(this);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == button)
{
if(c.isSelected())
{
String item = area.getText();
listModel.addElement(item);
}
}
}
public void itemStateChanged(ItemEvent e)
{
Object source = e.getSource();
if(source == a)
{
p2.setVisible(true);
p3.setVisible(true);
p4.setVisible(true);
}
if(source == b)
{
p2.setVisible(true);
p3.setVisible(true);
p4.setVisible(true);
}
}
}
//Driver Class to run the program
public class BankAccount_Test {
public static void main(String args[])
{
BankAccount Test = new BankAccount();
}
}
If you take my advice use the concept of OOP by separating the classes in to two classes
by this way you have a lot of accounts in your program and each account have it's information saved in the object.
class Accounts{
private String id;
private double balance;
private String name;
private double withdraw;
private double deposit;
//parametric Constructor
public Accounts(String id,String name, double balance,double withdraw,double deposit){
setId(id);
setName(name);
setBalance(balance);
setWithdraw(withdraw);
setDeposit(deposit);
}
//Default Construcor:
//Highly recommended having it because you have a parametric constructor
public Accounts(){
//...
}
/*
Don't forget setters and Getter
........
*/
@Override
public String toString(){
return "ID: "+getId()+"\n"
+"Name: "+getName()+"\n"
+"Balance: "+getBalance()+"\n"
+"Withdraw: "+getWithdraw()+"\n"
+"Deposit: "+getDeposit()+"\n";
}
}
And this will be your Main class:
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Bank {
private List<Accounts> list ;
private JButton submit;
//default Constructor
public Bank(){
list = new ArrayList<Accounts>();
submit = new JButton("Submit");
submit .addActionListener(this);
/*
will be alot of code if I create the whole frame
but suppose you have created your frame */
}
public void actionPerformed(ActionEvent ev){
if(ev.getSource()==submit){
Accounts ac = new Accounts();
ac.setId("id.getText()");
ac.setName("name.getText()");
//...
textArea.setText(ac.toString());//see it's easy
list.add(ac);
}
}
}