?
Solved

Mortgage calculator can't display radio buttons

Posted on 2006-06-07
3
Medium Priority
?
340 Views
Last Modified: 2008-02-01
I am trying to change my mortgage calculator program from a GUI that accepts loan amount, interest rate and term to one that only accepts loan amount and allows you to select a radio button to select one of 3 pre-determined rates and terms. I have removed references to my other input data items and tried to add radio buttons that represent each loan. While I haven't written any code for the buttons yet, I was trying to get them as a first step to display on my GUI. I added the code for the buttons and group, but when I compile and run I do not see them. Any assistance would be appreciated. In relation to the same issue, is there a way to specify coordinates for the radio buttons as in the label belowZ?
headerJLabel.setBounds(16, 16, 300, 46);

Thanks,
Jon

//Week2 Jan Schab


/* Mortgage Monyhly Payment Calculator with graphical user interface
Programmer:  J.Schab
Date:        June 4,2006
Filename:    Mortgage.java
Purpose:     This project accepts the mortgage amount, the annual interest
rate, and the term of the loan in years from the user. The program
then calculates the monthly mortgage payment and displays it in currency format.
There is a quit button to exit the application, and a clear button to
clear user entered fields in order to calculate a new mortgage. The program
will also clear the payment field should the user press any key while positioned
in any input textfield.


 */



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 java.text.*;

class Mortgage extends JFrame
{


    private JLabel headerJLabel;

     Border rowborder = new EmptyBorder( 3, 10, 3, 10 );
       

    // JLabel and JTextField for the mortgage amount
    private JLabel amountJLabel;
    private JTextField amountJTextField;

    // JLabel for monthly mortgage payment
    private JLabel monthpayJLabel;

    // JTextArea for displaying results
    private JTextArea outputJTextArea;

    // JButton to initiate calculations
    private JButton calculateJButton;

    // JButton to clear entries
    private JButton clearJButton;

    // JButton to quit
    private JButton quitJButton;

    // no-argument constructor
    public Mortgage()
    {
        createUserInterface();
    }

    // create and position the GUI components; register event handlers
    private void createUserInterface()
    {
        // get content pane for attaching GUI components
        Container contentPane = getContentPane();

        // set northLabel using borderlayout
        headerJLabel = new JLabel();
        headerJLabel.setBounds(16, 16, 300, 46);
        headerJLabel.setText("Enter Values then Calculate for Monthly Payment");
        contentPane.add(headerJLabel);

        // enable specific positioning of GUI components
        contentPane.setLayout(null);

        // set up amountJLabel
        amountJLabel = new JLabel();
        amountJLabel.setBounds(116, 76, 104, 26);
        amountJLabel.setText("Mortgage amount:");
        contentPane.add(amountJLabel);

        // set up amountJTextField
        amountJTextField = new JTextField();
        amountJTextField.setBounds(244, 76, 56, 26);
        amountJTextField.setHorizontalAlignment(JTextField.RIGHT);
        contentPane.add(amountJTextField);
        amountJTextField.addKeyListener(

        new KeyAdapter() // anonymous inner class
        {
            // event handler for key pressed in amountJTextField
            public void keyPressed(KeyEvent event)
            {
                amountJTextFieldkeyPressed(event);
            }

        } // end anonymous inner class

        ); // end call to addKeyListener

    // added code below for radio buttons

        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);


        ButtonGroup bgroup = new ButtonGroup();      
          bgroup.add(aButton);
          bgroup.add(bButton);
          bgroup.add(cButton);
          radioPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 4, 4 ));      
          radioPanel.add(aButton);
          radioPanel.add(bButton);
          radioPanel.add(cButton);
          contentPane.add(radioPanel);
          //radioPanel.setMaximumSize( new Dimension( 10000, radioPanel.getMinimumSize().height ) );
          radioPanel.setBorder( rowborder );

         // end added code

        // set up calculateJButton
        calculateJButton = new JButton();
        calculateJButton.setBounds(16, 196, 90, 26);
        calculateJButton.setText("Calculate");
        contentPane.add(calculateJButton);
        calculateJButton.addActionListener(

        new ActionListener() // anonymous inner class
        {
            // event handler called when calculateJButton is pressed
            public void actionPerformed(ActionEvent event)
            {
                calculateJButtonActionPerformed(event);
            }

        } // end anonymous inner class

        ); // end call to addActionListener

        // set up clearJButton
        clearJButton = new JButton();
        clearJButton.setBounds(16, 240, 90, 26);
        clearJButton.setText("Clear");
        contentPane.add(clearJButton);
        clearJButton.addActionListener(

        new ActionListener() // anonymous inner class
        {
            // event handler called when clearJButton is pressed
            public void actionPerformed(ActionEvent event)
            {
                clearJButtonActionPerformed(event);
            }

        } // end anonymous inner class

        ); // end call to addActionListener


        // set up quitJButton
        quitJButton = new JButton();
        quitJButton.setBounds(140, 240, 90, 26);
        quitJButton.setText("Quit");
        contentPane.add(quitJButton);
        quitJButton.addActionListener(

        new ActionListener() // anonymous inner class
        {
            // event handler called when quitJButton is pressed
            public void actionPerformed(ActionEvent event)
            {
                quitJButtonActionPerformed(event);
            }

        } // end anonymous inner class

        ); // end call to addActionListener


        // set up outputJTextArea
        outputJTextArea = new JTextArea();
        contentPane.add(outputJTextArea);
        outputJTextArea.setBounds(244, 196, 56, 26);
        outputJTextArea.setEditable(false);

        //set up monthpayJLabel
        monthpayJLabel = new JLabel();
        monthpayJLabel.setBounds(120, 196, 156, 26);
        monthpayJLabel.setText("Monthly Payment:");
        contentPane.add(monthpayJLabel);

        // set properties of application’s window
        setTitle("Mortgage Calculator"); // set title bar text
        setSize(338, 350); // set window's size
        setVisible(true); // display window

    } // end method createUserInterface

    // called when user presses key in amountJTextField
    private void amountJTextFieldkeyPressed(KeyEvent event)
    {
        outputJTextArea.setText(""); // clear outputJTextArea
    }

        // method called when user clicks calculateJButton
    private void calculateJButtonActionPerformed(ActionEvent event)
    {
        try
        {
            //int term = Integer.parseInt(termJTextField.getText());
                // length of the loan in years
            //int monthCount = (term * 12); // Calculate months from years
            //int amount = Integer.parseInt(amountJTextField.getText());
            //double yearlyRate = Double.parseDouble(interestJTextField.getText())
            //    ;
            //double interestRateMonthly = (yearlyRate / 12) / 100;
                // Calculate monthly interest
            //double monthlyPayment; // monthly payment amount variable

            // Calculate monthly payment
            //monthlyPayment = (amount * interestRateMonthly) / (1-Math.pow(1 +
            //    interestRateMonthly,  - monthCount));



            DecimalFormat currency = new DecimalFormat("$0.00");
                // Format output
            //outputJTextArea.setText(currency.format(monthlyPayment));
                // Output payment amount

        }
        catch (NumberFormatException ex){}
    }


    // method called when user clicks clearJButton
    private void clearJButtonActionPerformed(ActionEvent event)
    {
        try
        {
            outputJTextArea.setText(""); // clear outputJTextArea
            amountJTextField.setText(""); // clear amountJTextField
            //interestJTextField.setText(""); // clear interestJTextField
            //termJTextField.setText(""); // clear termJTextField

        }
        catch (NumberFormatException ex){}
    }





    // method called when user clicks quitJButton
    private void quitJButtonActionPerformed(ActionEvent event)
    {
        try
        {
            System.exit(0);

        }
        catch (NumberFormatException ex){}
    }



    // main method
    public static void main(String[]args)
    {
        Mortgage application = new Mortgage();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.setSize(400, 300);
        application.setVisible(true);
    } // end method main

} // end class Mortgage
0
Comment
Question by:ghost8067
  • 2
3 Comments
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 16857408
you need to specify the size and position of your radioPanel

radioPanel.setBounds( x, y, w, h );

set x,y,w,h to specify where you would like it to appear
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 16857765
Hope the below code solves your problem,

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 java.text.*;

class Mortgage extends JFrame
{


    private JLabel headerJLabel;

     Border rowborder = new EmptyBorder( 3, 10, 3, 10 );


    // JLabel and JTextField for the mortgage amount
    private JLabel amountJLabel;
    private JTextField amountJTextField;

    // JLabel for monthly mortgage payment
    private JLabel monthpayJLabel;

    // JTextArea for displaying results
    private JTextArea outputJTextArea;

    // JButton to initiate calculations
    private JButton calculateJButton;

    // JButton to clear entries
    private JButton clearJButton;

    // JButton to quit
    private JButton quitJButton;

    // no-argument constructor
    public Mortgage()
    {
        createUserInterface();
    }

    // create and position the GUI components; register event handlers
    private void createUserInterface()
    {
        // get content pane for attaching GUI components
        JPanel contentPane = new JPanel( new GridBagLayout() ) ;

        // set northLabel using borderlayout
        headerJLabel = new JLabel();
//        headerJLabel.setBounds(16, 16, 300, 46);
        headerJLabel.setText("Enter Values then Calculate for Monthly Payment");
        contentPane.add( headerJLabel, new GridBagConstraints( 0, 0, 3, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 25, 0 ), 0, 0 ) );

        // enable specific positioning of GUI components
//        contentPane.setLayout(null);

        // set up amountJLabel
        amountJLabel = new JLabel();
//        amountJLabel.setBounds(116, 76, 104, 26);
        amountJLabel.setText("Mortgage amount:");
//        contentPane.add(amountJLabel);
        contentPane.add( amountJLabel, new GridBagConstraints( 1, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 10 ), 0, 0 ) );

        // set up amountJTextField
        amountJTextField = new JTextField();
//        amountJTextField.setBounds(244, 76, 56, 26);
        amountJTextField.setHorizontalAlignment(JTextField.RIGHT);
//        contentPane.add(amountJTextField);
        contentPane.add( amountJTextField, new GridBagConstraints( 2, 1, 1, 1, 1.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );

        amountJTextField.addKeyListener(

        new KeyAdapter() // anonymous inner class
        {
            // event handler for key pressed in amountJTextField
            public void keyPressed(KeyEvent event)
            {
                amountJTextFieldkeyPressed(event);
            }

        } // end anonymous inner class

        ); // end call to addKeyListener

    // added code below for radio buttons

        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);


        ButtonGroup bgroup = new ButtonGroup();
          bgroup.add(aButton);
          bgroup.add(bButton);
          bgroup.add(cButton);
          radioPanel.setLayout( new GridBagLayout() );
          radioPanel.add(aButton);
          radioPanel.add(bButton);
          radioPanel.add(cButton);
//          contentPane.add(radioPanel);
        contentPane.add( radioPanel, new GridBagConstraints( 0, 2, 3, 1, 1.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 10, 0, 10, 0 ), 0, 0 ) );
          //radioPanel.setMaximumSize( new Dimension( 10000, radioPanel.getMinimumSize().height ) );
          radioPanel.setBorder( rowborder );

         // end added code

        // set up calculateJButton
        calculateJButton = new JButton();
//        calculateJButton.setBounds(16, 196, 90, 26);
        calculateJButton.setText("Calculate");
//        contentPane.add(calculateJButton);
        contentPane.add( calculateJButton, new GridBagConstraints( 0, 3, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 10 ), 0, 0 ) );
        calculateJButton.addActionListener(

        new ActionListener() // anonymous inner class
        {
            // event handler called when calculateJButton is pressed
            public void actionPerformed(ActionEvent event)
            {
                calculateJButtonActionPerformed(event);
            }

        } // end anonymous inner class

        ); // end call to addActionListener

        //set up monthpayJLabel
        monthpayJLabel = new JLabel();
//        monthpayJLabel.setBounds(120, 196, 156, 26);
        monthpayJLabel.setText("Monthly Payment:");
