4 Replies - 17851 Views - Last Post: 24 February 2009 - 07:36 PM Rate Topic: -----

#1 mmelliott  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 26-June 08

Combo Box Array

Posted 23 February 2009 - 09:02 PM

I need a little help here. I was wondering if someone could help me by showing me an example of how to use an Array in a combobox? I don't have any code to display because I don't even know where to start. I do know how to call an Array


int[] term = new int[3];

	 term[0] = 7;
	 term[1] = 15;
	 term[2] = 30;



And how to create a combo box in Java
String course[] = {" 7 "," 15 "," 30 "};
	panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
	combo = new JComboBox(course);
	panel.add(combo);
	panel.add(new JLabel(" years"));
	contentPane.add(panel);



But what I need to do is have that Array be the selections in the combo box.

Any help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Combo Box Array

#2 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Combo Box Array

Posted 23 February 2009 - 09:14 PM

Do you know how to use an array?
Was This Post Helpful? 0
  • +
  • -

#3 mmelliott  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 26-June 08

Re: Combo Box Array

Posted 23 February 2009 - 09:23 PM

I created an Array in my last class. Here is the code that I wrote in that class.

import java.text.DecimalFormat; //imports java.text.DecimalFromat
import java.lang.Math; // Math library
import java.io.*; //java input output
import java.util.Date; //date format/constructor

