Solved

Radio Button Help

Posted on 2004-09-06
4
166 Views
Last Modified: 2010-03-31
I am perplexed with what is going wrong with my radio buttons.  I do not understand why I can not get the array to work within them.  I tryed the Item listerner but kept getting an error...

Please help
here is the code so far


import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.JRadioButton;
import java.io.*;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;

public class LoanApp6 extends JFrame implements ActionListener {

double term = 0;
double principal = 0;
double rate = 0;
double monthlyPayment = 0;
double interest = 0;
int x =0;
   
        double[][]CalcArray = new double[6][6];{
        CalcArray[0][0]=84;
        CalcArray[0][1]=.0535;
        CalcArray[1][0]=180;
        CalcArray[1][1]=.055;
        CalcArray[2][0]=360;
        CalcArray[2][1]=.0575;
               }

NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);

JPanel row1 = new JPanel();
JLabel lbMortgage = new JLabel("MORTGAGE CALCULATOR", JLabel.CENTER);

JPanel row2 = new JPanel(new GridLayout(1, 2));
JLabel lbPrincipal = new JLabel("Mortgage Principal $",JLabel.LEFT);
JTextField txtPrincipal = new JTextField(10);



JPanel radioPanel = new JPanel();
JRadioButton aButton = new JRadioButton("7 Years at 5.35%", true);
JRadioButton bButton = new JRadioButton("15 Years at 5.50%" ,false);
JRadioButton cButton = new JRadioButton("30 Years at 5.75%", false);


JPanel row5 = new JPanel(new GridLayout(1, 2));
JLabel lbPayment = new JLabel("Monthly Payment $", JLabel.LEFT);
JTextField txtPayment = new JTextField(10);

//create buttons
JPanel button = new JPanel(new FlowLayout( FlowLayout.LEFT, 10, 10));
JButton clearButton = new JButton("Clear");
JButton exitButton = new JButton("Exit");
JButton calculateButton = new JButton("Calculate");
JButton displayButton = new JButton("Display Payments");
 
//set textarea to diplay payments
JTextArea displayArea = new JTextArea(10, 45);
JScrollPane scroll = new JScrollPane(displayArea);
 
 
public LoanApp6()
     {
          super ("Mortgage Calculator");
          setSize(550, 420);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          Container pane = getContentPane();
          //GridLayout grid = new GridLayout(10, 4, 10, 10);
          //pane.setLayout(new GridLayout(20, 4, 10, 10));
               
               
          Border rowborder = new EmptyBorder( 3, 10, 3, 10 );
         
          pane.add(row1);
          row1.add(lbMortgage);
          row1.setMaximumSize( new Dimension( 10000, row1.getMinimumSize().height ) );
          row1.setBorder( rowborder );
         
          pane.add(row2);
          row2.add(lbPrincipal);
          row2.add(txtPrincipal);
          row2.setMaximumSize( new Dimension( 10000, row2.getMinimumSize().height ) );
          row2.setBorder( rowborder );
         
     
           ButtonGroup bgroup = new ButtonGroup();      
          bgroup.add(aButton);
          bgroup.add(bButton);
          bgroup.add(cButton);

          radioPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 4, 4 ));      
          radioPanel.add(aButton);
          radioPanel.add(bButton);
          radioPanel.add(cButton);
          pane.add(radioPanel);
          radioPanel.setMaximumSize( new Dimension( 10000, radioPanel.getMinimumSize().height ) );
          radioPanel.setBorder( rowborder );

          pane.add(row5);
          row5.add(lbPayment);
          row5.add(txtPayment);
          txtPayment.setEnabled(false); //payment is set uneditable
          row5.setMaximumSize( new Dimension( 10000, row5.getMinimumSize().height ) );
          row5.setBorder( rowborder );
         
          button.add(calculateButton);
          button.add(clearButton);
          button.add(exitButton);
          button.add(displayButton);
          pane.add(button);
          displayButton.setEnabled(false);
          button.setMaximumSize( new Dimension( 10000, button.getMinimumSize().height ) );
          // button.setBorder( rowborder );
         
         
          scroll.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
          pane.add(scroll);
          // pane.setS
         
                   
          // FlowLayout flo = new FlowLayout(FlowLayout.LEFT,10,10);
          // pane.setLayout(flo);
          pane.setLayout(new BoxLayout( pane, BoxLayout.Y_AXIS ));          
          setVisible(true);
          setContentPane(pane);    
          //pack();
         
          //adds listerns to buttons
          clearButton.addActionListener(this);
          exitButton.addActionListener(this);
          calculateButton.addActionListener(this);
          displayButton.addActionListener(this);
       
         
     
 

            aButton.addActionListener(this);
            bButton.addActionListener(this);
            cButton.addActionListener(this);

         
     }

