Solved

Radio Button Help

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse package explorer vs project explorer view 2 126
Cipher Configuration on Apache HTTPD 4 62
servlet  URL Rewriting 1 36
expectj telnet failing 5 35
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

776 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