public class MortageCalculatorWK5 // Class Headder
{
	public static void main(String[] args) throws IOException //main Method
	{
		int ammount;		// Identifies ammount as an int.
		DecimalFormat decimalPlaces=new DecimalFormat("0.00"); // Formats numbers into decimals with 2 decimal places.

		ammount=200000;  // hard codes ammount

		//Aray for intrest
		double[] intrest = new double[3];

		intrest[0] = .0535;
		intrest[1] = .0550;
		intrest[2] = .0575;

		//Aray for term
		int[] term = new int[3];

		term[0] = 7;
		term[1] = 15;
		term[2] = 30;

		//Aray for Payment
		double[] payment = new double[3];

		payment[0] = (ammount*((intrest[0]/12)/(1-Math.pow((1+(intrest[0]/12)),-(term[0]*12)))));
		payment[1] = (ammount*((intrest[1]/12)/(1-Math.pow((1+(intrest[1]/12)),-(term[1]*12)))));
		payment[2] = (ammount*((intrest[2]/12)/(1-Math.pow((1+(intrest[2]/12)),-(term[2]*12)))));

		//Main body display.
		System.out.println();
		PrintMessage("\t\t\t************************************",0);
		PrintMessage("\t\t\tWelcome to Mike's Mortage Calculator",0);
		PrintMessage("\t\t\t************************************",0);
		PrintMessage("",3000);
		PrintMessage("Hello, What is the ammount you wish to finance?\n\n",3000);
		PrintMessage("\t\t$"+ decimalPlaces.format(ammount),0);
		PrintMessage("",3000);
		PrintMessage("How many years would you like to finance this loan for?\n\n",1000);
		PrintMessage("The avaliable terms are: ",0);
		PrintMessage("\t"+term[0]+"years",0);
		PrintMessage("\t"+term[1]+"years",0);
		PrintMessage("\t"+term[2]+"years\n\n",3000);
		PrintMessage("\t\tCan you show me payments for all 3?\n\n",2000);
		PrintMessage("Sure... Let me figure this out Please Wait.  \n",1000);
		PrintMessage("**",1000);
		PrintMessage("***",1000);
		PrintMessage("*****",1000);
		PrintMessage("*******",1000);
		PrintMessage("******",1000);
		PrintMessage("*****",1000);
		PrintMessage("****",1000);
		PrintMessage("***",1000);
		PrintMessage("**\n",1000);

		//Displays the intrest rate and term
		PrintMessage("================================",0);
		PrintMessage("OK your intrest rates will be: ",0);
		PrintMessage(term[0]+" years ----> "+intrest[0]*100+"%",0);
		PrintMessage(term[1]+"years ----> "+intrest[1]*100+"0%",0);
		PrintMessage(term[2]+"years ----> "+intrest[2]*100+"%",0);
		PrintMessage("================================\n\n",3000);

		//Displays payment for all 3 terms and intrest rates.
		PrintMessage("================================",0);
		PrintMessage("Payments:",0);
		PrintMessage(term[0]+"  years at "+intrest[0]*100+"% APR -----> $"+decimalPlaces.format(payment[0]),0);
		PrintMessage(term[1]+"  years at "+intrest[1]*100+"% APR -----> $"+decimalPlaces.format(payment[1]),0);
		PrintMessage(term[2]+" years at "+intrest[2]*100+"% APR -----> $"+decimalPlaces.format(payment[2]),0);
		PrintMessage("================================\n\n",4000);

	//Declares and builds new variables
			double loanBalance0 = 0;
			double loanBalance1 = 0;
			double loanBalance2 = 0;

			double interestPaid0 = 0;
			double interestPaid1 = 0;
			double interestPaid2 = 0;

			double reducePayment0 = 0;
			double reducePayment1 = 0;
			double reducePayment2 = 0;
	// For demo purposes the list will slowly display 10 lines then rapidly move throught the rest of the figures to the end
			int lineCount = 10;
			loanBalance0 = ammount - payment[0];
			loanBalance1 = ammount - payment[1];
			  loanBalance2 = ammount - payment[2];

			PrintMessage("==============================================================================",0);
			PrintMessage("The Following is a list of the monthly payments and the intrest paied monthly.",0);
			PrintMessage("==============================================================================\n\n",2000);

			//Starts loop statement,and declares formula for loan balance and interest paid
			while (loanBalance2 > 0) {

			//Displays the loan balance and interest paid
			System.out.println("The loan balance is: $" + decimalPlaces.format(loanBalance0)+" The intrest paid on the loan is: $"+ decimalPlaces.format(interestPaid0));
			System.out.println("The loan balance is: $" + decimalPlaces.format(loanBalance1)+" The intrest paid on the loan is: $"+ decimalPlaces.format(interestPaid1));
			System.out.println("The loan balance is: $" + decimalPlaces.format(loanBalance2)+" The intrest paid on the loan is: $"+ decimalPlaces.format(interestPaid2)+"\n");
			System.out.println();

			reducePayment0 = payment[0] - interestPaid0;
			reducePayment1 = payment[1] - interestPaid1;
			reducePayment2 = payment[2] - interestPaid2;

			loanBalance0 = loanBalance0 - reducePayment0;
			loanBalance1 = loanBalance1 - reducePayment1;
			loanBalance2 = loanBalance2 - reducePayment2;

			interestPaid0 = (loanBalance0/12 * (intrest[0]));
			interestPaid1 = (loanBalance1/12 * (intrest[1]));
			interestPaid2 = (loanBalance2/12 * (intrest[2]));

			//Pauses screen
			if (lineCount > 0) {
			lineCount--;
			SetPause(1000);
			}
			}
			//Stops loop statement

			if (loanBalance2 <= 0) {
					System.out.println("The loan balance is: $0.00");
			}

 	//Program Closing Message
		PrintMessage("How many years would you like to finance for?\n\n",1500);
		PrintMessage("\t\t30 Years please\n\n",2000);
		PrintMessage(" Ok lets review the terms of your new loan\n",0);
		PrintMessage("================================",0);
		PrintMessage("You will be financing $"+decimalPlaces.format(ammount)+" for "+term[2]+" years\n",0);
		PrintMessage("You will have "+term[2]*12+" payments of "+decimalPlaces.format(payment[2])+" per month",0);
		PrintMessage("================================\n\n",3000);

		PrintMessage("Do you agree to these terms?\n\n",1500);

		PrintMessage("\t\tYES!!!\n\n",0);
		PrintMessage("OK!!!  You will recieve the paperwork in the mail in a couple of days\n\n",1000);

		PrintMessage("Thank you for using Mikes Mortage Calculator \n Have a great day!!!\n\n",0);

}

	//Method for program pause
	public static void SetPause(int iTime)
	{
		 try{
			Thread.sleep(iTime);
		 }
		 catch(Exception e)
		 {
			e.printStackTrace();
		 }
	}
	//Method for print
	public static void PrintMessage(String strVal, int iDelay)
	{
			System.out.println(strVal); // print out the message.....
				if(iDelay > 0) // if delay is 0 then you skip the pause.....
		{
				SetPause(iDelay);
		}
	}
}



