Hi I am in a java class and am working on a mortgage calculator with gui. I think I almost am done and have the assignment parameters completed but am stuck on two issues.

1) can't get my scroll pane to work.

2) I want to have the user select from a menu and then have the menu items populate the textfields. I can get part of it to work.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.text.*;
import java.text.NumberFormat;


public class shannon_change_request_6 extends JFrame{

    public shannon_change_request_6 ()
    {
        super ("Mortgage Calculator");
        initComponents();
    }
            
    private void initComponents ()
    {
    //set paramaters initialize the data fields
    
        principleLabel = new JLabel ();     //the principle label
        principleField = new JFormattedTextField (); //the principle text field
        loanMenuLabel = new JLabel();
        interestLabel = new JLabel();
        lengthYearsLabel = new JLabel ();
        monthlyPaymentLabel = new JLabel ();
        interestField = new JFormattedTextField();
        lengthYearsField = new JFormattedTextField ();
        monthlyPaymentField = new JFormattedTextField ();
        
        clearButton = new JButton (); //create the clear button
        calculateButton = new JButton ();//create the calculate button
        quitButton = new JButton ();// create the quit button
        topPanel = new JPanel ();//create panel to place label/field and 
        centerPanel = new JPanel (); //create panel for mortgage scheudle
        bottomPanel = new JPanel ();//create panel for buttons
        scrollSchedule = new JScrollPane ();//scroll pane for mortgage sched
        scrollText = new JTextArea (); //set text area for scroll pane
        scheduleTitle = new JLabel();
        loanMenu = new JComboBox ();//create list for loan menu
        componentFonts = new Font("Times New Roman", Font.PLAIN, 14); //create fonnt
        
        
       
   
        //layout of mortgage scheudle
        
             
        getContentPane().setLayout(new BorderLayout()); 
        
        //set menu items for user to choose
        
        String[] loanMenuItems = {"7 years at 5.35%", "15 years at 5.5%",
            "30 years at 5.75%"
        };
        
        loanMenu = new JComboBox (loanMenuItems);
        
        topPanel.setLayout(null); //allows to create own size
        topPanel.setPreferredSize(new Dimension(1000,175)); //create size
        topPanel.setBackground(Color.blue);//set color
        topPanel.add (principleField); //add to top
        topPanel.add (principleLabel); //add to top
        topPanel.add (loanMenu); //add to top
        topPanel.add (loanMenuLabel);//add to top
        topPanel.add (loanMenu);
        topPanel.add (interestField);
        topPanel.add (interestLabel);
        topPanel.add (lengthYearsField);
        topPanel.add (lengthYearsLabel);
        topPanel.add (monthlyPaymentLabel);
        topPanel.add (monthlyPaymentField);
        //position and set size for add items. First two numbers postion, last two size
        
        principleField.setBounds (200,10,150,30);
        principleLabel.setBounds (20,10,150,30);
        loanMenuLabel.setBounds (500,10,150,30);
        loanMenu.setBounds (660,10,175,30);
        interestField.setBounds (200,50,150,30);
        interestLabel.setBounds (20,50,150,30);
        lengthYearsField.setBounds (200,90,150,30);
        lengthYearsLabel.setBounds (20,90,150,30);
        monthlyPaymentField.setBounds (200,130,150,30);
        monthlyPaymentLabel.setBounds (20,130,150,30);
        
        principleLabel.setFont(componentFonts);
        principleLabel.setText("Enter Loan Amount:");
        loanMenuLabel.setFont(componentFonts);
        loanMenuLabel.setText ("Or Choose Available Loan:");
        loanMenu.setFont (componentFonts);
        interestLabel.setFont(componentFonts);
        interestLabel.setText("Enter InterestRate:");
        lengthYearsLabel.setFont(componentFonts);
        lengthYearsLabel.setText("Enter Loan Term (years):");                           
        monthlyPaymentLabel.setFont(componentFonts);
        monthlyPaymentLabel.setText("The Monthly Payment is:");
        
        centerPanel.setLayout(null); //allows to create own size
        centerPanel.setPreferredSize(new Dimension(700,400)); //create  size, first number width
        centerPanel.setBackground(Color.white);//set color
        centerPanel.add (scrollSchedule); //add center
        centerPanel.add (scheduleTitle);
        centerPanel.add (scrollText);
        
        //position and set size for add items. First two numbers postion, last two size
        scheduleTitle.setBounds (80,10,650,30);
        scrollSchedule.setBounds (80,40,700,350);
        scrollText.setBounds (80,40,700,350);
        
        scheduleTitle.setFont (componentFonts);
        scheduleTitle.setText ("Payment      Interest Paid           Balance"); 
        
        
        bottomPanel.setLayout(null); //allows to create own size
        bottomPanel.setPreferredSize(new Dimension(700,50)); //create size
        bottomPanel.setBackground(Color.blue);//set color
        bottomPanel.add (calculateButton); //add to top
        bottomPanel.add (clearButton); //add to top
        bottomPanel.add (quitButton); //add to top
        
        calculateButton.setFont(componentFonts);
        calculateButton.setText ("Calculate");
        
        clearButton.setFont(componentFonts);
        clearButton.setText ("Clear");
        
        quitButton.setFont(componentFonts);
        quitButton.setText ("Exit");
        //position and set size for add items. First two numbers postion, last two size
        
        calculateButton.setBounds (100,10,100,30);
        clearButton.setBounds (300,10,100,30);
        quitButton.setBounds (500,10,100,30);
    
               
           
   //assign action listners     
    quitButton.addActionListener(new ActionListener(){  //quit program
            public void actionPerformed(ActionEvent ae){
                System.exit(0);
            }
        });
   
    clearButton.addActionListener(new ActionListener(){  //clear fields
            public void actionPerformed(ActionEvent ae){
                principleField.setText("");
                interestField.setText("");
                lengthYearsField.setText("");
                monthlyPaymentField.setText ("");
            }
        });
    
    calculateButton.addActionListener (new ActionListener(){
        public void actionPerformed (ActionEvent as){
        NumberFormat nf = NumberFormat.getCurrencyInstance();//display currency and decimal place
        double interest = Double.parseDouble(interestField.getText()); 
        double principle = Double.parseDouble (principleField.getText());
        double lengthyears = Double.parseDouble (lengthYearsField.getText());
        
            
        double monthlypayment = (principle * interest  /100/12)/ 
                (1 - Math.pow(1/ (1 + interest /100/12),lengthyears * 12));
        monthlyPaymentField.setValue (new Double (monthlypayment));                    
        
        
        //for loop to provide calculation and add to text box.
        for (int count=1;count<=lengthyears*12;count++){
       	}
        interest = interest/100;
	double new_balance = principle;
	
        for (int count=1;count<=lengthyears*12;count++){
	
            double payment = count;
            double monthly_interest = new_balance * interest * 1/12;
            double principlePayment = monthlypayment - monthly_interest;
            new_balance = new_balance - principlePayment;
					
            scrollText.append(" " + payment + "\t" + nf.format(monthly_interest) 
            + "\t" + nf.format(new_balance) + "\n");
        
           }
        }
    
    });
    
    
    
    
    loanMenu.addItemListener(new ItemListener(){
       public void itemStateChanged(ItemEvent ie){
        String str = (String)loanMenu.getSelectedItem();
        lengthYearsField.setText("7");
        lengthYearsField.setText("15");
        lengthYearsField.setText("30");
        interestField.setText("5.35");
        interestField.setText("5.5");
        interestField.setText("5.75");
        
        
  }
  });

    
    
   getContentPane().add(topPanel, BorderLayout.NORTH);
   
   getContentPane().add(centerPanel, BorderLayout.CENTER);
   
   getContentPane().add(bottomPanel, BorderLayout.SOUTH);
   
   pack ();
   setResizable (true);
   setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   setLocation(25,25);
   
    
    }
     
      
    
  
    
