Results 1 to 9 of 9
  1. #1
    wndtjr2 is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default adding texts to show multiple texts in one JTextArea panel

    I'm trying to write a banking system for an assignment.
    and I have a problem to show all transaction result.
    after I finish transactions, click the radiobutton "all transaction"
    then It supposed to show mutiple results on one panel.
    But It only shows last trasaction.
    I have no idea what to do next

    here are the codes

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    
    
    public class bankAccount1 {
    
    	public static void main(String[] args) {
    		
    		JFrame frame = new JFrame("Checking Account Actions.");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		CheckingAccountActionsPanel panel = new CheckingAccountActionsPanel();
    		frame.getContentPane().add(panel);
    		
    		frame.pack();
    		frame.setVisible(true);
    		
    	}
    }
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.text.NumberFormat;
    import java.util.ArrayList;
    
    import javax.swing.*;
    
    
    public class CheckingAccountActionsPanel extends JPanel{
    	
    	private JLabel quote;
    	private JRadioButton enterTransaction, allTransactions, allChecks, allDeposits; // radio button for transactions and lists
    	private double initialBalance; // initial balance
    	static double finalBalance; // final balance
    	
    	static checkingAccount check; 
    	static Transaction newTrans;
    	static double balance; // current balance
    	
    	private static double be500Charge = 5.00; //below $ 500 charge
    	private static double be0Charge = 10.00; // below $ 0 charge
    	private static double checkCharge = 0.15; // checking charge
    	private static double depositCharge = 0.10; // deposit charge
    	
    	ArrayList transList;
    	
    	static int checkID = 2,depositID = 1,serviceChargeID = 3; // ID for check, deposit, service charge
    	
    	
    	public CheckingAccountActionsPanel() {
    		
    		initialBalance = initialBal(); //get initial balance
    		check = new checkingAccount(initialBalance); // passing initial balance  to checkingAcount class
    		
    		quote = new JLabel("Choose action:");
    		quote.setFont(new Font("Helvetica", Font.BOLD,24));
    		
    		enterTransaction = new JRadioButton("enter transaction", true);
    		enterTransaction.setBackground(Color.yellow);
    		allTransactions = new JRadioButton("list all transactions", true);
    		allTransactions.setBackground(Color.yellow);
    		allChecks = new JRadioButton("list all checks", true);
    		allChecks.setBackground(Color.yellow);
    		allDeposits = new JRadioButton("list all deposits",true);
    		allDeposits.setBackground(Color.yellow);
    		
    		ButtonGroup group = new ButtonGroup(); // bind together
    		group.add(enterTransaction);
    		group.add(allTransactions);
    		group.add(allChecks);
    		group.add(allDeposits);
    		
    		QuoteListener listener = new QuoteListener();
    		enterTransaction.addActionListener(listener);
    		allTransactions.addActionListener(listener);
    		allChecks.addActionListener(listener);
    		allDeposits.addActionListener(listener);
    		
    		add(quote);
    		add(enterTransaction);
    		add(allTransactions);
    		add(allChecks);
    		add(allDeposits);
    		
    		setBackground(Color.yellow);
    		setPreferredSize(new Dimension(300,100));
    		
    	}
    	
    	private class QuoteListener implements ActionListener{
    		
    		String line;
    		
    		public void actionPerformed (ActionEvent event){
    			
    			Object source = event.getSource();
    			
    			int transCode;
    			double transAmount;
    			int i = 0;
    			if(source == enterTransaction){//if enter transaction is clicked
    				transCode = getTCode(); // get trans code
    				transAmount = getTAmount(); // get trans amount
    				newTrans = new Transaction(check.gettransCount(),transCode,transAmount);
    				check.addTrans(newTrans);
    				
    				if(transCode == 1){
    					check.setBalance(transAmount, transCode); //pass trans amount and trans code to checkingAccount
    					
    					processCheck(transCode,transAmount,initialBalance);// show results of the transaction
    					JOptionPane.showMessageDialog(null, transList.get(i));
    					
    				}
    				else if(transCode == 2){
    					check.setBalance(transAmount, transCode);
    					newTrans = new Transaction(check.gettransCount(),transCode,transAmount);
    					processDeposit(transCode,transAmount);
    					check.addTrans(newTrans);
    				}
    				else{
    					finalBalance = (check.getBalance() - check.getServiceCharge());
    					JOptionPane.showMessageDialog(null, "Transaction : End +" +
    							"\nCurrent Balance : $" + check.getBalance() +
    							"Total Service Charge : $" + check.getServiceCharge() +
    							"\nFinal Balance : $" + finalBalance);
    				}
    			}
    			else if(source == allTransactions){
    				
    				int index;
    				double amount = 0;
    				int nums = 0;
    				int types = 0;
    				String line;
    				
    				line = String.format("List All Transactions" + "\n" + "ID        Type       Amount\n" );
    				
    				for(index = 0 ;index < check.getSize(); index++){
    					types = check.getTrans(index).getTransId();
    					nums = check.getTrans(index).getTransNumber();
    					amount = check.getTrans(index).getTransAmount();
    					line += String.format("%-10s  %3d  %10s", nums,types,amount) + "\n";
    				}
    				JTextArea text = new JTextArea(line);
    				text.setBorder(null);
    				text.setOpaque(false);
    				text.setFont(new Font("Monospaced", Font.PLAIN, 14) );	
    				JOptionPane.showMessageDialog(null, text);
    			}
    					
    					
    					
    //				line = String.format("List all Transactions" + "\nID          Type        Amount\n");
    //				for(index = 0;index < check.getSize();index++){
    //					types = check.getTrans(index).getTransId();
    //					nums = check.getTrans(index).getTransNumber();
    //					amount = check.getTrans(index).getTransAmount();
    //					line += String.format("%-10s  %3d  %10s",types,nums,amount)+ "\n"; 
    //				}
    //				
    //				JTextArea text = new JTextArea(line);
    //				text.setBorder(null);
    //				text.setOpaque(false);
    //				text.setFont(new Font("Monospaced", Font.PLAIN, 14) );	
    //				JOptionPane.showMessageDialog(null, text);
    //				
    //			}
    			else if(source == allChecks){
    				
    				line = String.format("List all Transactions");
    				JTextArea text = new JTextArea(line);
    				 text.setBorder(null);
    				 text.setOpaque(false);
    				 text.setFont(new Font("Monospaced", Font.PLAIN, 14) );	
    				 JOptionPane.showMessageDialog(null, text);
    				
    			}
    			else if(source == allDeposits){
    				
    				line = String.format("List all Transactions");
    				JTextArea text = new JTextArea(line);
    				 text.setBorder(null);
    				 text.setOpaque(false);
    				 text.setFont(new Font("Monospaced", Font.PLAIN, 14) );	
    				 JOptionPane.showMessageDialog(null, text);
    				
    			}
    		}
    	}
    	public static double initialBal(){
    		
    		double iniBal;
    		String input;
    		input = JOptionPane.showInputDialog("Enter initial account balance :");
    		iniBal = Double.parseDouble(input);
    		return iniBal;
    	}
    	public static int getTCode(){
    		
    		int tCode;
    		String input;
    		input = JOptionPane.showInputDialog("Enter trans code.");
    		tCode = Integer.parseInt(input);
    		return tCode;
    	}
    	public static double getTAmount(){
    		
    		double tAmount;
    		String input;
    		input = JOptionPane.showInputDialog("Enter trans amount.");
    		tAmount = Double.parseDouble(input);
    		return tAmount;
    	}
    
    	public static void processCheck(int tCode, double tAmount, double initial){
    		
    		balance = check.getBalance();
    		
    		if(balance <= 0){
    			check.setServiceCharge(be0Charge);
    			
    			check.setServiceCharge(checkCharge);
    			JOptionPane.showMessageDialog(null, "Transaction: Check in amount of $" + tAmount
    					+ "\n Current Balance : $ " + balance
    					+ "\nService Charge : Check --- charge $ " + checkCharge
    					+ "\nWarning : Balance below $ 50"
    					+ "\nService Charge : Below $0 --- charge $" + be0Charge
    					+ "\nTotal Service Charge : $ " + check.getServiceCharge()) ;
    		}
    		else if(balance <= 50){
    			check.setServiceCharge(checkCharge);
    			JOptionPane.showMessageDialog(null, "Transaction: Check in amount of $" + tAmount
    					+ "\n Current Balance : $ " + balance
    					+ "\nService Charge : Check --- charge $ " + checkCharge
    					+ "\nWarning : Balance below $ 50"
    					+ "\nTotal Service Charge : $ " + check.getServiceCharge()) ;
    		}
    		else if(balance <= 500){
    			check.setServiceCharge(checkCharge);
    			check.setServiceCharge(be500Charge);
    			JOptionPane.showMessageDialog(null, "Transaction: Check in amount of $" + tAmount
    					+ "\n Current Balance : $ " + balance
    					+ "\nService Charge : Check --- charge $ " + checkCharge
    					+ "\nWarning : Balance below $ 500 --- charge $" + be500Charge 
    					+ "\nTotal Service Charge : $ " + check.getServiceCharge()) ;
    		}
    		else{
    			check.setServiceCharge(checkCharge);
    			JOptionPane.showMessageDialog(null, "Transaction: Check in amount of $" + tAmount
    					+ "\n Current Balance : $ " + balance
    					+ "\nService Charge : Check --- charge $ " + checkCharge
    					+ "\nTotal Service Charge : $ " + check.getServiceCharge()) ;	
    		}
    	}
    	private static void processDeposit(int tCode,double tAmount){
    		check.setServiceCharge(depositCharge);
    		balance = check.getBalance();
    		JOptionPane.showMessageDialog(null, "Transaction : Deposit in amount of $" + tAmount +
    				"\nCurrent Balance : $" + balance +
    				"\nService Charge : Deposit --- charge $ " + depositCharge +
    				"\nTotal Service Charge : $" + check.getServiceCharge());
    	}
    }
    Java Code:
    import java.util.ArrayList;
    
    public class checkingAccount{
          
    	private double balance = 0;
    	private double totalServiceCharge = 0;
    	private ArrayList<Transaction> transList;  // keeps a list of Transaction objects for the account
    	private int transCount = 0;   // the count of Transaction objects and used as the ID for each transaction 
    	private int transSize = 0;
    
    	public checkingAccount(double initialBalance){
    
    		balance = initialBalance;
    	}
    	public double getBalance(){
    		return balance; 
    	}
    	public void setBalance(double transAmt, int tCode){
    		if(tCode == 1)
    			balance -= transAmt;
    		else //if(tCode == 2)
    			balance += transAmt; 
    	}
    	public double getServiceCharge(){    
    		return totalServiceCharge;
     
    	}
    	public void setServiceCharge(double currentServiceCharge){
    		totalServiceCharge += currentServiceCharge;  
    	}
    	public void addTrans(Transaction newTrans){// adds a transaction object to the transList
    		transList = new ArrayList<Transaction>();
    		transList.add(newTrans);
    	}
    	public int gettransCount(){
    		transCount++;
    		return transCount;  //returns the current value of transCount;
    		
    	}
    	
    	public Transaction getTrans(int i){
    		return transList.get(i); // returns the i-th Transaction object in the list
    		
    	}
    	public int getSize(){
    		transSize = transList.size();
    		return transSize;
    	}
    }
    Java Code:
    public class Transaction{
    	
        private int transNumber = 0; 
        private int transId = 0;
        private double transAmt = 0;
        
        public Transaction(int number, int id, double amount){
        	
            transNumber = number;
            transId = id;
            transAmt = amount;
        }
        public int getTransNumber(){
        	
            return transNumber;
        }
        public int getTransId(){
        	
            return transId;
        }
        public double getTransAmount(){
        	
            return transAmt;
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: adding texts to show multiple texts in one JTextArea panel

    But It only shows last trasaction.
    Where do you store the data for all the transactions? Are all the transactions stored there or only the last one?

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: adding texts to show multiple texts in one JTextArea panel

    Don't use setText(...). That replaces the previous text.

    Instead use append(...);

  4. #4
    wndtjr2 is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: adding texts to show multiple texts in one JTextArea panel

    I store all transaction at getTrans in check class.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: adding texts to show multiple texts in one JTextArea panel

    As each new transaction is stored, are the previous versions saved? Add some printlns to show that the number of saved transactions increases by one each time a new one is added.

  6. #6
    wndtjr2 is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: adding texts to show multiple texts in one JTextArea panel

    I modified a little by your advice
    But It still doesn't add each text to one arraylist.

    here is what i changed
    it's located at 106 - 130 line
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.text.NumberFormat;
    import java.util.ArrayList;
    
    import javax.swing.*;
    
    
    public class CheckingAccountActionsPanel extends JPanel{
    	
    	private JLabel quote;
    	private JRadioButton enterTransaction, allTransactions, allChecks, allDeposits; // radio button for transactions and lists
    	private double initialBalance; // initial balance
    	static double finalBalance; // final balance
    	
    	static checkingAccount check; 
    	static Transaction newTrans;
    	static double balance; // current balance
    	
    	private static double be500Charge = 5.00; //below $ 500 charge
    	private static double be0Charge = 10.00; // below $ 0 charge
    	private static double checkCharge = 0.15; // checking charge
    	private static double depositCharge = 0.10; // deposit charge
    	
    	ArrayList transList;
    	
    	static int checkID = 2,depositID = 1,serviceChargeID = 3; // ID for check, deposit, service charge
    	
    	
    	public CheckingAccountActionsPanel() {
    		
    		initialBalance = initialBal(); //get initial balance
    		check = new checkingAccount(initialBalance); // passing initial balance  to checkingAcount class
    		
    		quote = new JLabel("Choose action:");
    		quote.setFont(new Font("Helvetica", Font.BOLD,24));
    		
    		enterTransaction = new JRadioButton("enter transaction", true);
    		enterTransaction.setBackground(Color.yellow);
    		allTransactions = new JRadioButton("list all transactions", true);
    		allTransactions.setBackground(Color.yellow);
    		allChecks = new JRadioButton("list all checks", true);
    		allChecks.setBackground(Color.yellow);
    		allDeposits = new JRadioButton("list all deposits",true);
    		allDeposits.setBackground(Color.yellow);
    		
    		ButtonGroup group = new ButtonGroup(); // bind together
    		group.add(enterTransaction);
    		group.add(allTransactions);
    		group.add(allChecks);
    		group.add(allDeposits);
    		
    		QuoteListener listener = new QuoteListener();
    		enterTransaction.addActionListener(listener);
    		allTransactions.addActionListener(listener);
    		allChecks.addActionListener(listener);
    		allDeposits.addActionListener(listener);
    		
    		add(quote);
    		add(enterTransaction);
    		add(allTransactions);
    		add(allChecks);
    		add(allDeposits);
    		
    		setBackground(Color.yellow);
    		setPreferredSize(new Dimension(300,100));
    		
    	}
    	
    	private class QuoteListener implements ActionListener{
    		
    		String line;
    		
    		public void actionPerformed (ActionEvent event){
    			
    			Object source = event.getSource();
    			
    			int transCode;
    			double transAmount;
    			int i = 0;
    			if(source == enterTransaction){//if enter transaction is clicked
    				transCode = getTCode(); // get trans code
    				transAmount = getTAmount(); // get trans amount
    				newTrans = new Transaction(check.gettransCount(),transCode,transAmount);
    				check.addTrans(newTrans);
    				
    				if(transCode == 1){
    					check.setBalance(transAmount, transCode); //pass trans amount and trans code to checkingAccount
    					processCheck(transCode,transAmount,initialBalance);// show results of the transaction
    					
    					
    				}
    				else if(transCode == 2){
    					check.setBalance(transAmount, transCode);
    					processDeposit(transCode,transAmount);
    					
    				}
    				else{
    					finalBalance = (check.getBalance() - check.getServiceCharge());
    					JOptionPane.showMessageDialog(null, "Transaction : End +" +
    							"\nCurrent Balance : $" + check.getBalance() +
    							"Total Service Charge : $" + check.getServiceCharge() +
    							"\nFinal Balance : $" + finalBalance);
    				}
    			}
    			else if(source == allTransactions){
    				
    				int index;
    				double amount = 0;
    				int nums = 0;
    				int types = 0;
    				String line;
    				
    				line = String.format("List All Transactions" + "\n" + "ID        Type       Amount\n" );
    				JTextArea text = new JTextArea(line);
    				for(index = 0 ;index < check.getSize(); index++){
    					types = check.getTrans(index).getTransId();
    					nums = check.getTrans(index).getTransNumber();
    					amount = check.getTrans(index).getTransAmount();
    					line = String.format("%-10s  %3d  %10s", nums,types,amount);
    					text.append(line);
    				}
    				
    				text.setBorder(null);
    				text.setOpaque(false);
    				text.setFont(new Font("Monospaced", Font.PLAIN, 14) );	
    				
    				JOptionPane.showMessageDialog(null, text);
    				
    			}
    			
    			else if(source == allChecks){
    				
    				line = String.format("List all Transactions");
    				JTextArea text = new JTextArea(line);
    				 text.setBorder(null);
    				 text.setOpaque(false);
    				 text.setFont(new Font("Monospaced", Font.PLAIN, 14) );	
    				 JOptionPane.showMessageDialog(null, text);
    				
    			}
    			else if(source == allDeposits){
    				
    				line = String.format("List all Transactions");
    				JTextArea text = new JTextArea(line);
    				 text.setBorder(null);
    				 text.setOpaque(false);
    				 text.setFont(new Font("Monospaced", Font.PLAIN, 14) );	
    				 JOptionPane.showMessageDialog(null, text);
    				
    			}
    		}
    	}
    	public static double initialBal(){
    		
    		double iniBal;
    		String input;
    		input = JOptionPane.showInputDialog("Enter initial account balance :");
    		iniBal = Double.parseDouble(input);
    		return iniBal;
    	}
    	public static int getTCode(){
    		
    		int tCode;
    		String input;
    		input = JOptionPane.showInputDialog("Enter trans code.");
    		tCode = Integer.parseInt(input);
    		return tCode;
    	}
    	public static double getTAmount(){
    		
    		double tAmount;
    		String input;
    		input = JOptionPane.showInputDialog("Enter trans amount.");
    		tAmount = Double.parseDouble(input);
    		return tAmount;
    	}
    
    	public static void processCheck(int tCode, double tAmount, double initial){
    		
    		balance = check.getBalance();
    		
    		if(balance <= 0){
    			check.setServiceCharge(be0Charge);
    			
    			check.setServiceCharge(checkCharge);
    			JOptionPane.showMessageDialog(null, "Transaction: Check in amount of $" + tAmount
    					+ "\n Current Balance : $ " + balance
    					+ "\nService Charge : Check --- charge $ " + checkCharge
    					+ "\nWarning : Balance below $ 50"
    					+ "\nService Charge : Below $0 --- charge $" + be0Charge
    					+ "\nTotal Service Charge : $ " + check.getServiceCharge()) ;
    		}
    		else if(balance <= 50){
    			check.setServiceCharge(checkCharge);
    			JOptionPane.showMessageDialog(null, "Transaction: Check in amount of $" + tAmount
    					+ "\n Current Balance : $ " + balance
    					+ "\nService Charge : Check --- charge $ " + checkCharge
    					+ "\nWarning : Balance below $ 50"
    					+ "\nTotal Service Charge : $ " + check.getServiceCharge()) ;
    		}
    		else if(balance <= 500){
    			check.setServiceCharge(checkCharge);
    			check.setServiceCharge(be500Charge);
    			JOptionPane.showMessageDialog(null, "Transaction: Check in amount of $" + tAmount
    					+ "\n Current Balance : $ " + balance
    					+ "\nService Charge : Check --- charge $ " + checkCharge
    					+ "\nWarning : Balance below $ 500 --- charge $" + be500Charge 
    					+ "\nTotal Service Charge : $ " + check.getServiceCharge()) ;
    		}
    		else{
    			check.setServiceCharge(checkCharge);
    			JOptionPane.showMessageDialog(null, "Transaction: Check in amount of $" + tAmount
    					+ "\n Current Balance : $ " + balance
    					+ "\nService Charge : Check --- charge $ " + checkCharge
    					+ "\nTotal Service Charge : $ " + check.getServiceCharge()) ;	
    		}
    	}
    	private static void processDeposit(int tCode,double tAmount){
    		check.setServiceCharge(depositCharge);
    		balance = check.getBalance();
    		JOptionPane.showMessageDialog(null, "Transaction : Deposit in amount of $" + tAmount +
    				"\nCurrent Balance : $" + balance +
    				"\nService Charge : Deposit --- charge $ " + depositCharge +
    				"\nTotal Service Charge : $" + check.getServiceCharge());
    	}
    }
    Last edited by wndtjr2; 11-05-2011 at 07:12 AM.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: adding texts to show multiple texts in one JTextArea panel

    Where are the printlns that show the number of saved transactions increasing by one each time a new one is added?

  8. #8
    wndtjr2 is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: adding texts to show multiple texts in one JTextArea panel

    I think text.append(line) do the same work at line 121 is'n it?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: adding texts to show multiple texts in one JTextArea panel

    You need to make sure that each time you add a new object to the list that the length of the list increases by one.

    The append() statement is not used until much later after you have added to the list.
    You need to test EVERY time sometime is added to to the list. You are assuming something that needs to be verified by detailed debugging.

Similar Threads

  1. How to search russian texts in Lucene index?
    By ArtUrlWWW in forum Lucene
    Replies: 0
    Last Post: 01-11-2011, 11:46 AM
  2. Replies: 2
    Last Post: 02-01-2010, 05:45 PM
  3. Replies: 1
    Last Post: 08-21-2009, 08:07 PM
  4. Replies: 3
    Last Post: 12-21-2008, 08:22 PM
  5. color texts
    By layst in forum New To Java
    Replies: 1
    Last Post: 10-22-2008, 02:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •