Why don't you use a layout manager then place your radio button in a panel that goes into the manager...
Also, why are you using getContentPane() you no longer need to use it in Java 5/6
Main Topics
Browse All TopicsHello experts! I'm working on this good 'ole mortgage calculator program, and trust me...I've looked for solutions and I've stared at my code for days and compared, but I cannot get the radio button row to appear below the principle input box instead of across the top. Simple, I'm sure. But I'm stumpled. Now I know this program has other issues/problems going on before it does what it's meant to do, but for now I want to get the GUI set up. Could you please take a look and see what I'm doing wrong? Thank you in advance!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent
import java.awt.event.ActionListe
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBo
public class MortgageCalculator extends JFrame implements ActionListener
{
double rate = 0; //APR
double monthlyPayment = 0; //Monthly payment amount
double principle = 0; //Amount of loan
int term = 0; //Mortgage term in years
double interest = 0; //Interest, in months
int notePeriod = 0; //Mortgage term in months
String mTerm[] = {"7", "15", "30"};
String mRate[] = {"5.35%", "5.50%", "5.75%"};
//Labels and Input Fields
JPanel row1 = new JPanel();
JLabel mortgageLabel = new JLabel("MORTGAGE CALCULATOR", JLabel.CENTER);
JPanel row2 = new JPanel(new GridLayout(1, 2));
JLabel principleLabel = new JLabel("Mortgage Principle $",JLabel.LEFT);
JTextField principleTxt = new JTextField(8);
JPanel radioPanel = new JPanel();
JRadioButton buttonA = new JRadioButton("7 Years at 5.35%" , false);
JRadioButton buttonB = new JRadioButton("15 Years at 5.50%" , false);
JRadioButton buttonC = new JRadioButton("30 Years at 5.75%", false);
JPanel row5 = new JPanel(new GridLayout(1, 2));
JLabel monthlyPaymentLabel = new JLabel("Monthly Payment $", JLabel.LEFT);
JTextField monthlyPaymentTxt = new JTextField(8);
//Create buttons
JPanel button = new JPanel(new FlowLayout(FlowLayout.CENT
JButton amortizeButton = new JButton("Amortize Payments");
JButton clearButton = new JButton("Clear");
JButton exitButton = new JButton("Exit");
JButton calculateButton = new JButton("Calculate");
JTextArea displayArea = new JTextArea(10, 30);
JScrollPane scroll = new JScrollPane(displayArea);
public MortgageCalculator()
{
super ("Mortgage Payment Calculator"); //Header
setSize(450, 400); //GUI frame size
setDefaultCloseOperation(J
Container pane = getContentPane();
Border rowborder = new EmptyBorder( 3, 10, 3, 10 ); //Border around elements inside frame
//Pane and row properties, GUI layout
JPanel panel = new JPanel(new GridLayout(4,1));
panel.add(row1);
row1.add(mortgageLabel);
row1.setMaximumSize( new Dimension( 1000, row1.getMinimumSize().heig
row1.setBorder( rowborder);
panel.add(row2);
row2.add(principleLabel);
row2.add(principleTxt);
row2.setMaximumSize( new Dimension( 1000, row2.getMinimumSize().heig
row2.setBorder( rowborder);
ButtonGroup bgroup = new ButtonGroup();
bgroup.add(buttonA);
bgroup.add(buttonB);
bgroup.add(buttonC);
radioPanel.setLayout(new FlowLayout(FlowLayout.RIGH
radioPanel.add(buttonA);
radioPanel.add(buttonB);
radioPanel.add(buttonC);
pane.add(radioPanel);
radioPanel.setMaximumSize(
radioPanel.setBorder( rowborder);
panel.add(row5);
row5.add(monthlyPaymentLab
row5.add(monthlyPaymentTxt
monthlyPaymentTxt.setEnabl
row5.setMaximumSize( new Dimension( 1000, row5.getMinimumSize().heig
row5.setBorder( rowborder);
panel.add(button); //Add buttons
button.add(calculateButton
button.add(clearButton);
button.add(exitButton);
button.add(amortizeButton)
pane.add(panel);
button.setMaximumSize( new Dimension( 1000, button.getMinimumSize().he
scroll.setBorder(BorderFac
pane.add(scroll);
pane.setLayout(new BoxLayout( pane, BoxLayout.Y_AXIS));
setVisible(true);
setContentPane(pane);
amortizeButton.addActionLi
clearButton.addActionListe
exitButton.addActionListen
calculateButton.addActionL
buttonA.addActionListener(
buttonB.addActionListener(
buttonC.addActionListener(
}
public void actionPerformed(ActionEven
{
Object command = event.getSource();
if(command == calculateButton)
{
try
{
principle = Double.parseDouble(princip
}
catch(NumberFormatExceptio
{
//Select rate and term
if(buttonA.isSelected() == true)
{
rate = 5.35;
term = 7;
}
else if(buttonB.isSelected() == true)
{
rate = 5.5;
term = 15;
}
else if (buttonC.isSelected() == true)
{
rate = 5.75;
term = 30;
}
else //none of the buttons are selected, this is an actual error. Throw an exception
{
JOptionPane.showMessageDia
}
}
//Calculate monthly interest rate
interest = ((rate / 100) / 12);
//Calculate total number of payments
notePeriod = term * 12; // (for example, 5 years means notePeriod = 60)
//Calculate monthly payment
if (rate == 0.0)
{
monthlyPayment = principle / notePeriod;
}
else
{
monthlyPayment = (principle * interest) / (1 - Math.pow(1 + interest, -notePeriod));
}
//Currency formatter
NumberFormat myCurrencyFormatter;
myCurrencyFormatter = NumberFormat.getCurrencyIn
monthlyPaymentTxt.setText(
}
if(command == clearButton)
{
principleTxt.setText(null)
monthlyPaymentTxt.setText(
displayArea.setText(null);
}
if(command == exitButton)
{
System.exit(0);
}
if (command == amortizeButton)
{
double year = 0;
double interest = 0;
double balance = 0;
double monthlyInterest = 0;
int x = 1;
int months = term * 12;
String titles = "Month\t Principle\tInterest\tBalan
interest = (rate / 100) /12;
balance = principle;
displayArea.setText(titles
for (x = 1; x <= months; ++x)
{
//Calculate Detailed Payment Info
monthlyInterest = balance * interest;
if(interest != 0)
year = monthlyPayment - monthlyInterest;
balance = principle - year;
//Format Variables
DecimalFormat df = new DecimalFormat("\u00A4#,##0
DecimalFormat pf = new DecimalFormat("#,##0.00%")
DecimalFormat mi = new DecimalFormat("#,##0.000%"
//Positions the Cursor to the Top of the TextArea
displayArea.setCaretPositi
displayArea.append((months
}
}
}
public void Payment()
{
//Get user input
principle = Double.parseDouble(princip
//Calculate monthly interest rate
interest = ((rate / 100) / 12);
//Calculate total number of payments
notePeriod = term * 12;
//Calculate monthly payment
if (rate == 0.0)
{
monthlyPayment = principle / notePeriod;
}
else
{
monthlyPayment = (principle * interest) / (1 - Math.pow(1 + interest, -notePeriod));
}
//Currency formatter
NumberFormat myCurrencyFormatter;
myCurrencyFormatter = NumberFormat.getCurrencyIn
monthlyPaymentTxt.setText(
}
public static void main (String[] arguments) //Main Method
{
MortgageCalculator smc = new MortgageCalculator();
smc.setVisible(true);
smc.setDefaultCloseOperati
}
} //End of program
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
try this code,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent
import java.awt.event.ActionListe
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBo
public class MortgageCalculator extends JFrame implements ActionListener
{
double rate = 0; //APR
double monthlyPayment = 0; //Monthly payment amount
double principle = 0; //Amount of loan
int term = 0; //Mortgage term in years
double interest = 0; //Interest, in months
int notePeriod = 0; //Mortgage term in months
String mTerm[] = {"7", "15", "30"};
String mRate[] = {"5.35%", "5.50%", "5.75%"};
//Labels and Input Fields
JPanel row1 = new JPanel();
JLabel mortgageLabel = new JLabel("MORTGAGE CALCULATOR", JLabel.CENTER);
JPanel row2 = new JPanel(new GridLayout(1, 2));
JLabel principleLabel = new JLabel("Mortgage Principle $",JLabel.LEFT);
JTextField principleTxt = new JTextField(8);
JPanel radioPanel = new JPanel();
JRadioButton buttonA = new JRadioButton("7 Years at 5.35%" , false);
JRadioButton buttonB = new JRadioButton("15 Years at 5.50%" , false);
JRadioButton buttonC = new JRadioButton("30 Years at 5.75%", false);
JPanel row5 = new JPanel(new GridLayout(1, 2));
JLabel monthlyPaymentLabel = new JLabel("Monthly Payment $", JLabel.LEFT);
JTextField monthlyPaymentTxt = new JTextField(8);
//Create buttons
JPanel button = new JPanel(new FlowLayout(FlowLayout.CENT
JButton amortizeButton = new JButton("Amortize Payments");
JButton clearButton = new JButton("Clear");
JButton exitButton = new JButton("Exit");
JButton calculateButton = new JButton("Calculate");
JTextArea displayArea = new JTextArea(10, 30);
JScrollPane scroll = new JScrollPane(displayArea);
public MortgageCalculator()
{
super ("Mortgage Payment Calculator"); //Header
setSize(450, 400); //GUI frame size
setDefaultCloseOperation(J
Container pane = getContentPane();
pane.setLayout(new BoxLayout( pane, BoxLayout.Y_AXIS));
Border rowborder = new EmptyBorder( 3, 10, 3, 10 ); //Border around elements inside frame
//Pane and row properties, GUI layout
JPanel panel = new JPanel(new GridLayout(5,1));
panel.add(row1);
row1.add(mortgageLabel);
row1.setMaximumSize( new Dimension( 1000, row1.getMinimumSize().heig
row1.setBorder( rowborder);
panel.add(row2);
row2.add(principleLabel);
row2.add(principleTxt);
row2.setMaximumSize( new Dimension( 1000, row2.getMinimumSize().heig
row2.setBorder( rowborder);
panel.add(radioPanel);
ButtonGroup bgroup = new ButtonGroup();
bgroup.add(buttonA);
bgroup.add(buttonB);
bgroup.add(buttonC);
radioPanel.setLayout(new FlowLayout(FlowLayout.RIGH
radioPanel.add(buttonA);
radioPanel.add(buttonB);
radioPanel.add(buttonC);
// pane.add(radioPanel);
radioPanel.setMaximumSize(
radioPanel.setBorder( rowborder);
panel.add(row5);
row5.add(monthlyPaymentLab
row5.add(monthlyPaymentTxt
monthlyPaymentTxt.setEnabl
row5.setMaximumSize( new Dimension( 1000, row5.getMinimumSize().heig
row5.setBorder( rowborder);
panel.add(button); //Add buttons
button.add(calculateButton
button.add(clearButton);
button.add(exitButton);
button.add(amortizeButton)
pane.add(panel);
button.setMaximumSize( new Dimension( 1000, button.getMinimumSize().he
scroll.setBorder(BorderFac
pane.add(scroll);
setVisible(true);
setContentPane(pane);
amortizeButton.addActionLi
clearButton.addActionListe
exitButton.addActionListen
calculateButton.addActionL
buttonA.addActionListener(
buttonB.addActionListener(
buttonC.addActionListener(
}
public void actionPerformed(ActionEven
{
Object command = event.getSource();
if(command == calculateButton)
{
try
{
principle = Double.parseDouble(princip
}
catch(NumberFormatExceptio
{
//Select rate and term
if(buttonA.isSelected() == true)
{
rate = 5.35;
term = 7;
}
else if(buttonB.isSelected() == true)
{
rate = 5.5;
term = 15;
}
else if (buttonC.isSelected() == true)
{
rate = 5.75;
term = 30;
}
else //none of the buttons are selected, this is an actual error. Throw an exception
{
JOptionPane.showMessageDia
}
}
//Calculate monthly interest rate
interest = ((rate / 100) / 12);
//Calculate total number of payments
notePeriod = term * 12; // (for example, 5 years means notePeriod = 60)
//Calculate monthly payment
if (rate == 0.0)
{
monthlyPayment = principle / notePeriod;
}
else
{
monthlyPayment = (principle * interest) / (1 - Math.pow(1 + interest, -notePeriod));
}
//Currency formatter
NumberFormat myCurrencyFormatter;
myCurrencyFormatter = NumberFormat.getCurrencyIn
monthlyPaymentTxt.setText(
}
if(command == clearButton)
{
principleTxt.setText(null)
monthlyPaymentTxt.setText(
displayArea.setText(null);
}
if(command == exitButton)
{
System.exit(0);
}
if (command == amortizeButton)
{
double year = 0;
double interest = 0;
double balance = 0;
double monthlyInterest = 0;
int x = 1;
int months = term * 12;
String titles = "Month\t Principle\tInterest\tBalan
interest = (rate / 100) /12;
balance = principle;
displayArea.setText(titles
for (x = 1; x <= months; ++x)
{
//Calculate Detailed Payment Info
monthlyInterest = balance * interest;
if(interest != 0)
year = monthlyPayment - monthlyInterest;
balance = principle - year;
//Format Variables
DecimalFormat df = new DecimalFormat("\u00A4#,##0
DecimalFormat pf = new DecimalFormat("#,##0.00%")
DecimalFormat mi = new DecimalFormat("#,##0.000%"
//Positions the Cursor to the Top of the TextArea
displayArea.setCaretPositi
displayArea.append((months
}
}
}
public void Payment()
{
//Get user input
principle = Double.parseDouble(princip
//Calculate monthly interest rate
interest = ((rate / 100) / 12);
//Calculate total number of payments
notePeriod = term * 12;
//Calculate monthly payment
if (rate == 0.0)
{
monthlyPayment = principle / notePeriod;
}
else
{
monthlyPayment = (principle * interest) / (1 - Math.pow(1 + interest, -notePeriod));
}
//Currency formatter
NumberFormat myCurrencyFormatter;
myCurrencyFormatter = NumberFormat.getCurrencyIn
monthlyPaymentTxt.setText(
}
public static void main (String[] arguments) //Main Method
{
MortgageCalculator smc = new MortgageCalculator();
smc.setVisible(true);
smc.setDefaultCloseOperati
}
} //End of program
Business Accounts
Answer for Membership
by: objectsPosted on 2007-07-15 at 18:38:35ID: 19492663
> pane.add(radioPanel);
try adding it last