    public static void main(String[] args)
    //command to tell program to run display
    {
        EventQueue.invokeLater(new Runnable ()
        {
            public void run()
            {new shannon_change_request_6().setVisible(true);
            }
        });
    }
     
    
        //datafieldmembers
        private JLabel principleLabel;
        private JFormattedTextField principleField;
        private JLabel loanMenuLabel;
        private JLabel interestLabel;
        private JLabel lengthYearsLabel;
        private JLabel monthlyPaymentLabel;
        private JFormattedTextField interestField;
        private JFormattedTextField lengthYearsField;
        private JFormattedTextField monthlyPaymentField;

        private JButton clearButton;
        private JButton calculateButton;
        private JButton quitButton;
        private JPanel topPanel;
        private JPanel centerPanel;
        private JPanel bottomPanel;
        private JScrollPane scrollSchedule;
        private JTextArea scrollText;
        private JLabel scheduleTitle;
        private JComboBox loanMenu;
        private Font componentFonts;
        private double principle; //for manual user input
        private double interest; //for manual user input
        private int lengthyears; //for manual user input
          
        
}

Recommended Answers

All 7 Replies

Please edit your code and add code tags (the [code] icon above the input box) to restore the code's indentations. Unformatted code is hard to read.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package shannon6;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.text.*;
import java.text.NumberFormat;


public class Shannon6 extends JFrame{

    public Shannon6 ()
    {
        super ("Mortgage Calculator");
        initComponents();
    }
            
