13 Replies - 763 Views - Last Post: 15 November 2011 - 10:08 PM Rate Topic: -----

#1 badjervid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 13-November 11

Combo box not working

Posted 15 November 2011 - 08:56 PM

Please help I can not get my interest rate list to appear in my combo box.

package MortgageCalculator;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.*;
import java.util.*;

public class MortgageCalculator  implements ActionListener
{	//field to get user input
	JTextField principalTextBox;
	JComboBox termsTextBox;
	JScrollPane scroller;//scroller to scroll the result area
     
	public void calculate() //Method To calculate the result
	{
		try {
			DecimalFormat moneyFormatter = new DecimalFormat("$#,###.00");  //sets the format of output
			//to hold balance
                        String bal = principalTextBox.getText();
                        if(bal.indexOf("$")==0)
                            bal = bal.substring(1);
                        double balance=0;
                        try {
                        balance = Double.parseDouble(bal);  //convert text bos data to double data type
                        }catch(Exception e){
                            JOptionPane.showMessageDialog(principalTextBox, "Please provide numeric value");
                            return;
                        }
                        double initAmt = balance;
			int months = 84; //To hold months
                        //array for the mortgage data for the different loans.
                        double interestRates[] = { 0.0535,0.05,0.0565,0.575};
                        int numMonths[] = {84,120,240,360};
			//to hold interest
			double interest = 0.0535;

                        //user selection
                        months = numMonths[termsTextBox.getSelectedIndex()];
			interest = interestRates[termsTextBox.getSelectedIndex()];

			//Headings for grid
			String[] columnNames = {"Month","Payment","Principal Paid","Interest Paid","Total Interest","Balance"}; // Data Grid headings
			Object[][] data = new Object[months][6];
			//calculating monthly interest
			double monthlyInterest = interest / 12.0;
			double totalInterest = 0.0;
			 //calculating interests and moth payments
			double monthlyPayment = balance * ((monthlyInterest * Math.pow(1.0 + monthlyInterest, months)) / (Math.pow(1.0 + monthlyInterest, months) - 1.0));
			for (int j = 0; j < months; j++)
			{
				double interestThisPeriod = balance * monthlyInterest;
				totalInterest = totalInterest + interestThisPeriod;
				balance = balance - monthlyPayment + interestThisPeriod;
				data[j][0] = ""+(j+1);
				data[j][1] = moneyFormatter.format(monthlyPayment);
				data[j][2] = moneyFormatter.format(monthlyPayment-interestThisPeriod);
				data[j][3] = moneyFormatter.format(interestThisPeriod);
				data[j][4] = moneyFormatter.format(totalInterest);
				data[j][5] = moneyFormatter.format(balance);
			}
			//Showing result in table
			JTable table = new JTable(data, columnNames);
			//Adding table to scroller so that user can scroll the results
			scroller.setViewportView(table);
                        //calculating values and creating object of JP to display chart
               double total = initAmt + totalInterest;
               double mortgagePercent = initAmt / total;
               double interestPercent = totalInterest / total;
         
		} catch (Exception e) {}
	}

