Solved

Radio Button Help

Posted on 2004-09-06
4
163 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

919 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now