    private void initComponents ()
    {
    //set paramaters initialize the data fields
    
        principleLabel = new JLabel ();     //the principle label
        principleField = new JFormattedTextField (); //the principle text field
        loanMenuLabel = new JLabel();
        interestLabel = new JLabel();
        lengthYearsLabel = new JLabel ();
        monthlyPaymentLabel = new JLabel ();
        interestField = new JFormattedTextField();
        lengthYearsField = new JFormattedTextField ();
        monthlyPaymentField = new JFormattedTextField ();
        
        clearButton = new JButton (); //create the clear button
        calculateButton = new JButton ();//create the calculate button
        quitButton = new JButton ();// create the quit button
        topPanel = new JPanel ();//create panel to place label/field and 
        centerPanel = new JPanel (); //create panel for mortgage scheudle
        bottomPanel = new JPanel ();//create panel for buttons
        loanMenu = new JComboBox ();//create list for loan menu
        componentFonts = new Font("Times New Roman", Font.PLAIN, 14); //create fonnt
        
        scrollSchedule = new JScrollPane ();//scroll pane for mortgage sched
        scrollText = new JTextArea (); //set text area for scroll pane
        scheduleTitle = new JLabel();
       
   
        //layout of mortgage scheudle
        
             
        getContentPane().setLayout(new BorderLayout()); 
        
        //set menu items for user to choose
        
        String[] loanMenuItems = {"7 years at 5.35%", "15 years at 5.5%",
            "30 years at 5.75%"
        };
        
        loanMenu = new JComboBox (loanMenuItems);
        
        topPanel.setLayout(null); //allows to create own size
        topPanel.setPreferredSize(new Dimension(1000,175)); //create size
        topPanel.setBackground(Color.blue);//set color
        topPanel.add (principleField); //add to top
        topPanel.add (principleLabel); //add to top
        topPanel.add (loanMenu); //add to top
        topPanel.add (loanMenuLabel);//add to top
        topPanel.add (loanMenu);
        topPanel.add (interestField);
        topPanel.add (interestLabel);
        topPanel.add (lengthYearsField);
        topPanel.add (lengthYearsLabel);
        topPanel.add (monthlyPaymentLabel);
        topPanel.add (monthlyPaymentField);
        //position and set size for add items. First two numbers postion, last two size
        
        principleField.setBounds (200,10,150,30);
        principleLabel.setBounds (20,10,150,30);
        loanMenuLabel.setBounds (500,10,150,30);
        loanMenu.setBounds (660,10,175,30);
        interestField.setBounds (200,50,150,30);
        interestLabel.setBounds (20,50,150,30);
        lengthYearsField.setBounds (200,90,150,30);
        lengthYearsLabel.setBounds (20,90,150,30);
        monthlyPaymentField.setBounds (200,130,150,30);
        monthlyPaymentLabel.setBounds (20,130,150,30);
        
        principleLabel.setFont(componentFonts);
        principleLabel.setText("Enter Loan Amount:");
        loanMenuLabel.setFont(componentFonts);
        loanMenuLabel.setText ("Or Choose Available Loan:");
        loanMenu.setFont (componentFonts);
        interestLabel.setFont(componentFonts);
        interestLabel.setText("Enter InterestRate:");
        lengthYearsLabel.setFont(componentFonts);
        lengthYearsLabel.setText("Enter Loan Term (years):");                           
        monthlyPaymentLabel.setFont(componentFonts);
        monthlyPaymentLabel.setText("The Monthly Payment is:");
        
        centerPanel.setLayout(null); //allows to create own size
        centerPanel.setPreferredSize(new Dimension(700,400)); //create  size, first number width
        centerPanel.setBackground(Color.white);//set color
        centerPanel.add (scrollSchedule); //add center
        centerPanel.add (scheduleTitle);
        centerPanel.add (scrollText);
        
        //position and set size for add items. First two numbers postion, last two size
        scheduleTitle.setBounds (80,10,650,30);
        scrollSchedule.setBounds (80,40,700,350);
        scrollText.setBounds (80,40,700,350);
        
        scheduleTitle.setFont (componentFonts);
        scheduleTitle.setText ("Payment      Interest Paid           Balance"); 
        
        
        bottomPanel.setLayout(null); //allows to create own size
        bottomPanel.setPreferredSize(new Dimension(700,50)); //create size
        bottomPanel.setBackground(Color.blue);//set color
        bottomPanel.add (calculateButton); //add to top
        bottomPanel.add (clearButton); //add to top
        bottomPanel.add (quitButton); //add to top
        
        calculateButton.setFont(componentFonts);
        calculateButton.setText ("Calculate");
        
        clearButton.setFont(componentFonts);
        clearButton.setText ("Clear");
        
        quitButton.setFont(componentFonts);
        quitButton.setText ("Exit");
        //position and set size for add items. First two numbers postion, last two size
        
        calculateButton.setBounds (100,10,100,30);
        clearButton.setBounds (300,10,100,30);
        quitButton.setBounds (500,10,100,30);
    
               
           
   //assign action listners     
    quitButton.addActionListener(new ActionListener(){  //quit program
            public void actionPerformed(ActionEvent ae){
                System.exit(0);
            }
        });
   
    clearButton.addActionListener(new ActionListener(){  //clear fields
            public void actionPerformed(ActionEvent ae){
                principleField.setText("");
                interestField.setText("");
                lengthYearsField.setText("");
                monthlyPaymentField.setText ("");
            }
        });
    
    calculateButton.addActionListener (new ActionListener(){
        public void actionPerformed (ActionEvent as){
        NumberFormat nf = NumberFormat.getCurrencyInstance();//display currency and decimal place
        double interest = Double.parseDouble(interestField.getText()); 
        double principle = Double.parseDouble (principleField.getText());
        double lengthyears = Double.parseDouble (lengthYearsField.getText());
        
            
        double monthlypayment = (principle * interest  /100/12)/ 
                (1 - Math.pow(1/ (1 + interest /100/12),lengthyears * 12));
        monthlyPaymentField.setValue (new Double (monthlypayment));                    
        
        
        //for loop to provide calculation and add to text box.
        for (int count=1;count<=lengthyears*12;count++){
       	}
        interest = interest/100;
	double new_balance = principle;
	
        for (int count=1;count<=lengthyears*12;count++){
	
            double payment = count;
            double monthly_interest = new_balance * interest * 1/12;
            double principlePayment = monthlypayment - monthly_interest;
            new_balance = new_balance - principlePayment;
					
            scrollText.append(" " + payment + "\t" + nf.format(monthly_interest) 
            + "\t" + nf.format(new_balance) + "\n");
        
           }
        }
    
    });
    
    
    
    
    loanMenu.addItemListener(new ItemListener(){
       public void itemStateChanged(ItemEvent ie){
        String str = (String)loanMenu.getSelectedItem();
        lengthYearsField.setText("7");
        lengthYearsField.setText("15");
        lengthYearsField.setText("30");
        interestField.setText("5.35");
        interestField.setText("5.5");
        interestField.setText("5.75");
        
        
  }
  });

    
    
   getContentPane().add(topPanel, BorderLayout.NORTH);
   
   getContentPane().add(centerPanel, BorderLayout.CENTER);
   
   getContentPane().add(bottomPanel, BorderLayout.SOUTH);
   
   pack ();
   setResizable (true);
   setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   setLocation(25,25);
   
    
    }
     
      
       