	public void addComponentsToPane(Container contentPane) //All view
	{
		// Any number of rows and 2 columns
		contentPane.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		//Adding controls on frame
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		contentPane.add(new JLabel("Principal:"), c);

		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 0;
		principalTextBox = new JTextField();
		contentPane.add(principalTextBox, c);

		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 1;
		contentPane.add(new JLabel("Terms:"), c);
		termsTextBox = new JComboBox();
                 Scanner fileScanner = null;
       
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 1;
		contentPane.add(termsTextBox, c);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 2;
		JButton calcButton = new JButton("Calculate");
		JButton reset = new JButton("Reset");
                JPanel jp = new JPanel();

                jp.add(calcButton);jp.add(reset);
                calcButton.addActionListener(this);
                JButton exit = new JButton("Exit");
                 jp.add(exit);
                 exit.addActionListener(this);
                reset.addActionListener(this);

		contentPane.add(jp, c);

		scroller = new JScrollPane();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 3;
		c.gridwidth = 2;
		c.gridheight = 2;
		//Showing table with values 0 on the frame
		String[] columnNames = {"Month","Payment","Principal Paid","Interest Paid","Total Interest","Balance"};
		Object[][] data = { {"0","0","0","0","0","0"} };
		JTable table = new JTable(data, columnNames);
		scroller.setViewportView(table);
		contentPane.add(scroller, c);
	}
	public void createAndShowGUI()
	{
		JFrame frame = new JFrame("Mortgage Calculator");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//Set up the content pane and components in GridLayout
		addComponentsToPane(frame.getContentPane());
		frame.pack();
		frame.setVisible(true);
	}
	public static void main(String[] args)
	{
		final MortgageCalculator  am = new MortgageCalculator ();
		javax.swing.SwingUtilities.invokeLater(new Runnable()
		{
            @Override
			public void run()
			{
				am.createAndShowGUI();
			}
		});
	}
	//When the use clicks Calculate buttin, this method will be invoked.
    @Override
	public void actionPerformed(ActionEvent e)
	{
                String cmd = e.getActionCommand();
                if(cmd.equalsIgnoreCase("Exit"))
                {
                    System.exit(1);
                }
                if(cmd.equalsIgnoreCase("Reset"))
                {
                    principalTextBox.setText("");
                    String[] columnNames = {"Month","Payment","Principal Paid","Interest Paid","Total Interest","Balance"};
                    Object[][] data = { {"0","0","0","0","0","0"} };
                    JTable table = new JTable(data, columnNames);
                    table = new JTable(data, columnNames);
                    scroller.setViewportView(table);
                    
                }
		calculate();
	}
}






Is This A Good Question/Topic? 0
  • +

Replies To: Combo box not working

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Combo box not working

Posted 15 November 2011 - 09:00 PM

Where do you think you fill it ?
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Combo box not working

Posted 15 November 2011 - 09:00 PM

Where do you think you fill it ?
Was This Post Helpful? 0
  • +
  • -

#4 badjervid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 13-November 11

Re: Combo box not working

Posted 15 November 2011 - 09:04 PM

I am not sure I think after line 30
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Combo box not working

Posted 15 November 2011 - 09:10 PM

don't see any reference to variable termsTextBox in that whole calculate() method :)

You just don't fill it, the only reference to termsTextBox are

	JComboBox termsTextBox;

                        months = numMonths[termsTextBox.getSelectedIndex()];
			interest = interestRates[termsTextBox.getSelectedIndex()];

		termsTextBox = new JComboBox();

		contentPane.add(termsTextBox, c);


Was This Post Helpful? 0
  • +
  • -

#6 badjervid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 13-November 11

Re: Combo box not working

Posted 15 November 2011 - 09:15 PM

I am not quite sure I under stand?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Combo box not working

Posted 15 November 2011 - 09:20 PM

If you want a ComboBox to display:
choice 1
choice 2
choice 3

you'll have to

combo.addItem("choice 1");
combo.addItem("choice 2");
combo.addItem("choice 3");

so you have to build Strings out your different months and interest and add these String into your JComboBox
Was This Post Helpful? 0
  • +
  • -

#8 badjervid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 13-November 11

Re: Combo box not working

Posted 15 November 2011 - 09:29 PM

public class MortgageCalculator  implements ActionListener
{	//field to get user input
	JTextField principalTextBox;
	JComboBox termsTextBox;
	JScrollPane scroller;//scroller to scroll the result area
     