//        contentPane.add(monthpayJLabel);
        contentPane.add( monthpayJLabel, new GridBagConstraints( 1, 3, 1, 1, 0.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 0, 0, 10 ), 0, 0 ) );

        // set up outputJTextArea
        outputJTextArea = new JTextArea();
//        contentPane.add(outputJTextArea);
        contentPane.add( outputJTextArea, new GridBagConstraints( 2, 3, 1, 1, 1.0, 0.0,
            GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 0, 0, 0, 0 ), 0, 0 ) );
//        outputJTextArea.setBounds(244, 196, 56, 26);
        outputJTextArea.setEditable(false);

        JPanel btnPanel = new JPanel( new FlowLayout() ) ;
        // set up clearJButton
        clearJButton = new JButton();
//        clearJButton.setBounds(16, 240, 90, 26);
        clearJButton.setText("Clear");
//        contentPane.add(clearJButton);
        btnPanel.add( clearJButton );
        clearJButton.addActionListener(

        new ActionListener() // anonymous inner class
        {
            // event handler called when clearJButton is pressed
            public void actionPerformed(ActionEvent event)
            {
                clearJButtonActionPerformed(event);
            }

        } // end anonymous inner class

        ); // end call to addActionListener


        // set up quitJButton
        quitJButton = new JButton();
//        quitJButton.setBounds(140, 240, 90, 26);
        quitJButton.setText("Quit");
//        contentPane.add(quitJButton);
        btnPanel.add( quitJButton );
        quitJButton.addActionListener(

        new ActionListener() // anonymous inner class
        {
            // event handler called when quitJButton is pressed
            public void actionPerformed(ActionEvent event)
            {
                quitJButtonActionPerformed(event);
            }

        } // end anonymous inner class

        ); // end call to addActionListener

        getContentPane().add( contentPane, BorderLayout.CENTER ) ;
        getContentPane().add( btnPanel, BorderLayout.SOUTH ) ;

        // set properties of application’s window
        setTitle("Mortgage Calculator"); // set title bar text
        //setSize(338, 350); // set window's size
        pack() ;
        setVisible(true); // display window

    } // end method createUserInterface

    // called when user presses key in amountJTextField
    private void amountJTextFieldkeyPressed(KeyEvent event)
    {
        outputJTextArea.setText(""); // clear outputJTextArea
    }

        // method called when user clicks calculateJButton
    private void calculateJButtonActionPerformed(ActionEvent event)
    {
        try
        {
            //int term = Integer.parseInt(termJTextField.getText());
                // length of the loan in years
            //int monthCount = (term * 12); // Calculate months from years
            //int amount = Integer.parseInt(amountJTextField.getText());
            //double yearlyRate = Double.parseDouble(interestJTextField.getText())
            //    ;
            //double interestRateMonthly = (yearlyRate / 12) / 100;
                // Calculate monthly interest
            //double monthlyPayment; // monthly payment amount variable

            // Calculate monthly payment
            //monthlyPayment = (amount * interestRateMonthly) / (1-Math.pow(1 +
            //    interestRateMonthly,  - monthCount));



            DecimalFormat currency = new DecimalFormat("$0.00");
                // Format output
            //outputJTextArea.setText(currency.format(monthlyPayment));
                // Output payment amount

        }
        catch (NumberFormatException ex){}
    }


    // method called when user clicks clearJButton
    private void clearJButtonActionPerformed(ActionEvent event)
    {
        try
        {
            outputJTextArea.setText(""); // clear outputJTextArea
            amountJTextField.setText(""); // clear amountJTextField
            //interestJTextField.setText(""); // clear interestJTextField
            //termJTextField.setText(""); // clear termJTextField

        }
        catch (NumberFormatException ex){}
    }





    // method called when user clicks quitJButton
    private void quitJButtonActionPerformed(ActionEvent event)
    {
        try
        {
            System.exit(0);

        }
        catch (NumberFormatException ex){}
    }



    // main method
    public static void main(String[]args)
    {
        Mortgage application = new Mortgage();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.setSize(400, 300);
        application.setVisible(true);
    } // end method main

} // end class Mortgage
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 16857881
hey setting the layout to null and using the bounds are not reccommended in swings! better go with GBL which is much flexible!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

612 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