    public static void main(String[] args)
    //command to tell program to run display
    {
        EventQueue.invokeLater(new Runnable ()
        {
            public void run()
            {new Shannon6().setVisible(true);
            }
        });
    }
     
    
        //datafieldmembers
        private JLabel principleLabel;
        private JFormattedTextField principleField;
        private JLabel loanMenuLabel;
        private JLabel interestLabel;
        private JLabel lengthYearsLabel;
        private JLabel monthlyPaymentLabel;
        private JFormattedTextField interestField;
        private JFormattedTextField lengthYearsField;
        private JFormattedTextField monthlyPaymentField;

        private JButton clearButton;
        private JButton calculateButton;
        private JButton quitButton;
        private JPanel topPanel;
        private JPanel centerPanel;
        private JPanel bottomPanel;
        private JScrollPane scrollSchedule;
        private JTextArea scrollText;
        private JLabel scheduleTitle;
        private JComboBox loanMenu;
        private Font componentFonts;
        private double principle; //for manual user input
        private double interest; //for manual user input
        private int lengthyears; //for manual user input
          
        
}

Thanks that's a lot better.
For the scroll pane go to this site:
http://download.oracle.com/javase/tutorial/reallybigindex.html
and find Scroll panes.
There is an explanation on how to use them.

I want to have the user select from a menu and then have the menu items populate the textfields. I can get part of it to work.

I don't see any menus in your code. Are you talking about the JComboBox?
Can you explain what more you want that code to do?

yes I was talking about the combo boxes.

Can you explain what more you want that code to do?

I want the user to choose an option from the jcombo box then I want the text fields to be filled in with the appropriate information....7, 15 or 30 in the years text field and the different interest rates in the interst field. I can get 1 to work but not all three options.

Your listener code for the combo box doesn't consider anything about the event that caused it to be called. Add a println to the code to see what information is available.
Something like: System.out.println("iSC ie=" + ie);
Then read the API doc for the Event class to see how to get that info.
Then change your code to use that info to select the correct data to put in the text fields.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.