	public void calculate() //Method To calculate the result
	{
		
            
            try {
			DecimalFormat moneyFormatter = new DecimalFormat("$#,###.00");  //sets the format of output
			//to hold balance
                        String bal = principalTextBox.getText();
                        if(bal.indexOf("$")==0)
                            bal = bal.substring(1);
                        double balance=0;
                        try {
                        balance = Double.parseDouble(bal);  //convert text bos data to double data type
                        }catch(Exception e){
                            JOptionPane.showMessageDialog(principalTextBox, "Please provide numeric value");
                            return;
                        }
                        JComboBox termsTextBox;

                       months = numMonths[termsTextBox.getSelectedIndex()];
		interest = interestRates[termsTextBox.getSelectedIndex()];

	termsTextBox = new JComboBox();

	contentPane.add("5.35% 7 years", c);
        contentPane.add("5% 10 years", c);
        contentPane.add("5.65% 15 years", c);
        contentPane.add("5.75% 30 years", c);       
        
                        double initAmt = balance;
			int months = 84; //To hold months
                        //array for the mortgage data for the different loans.
                        double interestRates[] = { 0.0535,0.05,0.0565,0.575};
                        int numMonths[] = {84,120,240,360};
			//to hold interest
			double interest = 0.0535;

                        //user selection
                        months = numMonths[termsTextBox.getSelectedIndex()];
			interest = interestRates[termsTextBox.getSelectedIndex()];

			//Headings for grid


So would this be right?
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Combo box not working

Posted 15 November 2011 - 09:37 PM

termsTextBox.addItem("5.35% 7 years");

but should be done in your constructor not in calculate() which

months = numMonths[termsTextBox.getSelectedIndex()];
uses this statement to determine which index of the comboBox is selected
to have an index selected, it has to have something in it
Was This Post Helpful? 1
  • +
  • -

#10 badjervid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 13-November 11

Re: Combo box not working

Posted 15 November 2011 - 09:43 PM

Where would you insert the the code?

                        JComboBox termsTextBox;

                       months = numMonths[termsTextBox.getSelectedIndex()];
		interest = interestRates[termsTextBox.getSelectedIndex()];

	termsTextBox = new JComboBox();

	termsTextBox.addItem("5.35% 7 years");
        termsTextBox.addItem("5% 10 years");
        termsTextBox.addItem("5.65% 15 years");
        termsTextBox.addItem("5.75% 30 years");

Was This Post Helpful? 0
  • +
  • -

#11 badjervid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 13-November 11

Re: Combo box not working

Posted 15 November 2011 - 09:51 PM

after line 76 would be my guess but I get errors when I do that
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Combo box not working

Posted 15 November 2011 - 09:55 PM

In your addComponentToPane()

You already create the ComboBox

actually if you put

double interestRates[] = { 0.0535,0.05,0.0565,0.575};
int numMonths[] = {84,120,240,360};

in your instance variable you can write

    termsTextBox = new JComboBox();
    for(int i = 0; i < interestsRates.length; i++) {
        String str = String.format("%f.2%% in %d years", interestRates * 100, numMonths / 12);
        termsTextBox.addItem(str);
    }
	c.fill = GridBagConstraints.HORIZONTAL;
	c.gridx = 1;
	c.gridy = 1;
	contentPane.add(termsTextBox, c);


Was This Post Helpful? 0
  • +
  • -

#13 badjervid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 13-November 11

Re: Combo box not working

Posted 15 November 2011 - 10:06 PM

I tried that but I am getting a lot of errors.

xception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: variable interestsRates
location: class MortgageCalculator.MortgageCalculator
at MortgageCalculator.MortgageCalculator.addComponentsToPane(MortgageCalculator.java:87)
at MortgageCalculator.MortgageCalculator.createAndShowGUI(MortgageCalculator.java:149)
at MortgageCalculator.MortgageCalculator$1.run(MortgageCalculator.java:161)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:642)
at java.awt.EventQueue.access$000(EventQueue.java:


package MortgageCalculator;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.*;
import java.util.*;

public class MortgageCalculator  implements ActionListener
{	//field to get user input
	JTextField principalTextBox;
	JComboBox termsTextBox;
	JScrollPane scroller;//scroller to scroll the result area
     
	public void calculate() //Method To calculate the result
	{
		
            
            try {
			DecimalFormat moneyFormatter = new DecimalFormat("$#,###.00");  //sets the format of output
			//to hold balance
                        String bal = principalTextBox.getText();
                        if(bal.indexOf("$")==0)
                            bal = bal.substring(1);
                        double balance=0;
                        try {
                        balance = Double.parseDouble(bal);  //convert text bos data to double data type
                        }catch(Exception e){
                            JOptionPane.showMessageDialog(principalTextBox, "Please provide numeric value");
                            return;
                        }

        
                        double initAmt = balance;
			int months = 84; //To hold months
                        //array for the mortgage data for the different loans.
                        double interestRates[] = { 0.0535,0.05,0.0565,0.575};
                        int numMonths[] = {84,120,240,360};
			//to hold interest
			double interest = 0.0535;

                        //user selection
                        months = numMonths[termsTextBox.getSelectedIndex()];
			interest = interestRates[termsTextBox.getSelectedIndex()];

			//Headings for grid
			String[] columnNames = {"Month","Payment","Principal Paid","Interest Paid","Total Interest","Balance"}; // Data Grid headings
			Object[][] data = new Object[months][6];
			//calculating monthly interest
			double monthlyInterest = interest / 12.0;
			double totalInterest = 0.0;
			 //calculating interests and moth payments
			double monthlyPayment = balance * ((monthlyInterest * Math.pow(1.0 + monthlyInterest, months)) / (Math.pow(1.0 + monthlyInterest, months) - 1.0));
			for (int j = 0; j < months; j++)
			{
				double interestThisPeriod = balance * monthlyInterest;
				totalInterest = totalInterest + interestThisPeriod;
				balance = balance - monthlyPayment + interestThisPeriod;
				data[j][0] = ""+(j+1);
				data[j][1] = moneyFormatter.format(monthlyPayment);
				data[j][2] = moneyFormatter.format(monthlyPayment-interestThisPeriod);
				data[j][3] = moneyFormatter.format(interestThisPeriod);
				data[j][4] = moneyFormatter.format(totalInterest);
				data[j][5] = moneyFormatter.format(balance);
			}
			//Showing result in table
			JTable table = new JTable(data, columnNames);
			//Adding table to scroller so that user can scroll the results
			scroller.setViewportView(table);
                        //calculating values and creating object of JP to display chart
               double total = initAmt + totalInterest;
               double mortgagePercent = initAmt / total;
               double interestPercent = totalInterest / total;
         
		} catch (Exception e) {}
	}

	public void addComponentsToPane(Container contentPane) //All view
	{
            
            
		// Any number of rows and 2 columns
		contentPane.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		   termsTextBox = new JComboBox();
   for(int i = 0; i < interestsRates.length; i++) {
       String str = String.format("%f.2%% in %d years", interestRates * 100, numMonths / 12);
       termsTextBox.addItem(str);
   }
                //Adding controls on frame
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		contentPane.add(new JLabel("Principal:"), c);

		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 0;
		principalTextBox = new JTextField();
		contentPane.add(principalTextBox, c);

		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 1;
		contentPane.add(new JLabel("Terms:"), c);
		termsTextBox = new JComboBox();
                 Scanner fileScanner = null;
       
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 1;
		contentPane.add(termsTextBox, c);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 2;
		JButton calcButton = new JButton("Calculate");
		JButton reset = new JButton("Reset");
                JPanel jp = new JPanel();

                jp.add(calcButton);jp.add(reset);
                calcButton.addActionListener(this);
                JButton exit = new JButton("Exit");
                 jp.add(exit);
                 exit.addActionListener(this);
                reset.addActionListener(this);

		contentPane.add(jp, c);

		scroller = new JScrollPane();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 3;
		c.gridwidth = 2;
		c.gridheight = 2;
		//Showing table with values 0 on the frame
		String[] columnNames = {"Month","Payment","Principal Paid","Interest Paid","Total Interest","Balance"};
		Object[][] data = { {"0","0","0","0","0","0"} };
		JTable table = new JTable(data, columnNames);
		scroller.setViewportView(table);
		contentPane.add(scroller, c);
	}
	public void createAndShowGUI()
	{
		JFrame frame = new JFrame("Mortgage Calculator");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//Set up the content pane and components in GridLayout
		addComponentsToPane(frame.getContentPane());
		frame.pack();
		frame.setVisible(true);
	}
	public static void main(String[] args)
	{
		final MortgageCalculator  am = new MortgageCalculator ();
		javax.swing.SwingUtilities.invokeLater(new Runnable()
		{
            @Override
			public void run()
			{
				am.createAndShowGUI();
			}
		});
	}
	//When the use clicks Calculate buttin, this method will be invoked.
    @Override
	public void actionPerformed(ActionEvent e)
	{
                String cmd = e.getActionCommand();
                if(cmd.equalsIgnoreCase("Exit"))
                {
                    System.exit(1);
                }
                if(cmd.equalsIgnoreCase("Reset"))
                {
                    principalTextBox.setText("");
                    String[] columnNames = {"Month","Payment","Principal Paid","Interest Paid","Total Interest","Balance"};
                    Object[][] data = { {"0","0","0","0","0","0"} };
                    JTable table = new JTable(data, columnNames);
                    table = new JTable(data, columnNames);
                    scroller.setViewportView(table);
                    
                }
		calculate();
	}
}




I am still getting the errors but i realize that was the wrong spot


package MortgageCalculator;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.*;
import java.util.*;

public class MortgageCalculator  implements ActionListener
{	//field to get user input
	JTextField principalTextBox;
	JComboBox termsTextBox;
	JScrollPane scroller;//scroller to scroll the result area
     
	public void calculate() //Method To calculate the result
	{
		
            
            try {
			DecimalFormat moneyFormatter = new DecimalFormat("$#,###.00");  //sets the format of output
			//to hold balance
                        String bal = principalTextBox.getText();
                        if(bal.indexOf("$")==0)
                            bal = bal.substring(1);
                        double balance=0;
                        try {
                        balance = Double.parseDouble(bal);  //convert text bos data to double data type
                        }catch(Exception e){
                            JOptionPane.showMessageDialog(principalTextBox, "Please provide numeric value");
                            return;
                        }

        
                        double initAmt = balance;
			int months = 84; //To hold months
                        //array for the mortgage data for the different loans.
                        double interestRates[] = { 0.0535,0.05,0.0565,0.575};
                        int numMonths[] = {84,120,240,360};
			//to hold interest
			double interest = 0.0535;

                        //user selection
                        months = numMonths[termsTextBox.getSelectedIndex()];
			interest = interestRates[termsTextBox.getSelectedIndex()];

			//Headings for grid
			String[] columnNames = {"Month","Payment","Principal Paid","Interest Paid","Total Interest","Balance"}; // Data Grid headings
			Object[][] data = new Object[months][6];
			//calculating monthly interest
			double monthlyInterest = interest / 12.0;
			double totalInterest = 0.0;
			 //calculating interests and moth payments
			double monthlyPayment = balance * ((monthlyInterest * Math.pow(1.0 + monthlyInterest, months)) / (Math.pow(1.0 + monthlyInterest, months) - 1.0));
			for (int j = 0; j < months; j++)
			{
				double interestThisPeriod = balance * monthlyInterest;
				totalInterest = totalInterest + interestThisPeriod;
				balance = balance - monthlyPayment + interestThisPeriod;
				data[j][0] = ""+(j+1);
				data[j][1] = moneyFormatter.format(monthlyPayment);
				data[j][2] = moneyFormatter.format(monthlyPayment-interestThisPeriod);
				data[j][3] = moneyFormatter.format(interestThisPeriod);
				data[j][4] = moneyFormatter.format(totalInterest);
				data[j][5] = moneyFormatter.format(balance);
			}
			//Showing result in table
			JTable table = new JTable(data, columnNames);
			//Adding table to scroller so that user can scroll the results
			scroller.setViewportView(table);
                        //calculating values and creating object of JP to display chart
               double total = initAmt + totalInterest;
               double mortgagePercent = initAmt / total;
               double interestPercent = totalInterest / total;
         
		} catch (Exception e) {}
	}

	public void addComponentsToPane(Container contentPane) //All view
	{
            
            
		// Any number of rows and 2 columns
		contentPane.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		   
                //Adding controls on frame
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		contentPane.add(new JLabel("Principal:"), c);

		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 0;
		principalTextBox = new JTextField();
		contentPane.add(principalTextBox, c);

		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 1;
		contentPane.add(new JLabel("Terms:"), c);
		termsTextBox = new JComboBox();
                 Scanner fileScanner = null;
       
                 termsTextBox = new JComboBox();
   for(int i = 0; i < interestsRates.length; i++) {
       String str = String.format("%f.2%% in %d years", interestRates * 100, numMonths / 12);
       termsTextBox.addItem(str);
   }
                 
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 1;
		contentPane.add(termsTextBox, c);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 2;
		JButton calcButton = new JButton("Calculate");
		JButton reset = new JButton("Reset");
                JPanel jp = new JPanel();

                jp.add(calcButton);jp.add(reset);
                calcButton.addActionListener(this);
                JButton exit = new JButton("Exit");
                 jp.add(exit);
                 exit.addActionListener(this);
                reset.addActionListener(this);

		contentPane.add(jp, c);

		scroller = new JScrollPane();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 3;
		c.gridwidth = 2;
		c.gridheight = 2;
		//Showing table with values 0 on the frame
		String[] columnNames = {"Month","Payment","Principal Paid","Interest Paid","Total Interest","Balance"};
		Object[][] data = { {"0","0","0","0","0","0"} };
		JTable table = new JTable(data, columnNames);
		scroller.setViewportView(table);
		contentPane.add(scroller, c);
	}
	public void createAndShowGUI()
	{
		JFrame frame = new JFrame("Mortgage Calculator");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//Set up the content pane and components in GridLayout
		addComponentsToPane(frame.getContentPane());
		frame.pack();
		frame.setVisible(true);
	}
	public static void main(String[] args)
	{
		final MortgageCalculator  am = new MortgageCalculator ();
		javax.swing.SwingUtilities.invokeLater(new Runnable()
		{
            @Override
			public void run()
			{
				am.createAndShowGUI();
			}
		});
	}
	//When the use clicks Calculate buttin, this method will be invoked.
    @Override
	public void actionPerformed(ActionEvent e)
	{
                String cmd = e.getActionCommand();
                if(cmd.equalsIgnoreCase("Exit"))
                {
                    System.exit(1);
                }
                if(cmd.equalsIgnoreCase("Reset"))
                {
                    principalTextBox.setText("");
                    String[] columnNames = {"Month","Payment","Principal Paid","Interest Paid","Total Interest","Balance"};
                    Object[][] data = { {"0","0","0","0","0","0"} };
                    JTable table = new JTable(data, columnNames);
                    table = new JTable(data, columnNames);
                    scroller.setViewportView(table);
                    
                }
		calculate();
	}
}



Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Combo box not working

Posted 15 November 2011 - 10:08 PM

As I said, you'll have to move your two arrays into the instance variables so all methods will see them

public class MortgageCalculator  implements ActionListener
{	//field to get user input
	JTextField principalTextBox;
	JComboBox termsTextBox;
	JScrollPane scroller;//scroller to scroll the result area

    // here
    //array for the mortgage data for the different loans.
    double interestRates[] = { 0.0535,0.05,0.0565,0.575};
    int numMonths[] = {84,120,240,360};


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1