Here is the code that I wrote for the first assignment in this current class.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;


public class Mortgage extends JFrame implements ActionListener {
  //private JTextField term;
  //private JTextField rate;
  private JTextField amount;
  private JLabel pmt;
  private JComboBox combo;

  /**
   * This is the constructor that creates the GUI.
   */
  public Mortgage() {
	// call the JFrame constructor
	super("Mike's Mortgage Calculator");

	// set the layout to GridLayout
	JPanel contentPane = new JPanel(new GridLayout(5, 2, 5, 5));
	// add a border of 5 pixels
	contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

	contentPane.add(new JLabel("Loan amount:"));

	// left alignment
	JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));

	// input fields
	panel.add(new JLabel("$"));
	amount = new JTextField(10);
	panel.add(amount);
	contentPane.add(panel);

	contentPane.add(new JLabel("Length of loan:"));

	String course[] = {" 7 "," 15 "," 30 "};
	panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
	combo = new JComboBox(course);
	panel.add(combo);
	panel.add(new JLabel(" years"));
	contentPane.add(panel);

	contentPane.add(new JLabel("Monthly payment: "));

	panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
	pmt = new JLabel(" ");
	panel.add(pmt);
	contentPane.add(panel);

	// buttons

	JButton compute = new JButton("Compute Payment");
	compute.addActionListener(this);
	contentPane.add(compute);

	JButton reset = new JButton("Clear");
	reset.addActionListener(this);
	contentPane.add(reset);

	// set the content pane
	setContentPane(contentPane);
  }

  /**
   * This is the main method. It creates a JFrame and
   * sets the content pane to a Mortgage object
   */
  public static void main(String[] args) {
	Mortgage m = new Mortgage();

	//Aray for intrest
	  double[] rate = new double[3];

		  rate[0] = .0535;
		  rate[1] = .0550;
		  rate[2] = .0575;

	// exit on close
	m.setDefaultCloseOperation(EXIT_ON_CLOSE);
	// set window size to fit all components
	m.pack();
	// display the UI
	m.setVisible(true);
  }

  /**
   * This method is called when a button is pressed.
   */
  public void actionPerformed(ActionEvent ev) {
	// clear button?
	String cmd = ev.getActionCommand();
	if (cmd.equals("Clear")) {
	  amount.setText("");
	  pmt.setText("");
	}

	// compute button?
	else if (cmd.equals("Compute Payment")) {
	  try {
		// parse the text into numbers
		double loanAmount = Double.parseDouble(amount.getText ());
		//int termMonths = Integer.parseInt(term.getText ()) * 12;
		//double interestRate = Double.parseDouble(rate.getText ()) / (12*100);

		// compute payment
		//double pmtRate = loanAmount * (interestRate / (1.0 - Math.pow (1.0 + interestRate, -termMonths)));

		// use a number formatter to display the numbers with two digits
		java.text.NumberFormat nf = java.text.NumberFormat.getCurrencyInstance();

		// display the rate
		pmt.setForeground(Color.blue);
		//pmt.setText(nf.format(pmtRate));
	  }
	  catch (NumberFormatException ex) {
		// we're here if we typed something else than numbers
		pmt.setForeground(Color.red);
		pmt.setText("Input Error");
	  }
	}
  }
}



I'm not trying to get someone to do my homework I just don't get the concept on how to drill the array into the combo box.
Was This Post Helpful? 0
  • +
  • -

#4 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Combo Box Array

Posted 23 February 2009 - 09:29 PM

Do you know how to loop through the elements of an array?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Combo Box Array

Posted 24 February 2009 - 07:36 PM

int[] term = {5,10,15};
JComboBox combo = new JComboBox();
for(int i = 0; i < term.length; i++)
	 combo.addItem(new Integer(term[i]));
	 // may be since 1.5 this will also work but the previous line will work for sure
	 combo.addItem(term[i]);


Was This Post Helpful? 1

Page 1 of 1