Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JRadioButton and JButton

807600Aug 17 2007 — edited Aug 17 2007
How can I make it so that when a user presses one of the radio buttons and clicks the "calculate" button, the calculation gets displayed?

Here is my code:
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.text.DecimalFormat;
import javax.swing.*;        


public class MortgageCalc extends javax.swing.JFrame
{

  public MortgageCalc()
  {
  MortgageCalc();
  }
  
  private void MortgageCalc()
  {
  
  /*From http://java.sun.com/docs/books/tutorial/uiswing/learn/index.html*/
  /*Fields for user to input data and labels on the side of the fields*/
  
  //Amount of mortgage
  loanAmountTextField = new javax.swing.JTextField(); 
  loanAmountLabel = new javax.swing.JLabel();

  //Button 1: 7 years at 5.35% 
  JRadioButton radioButton = new JRadioButton();
  radioButton.setMnemonic(KeyEvent.VK_A);

  radioButton.addActionListener(new java.awt.event.ActionListener()
   {
    public void actionPerformed(java.awt.event.ActionEvent evt)
    { radioButtonActionPerformed(evt); }
   });
  //Puts a label next to button 1
  radioLabel = new javax.swing.JLabel();

  //Button 2: 15 years at 5.5% 
  JRadioButton radioBButton = new JRadioButton();
  radioBButton.setMnemonic(KeyEvent.VK_B);

  radioBButton.addActionListener(new java.awt.event.ActionListener()
   {
    public void actionPerformed(java.awt.event.ActionEvent evt)
    { radioBButtonActionPerformed(evt); }
   });
  //Puts a label next to button 2
  radioBLabel = new javax.swing.JLabel();
   
  //Calculate
  calcButton = new javax.swing.JButton();
  calcButton.setText("Calculate");
  calcButton.addActionListener(new java.awt.event.ActionListener()
   {
    public void actionPerformed(java.awt.event.ActionEvent evt)
    { calcButtonActionPerformed(evt); }
   });

  //Reset
  resetButton = new javax.swing.JButton();
  resetButton.setText("Reset form");
  resetButton.addActionListener(new java.awt.event.ActionListener()
   {
    public void actionPerformed(java.awt.event.ActionEvent evt)
    { resetButtonActionPerformed(evt); }
   });
   
  //Quit
  quitButton = new javax.swing.JButton();
  quitButton.setText("Quit");
  quitButton.addActionListener(new java.awt.event.ActionListener()
   {
    public void actionPerformed(java.awt.event.ActionEvent evt)
    { quitButtonActionPerformed(evt); }
   });
   
  //Monthly payment amount display
  monthlyPaymentLabel = new javax.swing.JLabel();
    
  //Activates the default close button
  setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  
  //Title
  setTitle("Mortgage Calculator v2.3");
  
  loanAmountLabel.setText("Amount of Mortgage (no commas)");
  //monthsLabel.setText("Term of Mortgage (in years)");
  //interestLabel.setText("% interest rate");

  radioLabel.setText("7 years at 5.35%");
  radioBLabel.setText("15 years at 5.5%");
  
  monthlyPaymentLabel.setText("Monthly payment amount");
  

  
  //Layout of buttons using 
  //http://java.sun.com/docs/books/tutorial/uiswing/examples/learn/
  //CelsiusConverterProject/src/learn/CelsiusConverterGUI.java
  //as my template

  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
                                       getContentPane());
  
