0

Possible Duplicate:
How do I populate jcombobox from a textfile?

OK, I have an issue here that I cannot quiet figure out. I have a file that has variables on each line of: 5.35, 5.5, 5.75 in a file called apr.txt. I am reading the file and need to populate the file into a JComboBox. So far I have the file being read fine but cannot figure how to populate it into the JComboBox. This must be something easy. Can someone help point me in the proper direction?

Thank you for your help in advance.

import java.awt.*;
import java.text.NumberFormat;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.StringTokenizer;

import javax.swing.*;
import javax.swing.JComboBox;



  public class MortgageCalculatorGUI9 extends JFrame implements ActionListener
        {
         // Declarations

         //double [] Interest = {5.35, 5.5, 5.75};
         int [] Length = {7, 15, 30};
         double [] file_data = new double[3];


         JLabel mortgagelabel = new JLabel( "Principal Amount:$ " );
         JLabel paymentLabel = new JLabel( "Monthly Payment: " );
         JLabel intRateLabel = new JLabel( "Interest Rate %: " );
         JLabel termLabel = new JLabel( "Length of Loan of Loan in Years: "  );
         JTextField mortgagePrincipal = new JTextField(7);
         JTextField Payment = new JTextField(7);
         //JTextField intRateText = new JTextField(3);
         JComboBox intRateBox = new JComboBox();
         JTextField termText = new JTextField(3);
         JButton b7_535 = new JButton( "7 years at 5.35%" );
         JButton b15_55 = new JButton( "15 years at 5.50%" );
         JButton b30_575 = new JButton( "30 years at 5.75%");
         JButton exitButton = new JButton( "Exit" );
         JButton clearButton = new JButton( "Clear All" );
         JButton calculateButton = new JButton( "Calculate Loan" );
         JTextArea LoanPayments = new JTextArea(20,50);


        JScrollPane scroll = new JScrollPane(LoanPayments);

        public MortgageCalculatorGUI9()
        {
             //GUI setup
             super("Mortgage Calculator 1.0.5");
             setSize(800, 400);
             setLocation(500, 200);
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             JPanel pane = new JPanel(new GridLayout(3,1)); Container grid = getContentPane();
             grid.setLayout(new GridLayout(4,0,4,4)); pane.add(grid);
             pane.add(scroll);
             grid.add(mortgagelabel);
             grid.add(mortgagePrincipal);
             grid.add(intRateLabel);
             //grid.add(intRateText);
             grid.add (intRateBox);
             grid.add(termLabel);
             grid.add(termText);
             grid.add(paymentLabel);
             grid.add(Payment);
             grid.add(b7_535);
             grid.add(b15_55);
             grid.add(b30_575);
             grid.add(calculateButton);
             grid.add(clearButton);
             grid.add(exitButton);
             Payment.setEditable(false);
             setContentPane(pane);
             setContentPane(pane);
             setVisible(true);

            //add GUI functionality
            calculateButton.addActionListener (this);
            exitButton.addActionListener(this);
            clearButton.addActionListener(this);
            b7_535.addActionListener(this);
            b15_55.addActionListener(this);
            b30_575.addActionListener(this);
            mortgagePrincipal.addActionListener(this);
            //intRateText.addActionListener(this);
            intRateBox.addActionListener (this);
            termText.addActionListener(this);
            Payment.addActionListener(this);

         }

            public void actionPerformed(ActionEvent e)
                 {
                    Object command = e.getSource();

                    if (command == exitButton)
                 {

                    System.exit(0);

                 }
                 else if (command == b7_535)
                 {

                    calcLoan(Length[0], file_data[0]);

                 }
                 else if (command == b15_55)
                 {

                    calcLoan(Length[1], file_data[1]);

                 }
                 else if (command == b30_575)
                 {

                    calcLoan(Length[2], file_data[2]);

                 }

                 else if (command == calculateButton )
                 {

                    double terms = 0;
                    double rates = 0;
                    try

                 {
                     terms = Double.parseDouble(termText.getText());
                     //rates = Double.parseDouble(intRateText.getText());
                     read_File ();
                 }
                    catch (Exception ex)
                 {
                    LoanPayments.setText("Invalid term or rate Amount");
                    return;
                 }

                    calcLoan(terms, rates);

                 }

                 else if (command == clearButton)
                 {

                     mortgagePrincipal.setText("");
                     Payment.setText("");
                     //intRateText.setText("");
                     termText.setText("");
                     LoanPayments.setText("");

                 }

             }


             //Input File

                public void read_File()
                {

                    File inFile = new File("apr.txt");

                    try
                    {

                        BufferedReader istream = new BufferedReader(new FileReader(inFile));

                        for(int x=0;x<6;x++)
                        {
                            file_data[x]=Double.parseDouble (istream.readLine());
                        }



                    }

                    catch (Exception ex)
                        {

                            LoanPayments.setText ("Could Not Read From File.");
                            return;

                        }
                }



                //this is what needs to be done
                 private void calcLoan(double terms, double rates)

                 {

                     termText.setText(String.valueOf(terms) );
                     //intRateText.setText(String.valueOf(rates));
                     double amount = 0;

                 try
                 {

                    amount = Double.parseDouble(mortgagePrincipal.getText());

                 }

                    catch (Exception ex)

                 {

                    LoanPayments.setText("Invalid mortgage Amount");
                    return;

                 }

                     double interestRate = rates;

                     // Calculations
                     double intRate = (interestRate / 100) / 12;
                     int Months = (int)terms * 12;
                     double rate = (intRate / 12);
                     double payment = amount * intRate / (1 - (Math.pow(1/(1 +  intRate), Months)));
                     double remainingPrincipal = amount;
                     double MonthlyInterest = 0;
                     double MonthlyAmt = 0;
                     double[] balanceArray = new double[Months];
                     double[] interestArray = new double[Months];
                     double[] monthArray = new double[Months];
                     NumberFormat Money =  NumberFormat.getCurrencyInstance();
                     Payment.setText(Money.format(payment));
                     LoanPayments.setText("Month\tPrincipal\tInterest\tEnding  Balance\n");
                     int currentMonth = 0;

                        while(currentMonth < Months)
                             {

                             //create loop calculations
                             MonthlyInterest = (remainingPrincipal * intRate);
                             MonthlyAmt = (payment - MonthlyInterest);
                             remainingPrincipal = (remainingPrincipal - MonthlyAmt);
                             LoanPayments.append((++currentMonth) + "\t" + Money.format(MonthlyAmt) + "\t" + Money.format(MonthlyInterest) + "\t" + Money.format(remainingPrincipal) + "\n");
                             balanceArray[currentMonth] = MonthlyAmt;
                             interestArray[currentMonth] = MonthlyInterest;
                             monthArray[currentMonth] = currentMonth;

                             }

                         }


            public static void main(String[] args)
             {

             MortgageCalculatorGUI9 frame= new MortgageCalculatorGUI9();

             }
 }
