Radio Buttons

Apply logic to them

Page 1 of 1

3 Replies - 1895 Views - Last Post: 31 July 2009 - 04:51 PM Rate Topic: -----

#1 alexnguyen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 17-March 08

Radio Buttons

Posted 31 July 2009 - 03:52 PM

I have everything working on this monthly car payment GUI except the radio buttons. How do I get them to register the different interest rates? Right now, more than one can be selected. Do I need to set the radio button group up in another class?

/*
 * CarLoanPaymentCalculator.java
 * By: Phuong Nguyen
 * Created on Jul 29, 2009, 5:49:36 PM
 */

public class CarLoanPaymentCalculator extends javax.swing.JFrame
{
	// Creates new form CarLoanPaymentCalculator
	public CarLoanPaymentCalculator()
	{
		initComponents();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		PriceOfCarLabel = new javax.swing.JLabel();
		DownPaymentLabel = new javax.swing.JLabel();
		InterestRateLabel = new javax.swing.JLabel();
		LengthOfLoanInYearsLabel = new javax.swing.JLabel();
		PriceOfCarTextField = new javax.swing.JTextField();
		DownPaymentTextField = new javax.swing.JTextField();
		LengthOfLoanInYearsTextField = new javax.swing.JTextField();
		CalculateMonthlyPaymentButton = new javax.swing.JButton();
		MonthlyPaymentLabel = new javax.swing.JLabel();
		MonthlyPaymentTextField = new javax.swing.JTextField();
		GreatCreditRadioButton = new javax.swing.JRadioButton();
		AverageCreditRadioButton = new javax.swing.JRadioButton();
		PoorCreditRadioButton = new javax.swing.JRadioButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("Car Loan Payment Calculator");
		setBackground(new java.awt.Color(0, 153, 153));
		setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

		PriceOfCarLabel.setText("Price of Car:");

		DownPaymentLabel.setText("Down Payment: ");

		InterestRateLabel.setText("Interest Rate:");

		LengthOfLoanInYearsLabel.setText("Length of Loan in Years:");

		CalculateMonthlyPaymentButton.setText("Calculate Monthly Payment");
		CalculateMonthlyPaymentButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				CalculateMonthlyPaymentButtonActionPerformed(evt);
			}
		});

		MonthlyPaymentLabel.setText("Monthly Payment:");

		GreatCreditRadioButton.setText("Great Credit");

		AverageCreditRadioButton.setText("Average Credit");

		PoorCreditRadioButton.setText("Poor Credit");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addGap(52, 52, 52)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
					.addGroup(layout.createSequentialGroup()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
							.addComponent(DownPaymentLabel)
							.addComponent(PriceOfCarLabel)
							.addComponent(InterestRateLabel))
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
							.addGroup(layout.createSequentialGroup()
								.addGap(10, 10, 10)
								.addComponent(GreatCreditRadioButton))
							.addComponent(PriceOfCarTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
							.addComponent(DownPaymentTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE))
						.addGap(18, 18, 18)
						.addComponent(AverageCreditRadioButton)
						.addGap(18, 18, 18)
						.addComponent(PoorCreditRadioButton)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
					.addGroup(layout.createSequentialGroup()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
							.addComponent(LengthOfLoanInYearsLabel)
							.addComponent(MonthlyPaymentLabel))
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
							.addGroup(layout.createSequentialGroup()
								.addGap(35, 35, 35)
								.addComponent(LengthOfLoanInYearsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
							.addGroup(layout.createSequentialGroup()
								.addGap(4, 4, 4)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
									.addComponent(MonthlyPaymentTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
									.addComponent(CalculateMonthlyPaymentButton))))))
				.addGap(202, 202, 202))
		);
		layout.setVerticalGroup(
			layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
			.addGroup(layout.createSequentialGroup()
				.addGap(43, 43, 43)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(PriceOfCarLabel)
					.addComponent(PriceOfCarTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
				.addGap(18, 18, 18)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(DownPaymentLabel)
					.addComponent(DownPaymentTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
				.addGap(18, 18, 18)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
					.addComponent(InterestRateLabel)
					.addComponent(GreatCreditRadioButton)
					.addComponent(AverageCreditRadioButton)
					.addComponent(PoorCreditRadioButton))
				.addGap(10, 10, 10)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
					.addComponent(LengthOfLoanInYearsLabel)
					.addComponent(LengthOfLoanInYearsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
				.addGap(18, 18, 18)
				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
					.addGroup(layout.createSequentialGroup()
						.addComponent(CalculateMonthlyPaymentButton)
						.addGap(18, 18, 18)
						.addComponent(MonthlyPaymentTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
					.addComponent(MonthlyPaymentLabel))
				.addContainerGap(49, Short.MAX_VALUE))
		);

		pack();
	}// </editor-fold>

	private void CalculateMonthlyPaymentButtonActionPerformed(java.awt.event.ActionEvent evt) {															  
		// declare double variables
		double priceOfCar, downPayment, interestRate = 0.0, lengthOfLoanInYears, monthlyPayment;
		priceOfCar = Double.parseDouble( PriceOfCarTextField.getText() );
		downPayment = Double.parseDouble( DownPaymentTextField.getText() );
		lengthOfLoanInYears = Double.parseDouble( LengthOfLoanInYearsTextField.getText() );

		if( GreatCreditRadioButton.hasFocus() )
			interestRate = .05;
		if( AverageCreditRadioButton.hasFocus() )
			interestRate = .1;
		if( PoorCreditRadioButton.hasFocus() )
			interestRate = .12;

		monthlyPayment = ( (priceOfCar - downPayment) + ( priceOfCar - downPayment ) * interestRate )  / ( lengthOfLoanInYears * 12);

		MonthlyPaymentTextField.setText( String.valueOf(monthlyPayment) );
}															 

	/**
	* @param args the command line arguments
	*/
	public static void main(String args[])
	{
		java.awt.EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				new CarLoanPaymentCalculator().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify
	private javax.swing.JRadioButton AverageCreditRadioButton;
	private javax.swing.JButton CalculateMonthlyPaymentButton;
	private javax.swing.JLabel DownPaymentLabel;
	private javax.swing.JTextField DownPaymentTextField;
	private javax.swing.JRadioButton GreatCreditRadioButton;
	private javax.swing.JLabel InterestRateLabel;
	private javax.swing.JLabel LengthOfLoanInYearsLabel;
	private javax.swing.JTextField LengthOfLoanInYearsTextField;
	private javax.swing.JLabel MonthlyPaymentLabel;
	private javax.swing.JTextField MonthlyPaymentTextField;
	private javax.swing.JRadioButton PoorCreditRadioButton;
	private javax.swing.JLabel PriceOfCarLabel;
	private javax.swing.JTextField PriceOfCarTextField;
	// End of variables declaration

}



Is This A Good Question/Topic? 0
  • +

Replies To: Radio Buttons

#2 pbl   User is offline

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

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Radio Buttons

Posted 31 July 2009 - 03:58 PM

This is not a Java issue
it is an issue with you falling to use correctly your GUI generator
There is no ButtonGroup in your code
If you want RadioButton unicity you have to register (add) them to a ButtonGroup
Was This Post Helpful? 0
  • +
  • -

#3 alexnguyen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 17-March 08

Re: Radio Buttons

Posted 31 July 2009 - 04:09 PM

View Postpbl, on 31 Jul, 2009 - 02:58 PM, said:

This is not a Java issue
it is an issue with you falling to use correctly your GUI generator
There is no ButtonGroup in your code
If you want RadioButton unicity you have to register (add) them to a ButtonGroup


I am using NetBeans to create the GUI and it won't let me edit the code for the JRadioButtons...Can I go around it somehow in NetBeans?
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

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

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Radio Buttons

Posted 31 July 2009 - 04:51 PM

View Postalexnguyen, on 31 Jul, 2009 - 03:09 PM, said:

[I am using NetBeans to create the GUI and it won't let me edit the code for the JRadioButtons...Can I go around it somehow in NetBeans?

Don't use a GUI generator
Write your code "by hand" then you can do everything you want and understand what you are doing
GUI generators generate (this is their job) hundreds of lines of code hardly understandable by human beings

Can be usefull for a pilot or a proof of concepts but if you have to maintain the code generated from them.... stay away from them

Are you seriously asking us to try to decipher the hundreds of lines of code generated by a GUI generator to find out where you missed to clik on an option in the generator to group together some radio buttons ?

Write your own GUI (that will use 1/10 of these generated lines of code) and we will be glad to help
We are here to fix human errors not GUI generator errors

This post has been edited by pbl: 31 July 2009 - 05:10 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1