  getContentPane().setLayout(layout);
  layout.setHorizontalGroup(  //Horizontal positioning
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
      .addContainerGap()
      .addGroup(layout.createParallelGroup(
      javax.swing.GroupLayout.Alignment.LEADING)

        //Loan amount field and label position
        .addGroup(layout.createSequentialGroup()
          .addComponent(
            loanAmountTextField, 
            javax.swing.GroupLayout.PREFERRED_SIZE, 
            javax.swing.GroupLayout.DEFAULT_SIZE, 
            javax.swing.GroupLayout.PREFERRED_SIZE)
          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
          .addComponent(loanAmountLabel))
        
        //Button 1
        .addGroup(layout.createSequentialGroup()
          .addComponent(radioButton)
          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
          .addComponent(radioLabel))

        //Button 2
        .addGroup(layout.createSequentialGroup()
          .addComponent(radioBButton)
          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
          .addComponent(radioBLabel))
          
        //Calculate button            
        .addGroup(layout.createSequentialGroup()
          .addComponent(calcButton)
          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
          .addComponent(monthlyPaymentLabel))
        
        //Reset button            
        .addGroup(layout.createSequentialGroup()
          .addComponent(resetButton)
          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
          .addComponent(monthlyPaymentLabel))
          
        //Quit button            
        .addGroup(layout.createSequentialGroup()
          .addComponent(quitButton)
          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
          .addComponent(monthlyPaymentLabel)))
          
      .addContainerGap(30, Short.MAX_VALUE)) //length
  );
  
  layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, 
                  new java.awt.Component[] 
                  {
                  radioBButton,
                  radioButton, 
                  resetButton, 
                  quitButton, 
                  calcButton, 
                  loanAmountTextField
                  });
                  //Took out monthsTextField, interestTextField

  layout.setVerticalGroup( //Vertical positioning
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
      .addContainerGap()
      //Loan amount field and label position
      .addGroup(layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.BASELINE)
        .addComponent(loanAmountTextField, 
                      javax.swing.GroupLayout.PREFERRED_SIZE, 
                      javax.swing.GroupLayout.DEFAULT_SIZE, 
                      javax.swing.GroupLayout.PREFERRED_SIZE)
        .addComponent(loanAmountLabel))
        
      //Button 1
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
      .addGroup(layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.BASELINE)
        .addComponent(radioButton, 
                      javax.swing.GroupLayout.PREFERRED_SIZE, 
                      javax.swing.GroupLayout.DEFAULT_SIZE, 
                      javax.swing.GroupLayout.PREFERRED_SIZE)
        .addComponent(radioLabel))

      //Button 2
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
      .addGroup(layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.BASELINE)
        .addComponent(radioBButton, 
                      javax.swing.GroupLayout.PREFERRED_SIZE, 
                      javax.swing.GroupLayout.DEFAULT_SIZE, 
                      javax.swing.GroupLayout.PREFERRED_SIZE)
        .addComponent(radioBLabel))

                   
      //Calculate button
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
      .addGroup(layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.BASELINE)
        .addComponent(calcButton)
        .addComponent(monthlyPaymentLabel))

      //Reset button
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
      .addGroup(layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.BASELINE)
        .addComponent(resetButton))
        
      //Quit button
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
      .addGroup(layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.BASELINE)
        .addComponent(quitButton))
        
    .addContainerGap(20, Short.MAX_VALUE)) //height
    );
    pack();  
    
  
  }

  //Button 1 action
  private void radioButtonActionPerformed(java.awt.event.ActionEvent evt) 
  {
  double months = 84;
  double interest = .0535;
  }

  //Button 2 action
  private void radioBButtonActionPerformed(java.awt.event.ActionEvent evt) 
  {
  double months = 180;
  double interest = .055;
  }

  //Calculation action
  private void calcButtonActionPerformed(java.awt.event.ActionEvent evt) 
  {
  
  //DecimalFormat derived from http://forum.java.sun.com/
  //to make no more than two decimal places. 
  DecimalFormat df = new DecimalFormat("0.##");  

  //My calcuations
  double loanAmount = (Double.parseDouble(loanAmountTextField.getText()) );
  
  double monthlyAmount = 
  (loanAmount * (interest/12.0))/(1 - 1 /Math.pow((1 + interest/12.0), months));
  
  monthlyPaymentLabel.setText(df.format(monthlyAmount)
                          + " Monthly payment amount");  //output
  }
  
  //Reset action
  private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) 
  {
  new MortgageCalc().setVisible(true);
  }
  
  //Quit action
  private void quitButtonActionPerformed(java.awt.event.ActionEvent evt) 
  {
  System.exit(0);
  }  
  
  //Run the program
  public static void main(String args[])
  {
  java.awt.EventQueue.invokeLater(new Runnable()
    {
    public void run()
      {
      new MortgageCalc().setVisible(true);
      }
    });
  }
    
    //variables
    private javax.swing.JLabel loanAmountLabel;
    //private javax.swing.JLabel monthsLabel;
    //private javax.swing.JLabel interestLabel;

    private javax.swing.JButton calcButton;
    
    private javax.swing.JRadioButton radioButton;
    private javax.swing.JRadioButton radioBButton;
    
    private javax.swing.JLabel radioLabel;
    private javax.swing.JLabel radioBLabel;
    
    private javax.swing.JLabel monthlyPaymentLabel;
    private javax.swing.JTextField loanAmountTextField;
    
    private javax.swing.JButton quitButton;
    private javax.swing.JButton resetButton;
    private double months;
    private double interest;
    
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 14 2007
Added on Aug 17 2007
10 comments
382 views