Community
  • 1
  • 1
David
  • 31
  • 3
  • 1
    I just searched for 'populate jcombobox from file' and voila, [an answered question](http://stackoverflow.com/questions/3173149/how-do-i-populate-jcombobox-from-a-textfile) - this lack of ability to search is why we are complaining. – Charles Goodwin Jul 22 '11 at 02:15

1 Answers1

1

Have you looked at the JComboBox API for a suitable method? If you start there, you'll likely get the right answer faster than asking in StackOverflow (hint it starts with "add... and ends with ...Item") ;)

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Indeed, there seems to be a lack of self help here. SO isn't supposed to be the place to go if you fail first at the first attempt. – Charles Goodwin Jul 22 '11 at 01:45
  • I am new to java programming and have researched this issue all day. I have gotten this far without to much help to produce a good looking application. Trust me the programmers at work are getting tried of me as well. I have to start some where and do not know where all the resources are yet by the end of this I will. I do thank anyone for the help that they give me and their time I know it valuable. I will try what is suggested and see how far I get. Thank you very much. – David Jul 22 '11 at 01:50
  • Whenever you are using an api ,its always suggested (or preffered) that you go through the documentation of that api(if available) before writing a single line of code... – buch11 Jul 22 '11 at 01:54
  • David, give it a go, actually the method name is addItem, and it should be pretty easy for you to figure out how to use. If not, let us know what doesn't work and we can help you! – Hovercraft Full Of Eels Jul 22 '11 at 01:58
  • That is understandable though, with this class we have been thrown to the wolfes and forced to go it alone. I have research everything that I can but have some issues putting it togather. I am just a newbie trying to get a good grade and be the best programmer that I can after that. At least it is like I do not understand what I am writing or how to code specific things. Must of the time I make it harder than what it is. – David Jul 22 '11 at 02:00