Radio Button Help

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();
     }
}
boyerreneeAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
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
 
objectsCommented:
specifically what is the problem you are having?
0
 
boyerreneeAuthor Commented:
It does not recoginize the x value assigned.  Thjere for does not calculate the loan correctly
0
 
objectsCommented:
> public void buttonactionPerformed(ActionEvent select){

that should be:

public void actionPerformed(ActionEvent select){
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.