public void buttonactionPerformed(ActionEvent select){

     Object command = select.getSource();
   
 {  if (command==aButton)  
         x=0;
    else if (command==bButton)
        x=1;
    else if (command==cButton)
         x=2;
 }
}
     
     public void actionPerformed(ActionEvent event){

     Object command = event.getSource();
   
 

     if(command == calculateButton)
     {
          try
          {  
               term = CalcArray[x][0];
               principal = Double.parseDouble(txtPrincipal.getText());
               rate = CalcArray[x][1];
               interest = rate / 100.0 / 12.0;
               monthlyPayment = principal *(interest/(1-Math.pow(interest+1,-term)));          
               
               NumberFormat myCurrencyFormatter;
               myCurrencyFormatter = NumberFormat.getCurrencyInstance(Locale.US);
               txtPayment.setText(myCurrencyFormatter.format(monthlyPayment));
               displayButton.setEnabled(true);
          }    
     
          catch(NumberFormatException e)
          {
               JOptionPane.showMessageDialog(null, "Invaild Entry!  Please Try Again", "ERROR", JOptionPane.ERROR_MESSAGE);
           }
         
         
     }
     
          if(command == clearButton)
          {
             
               txtPayment.setText(null);
               displayArea.setText(null);
          }

          if(command == exitButton)
          {
               System.exit(0);
          }
         
          if(command == displayButton)
          {
               double year = 0;
               double interest = 0;
               double balance = 0;
               double monthlyInterest = 0;
               String titles = "Month \t Principal\t Interest\tBalance\n";
               
               monthlyInterest = (rate / 100) /12;
               balance = principal;
               
               displayArea.setText(titles);
               
             
               for(int counter = 0; counter <= term - 1; counter++)
               {
                    // Calculate payment detail info
                    interest = balance * monthlyInterest;

                    if(interest <= 0)
                         interest = 0;

                    year = monthlyPayment - interest;
                    balance = balance - year;
                NumberFormat currencyFormatter;
              currencyFormatter = NumberFormat.getCurrencyInstance(Locale.US);
                    displayArea.append("   " +(counter + 1) + "\t"
                         + currencyFormatter.format(year) + "\t"
                         + currencyFormatter.format(interest) + "\t"
                         + currencyFormatter.format(balance) + "\n");

                         // This positions the cursor to the top of the textArea. Otherwise, the
                         // textArea would be displayed showing the bottom rows.
                    displayArea.setCaretPosition(0);
     }    

     }
}          

public static void main(String[] arguments) {
     LoanApp6 loan = new LoanApp6();
     }
}
0
Comment
Question by:boyerrenee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 11993270
specifically what is the problem you are having?
0
 

Author Comment

by:boyerrenee
ID: 11993281
It does not recoginize the x value assigned.  Thjere for does not calculate the loan correctly
0
 
LVL 92

Expert Comment

by:objects
ID: 11993288
> public void buttonactionPerformed(ActionEvent select){

that should be:

public void actionPerformed(ActionEvent select){
0
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 11993292
just noticed you've also got an actionPerformed()
so what you need to do is move the following into your actionPerformed and remove buttonactionPerformed

if (command==aButton)  
         x=0;
    else if (command==bButton)
        x=1;
    else if (command==cButton)
         x=2;
 }
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question