Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Want to use a radio button or menu instead of a combobox 1

Status
Not open for further replies.

gschoser

Technical User
Dec 26, 2001
13
US
I at least have the program working correctly thanks to the awesome help on this board. However, now I want to tweak the program a little bit. I am currently using a combobox for the interest rate and term. I would like to use either a radio button or a Menu item to do basically the same thing. I have two programs, one calls the other, however I don't think that it makes a difference since all the coding is done at the end of the program for this.

Here is my program and I will use <------------- to point to the area's I have questions.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MortApp2 implements ActionListener
{
// String for combo box
String[] strRateTerms = { "7 year at 5.35%", "15 year at 5.5%", "30 year at 5.75%" };

// Declare our variables
static JFrame frame;
static JPanel mortPane;
static JPanel contentPane;
static JPanel pRow1;
static JPanel pRow2;
static JPanel pRow3;
static JPanel pRow4;
static JPanel pRow5;
static JLabel lblPrincipal;
static JLabel lblRateTerm;
static JLabel lblMonthlyPaymentLbl;
static JLabel lblMonthlyPayment;
static JTextField txtPrincipal;
static JTextField txtRate;
static JTextField txtTerm;
static JButton bCalculate;
static JButton bQuit;

// New vars
static JComboBox cbRateTerm; <-------------- Want to change Combobox to a Radio Button or Menu Item
static JTextArea txtMonths;
static JScrollPane spMonths;

static Mortcalc2 MC2;

private Component createComponents()
{
// Create the panels
contentPane = new JPanel(new GridLayout(2,1,0,0));
mortPane = new JPanel(new GridLayout(4,1,0,0));
pRow1 = new JPanel(new GridLayout(1,2,5,0));
pRow2 = new JPanel(new GridLayout(1,2,5,0));
pRow3 = new JPanel(new GridLayout(1,2,5,0));
pRow4 = new JPanel(new FlowLayout(FlowLayout.CENTER,5,0));
pRow5 = new JPanel(new FlowLayout(FlowLayout.CENTER,5,0));

// Create and add the components.
lblPrincipal = new JLabel("Principal:", JLabel.RIGHT);
txtPrincipal = new JTextField(20);
pRow1.add(lblPrincipal);
pRow1.add(txtPrincipal);

lblRateTerm = new JLabel("Rate/Term:", JLabel.RIGHT);
cbRateTerm = new JComboBox(strRateTerms); <--------------If I am a change to a menu item or a radio button do I need to make the change here also?
cbRateTerm.setSelectedIndex(0);
pRow2.add(lblRateTerm);
pRow2.add(cbRateTerm);

lblMonthlyPaymentLbl = new JLabel("Monthly Payment:", JLabel.RIGHT);
lblMonthlyPayment = new JLabel("");
pRow3.add(lblMonthlyPaymentLbl);
pRow3.add(lblMonthlyPayment);

bCalculate = new JButton("Calculate");
bCalculate.addActionListener(this);
pRow4.add(bCalculate);

bQuit = new JButton("Quit");
bQuit.addActionListener(this);
pRow4.add(bQuit);

txtMonths = new JTextArea(12, 50);
txtMonths.setEditable(false);
txtMonths.setTabSize(4);
spMonths = new JScrollPane(txtMonths, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
pRow5.add(spMonths);

// Add sub-panels to parents panel
mortPane.add(pRow1);
mortPane.add(pRow2);
mortPane.add(pRow3);
mortPane.add(pRow4);

contentPane.add(mortPane);
contentPane.add(pRow5);

// Return the content panel
return contentPane;
}

public void actionPerformed(ActionEvent e)
{
// Check which button triggered the event
if("Calculate".equals(e.getActionCommand()))
{
// Get information from fields
double principal = new Double(txtPrincipal.getText()).doubleValue();
double rate;
int term;

int i = cbRateTerm.getSelectedIndex();
int j;

double monthlyPayment;
double monthlyInterestPayment;
double monthlyPrincipalPayment;
double balance = principal;

// Set rate and term based on which item in the combobox is selected
if(i == 0)
{
rate = 5.35;
term = 7;
}
else if(i == 1)
{
rate = 5.5;
term = 15;
}
else
{
rate = 5.75;
term = 30;
}

// Create a string format
java.text.DecimalFormat dec = new java.text.DecimalFormat("$,###.00");

// Create a new instance of Mortcalc2 with values from text boxes
MC2 = new Mortcalc2(principal, rate, term);

// Set the monthly payment
lblMonthlyPayment.setText(dec.format(MC2.getMonthlyPayment()));

// Get the monthly payment
monthlyPayment = MC2.getMonthlyPayment();

// Find out how many months in the term
j = term * 12;

// Clear old calculation
txtMonths.setText("");

// Print header
txtMonths.append("Month\t\tInterest\t\tPrincipal\t\tBalance\n");
txtMonths.append("\t\tPayment\tPayment\n\n");

// Loop through all months
for(i = 1; i < j + 1; i++)
{
// Get the monthly payment information
monthlyInterestPayment = MC2.getMonthlyInterestPayment();
monthlyPrincipalPayment = MC2.getMonthlyPrincipalPayment();
balance = MC2.getBalance();

// Print the information
txtMonths.append(i + ": " + "\t\t" + dec.format(monthlyInterestPayment) + "\t\t");
txtMonths.append(dec.format(monthlyPrincipalPayment) + "\t\t" + dec.format(balance) + "\n");

// Calculate next month
MC2.nextMonth();
}
}
else if("Quit".equals(e.getActionCommand()))
{
// Exit the program
System.exit(0);
}

}

private static void createAndShowGUI()
{
// Set window decorations
JFrame.setDefaultLookAndFeelDecorated(true);

// Create and set up the window.
frame = new JFrame("mortApp");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Create an instance of our application
MortApp2 app = new MortApp2();

// Create our components
Component contents = app.createComponents();

// Add components to frame
frame.getContentPane().add(contents);

// Display the window.
frame.pack();
frame.setVisible(true);

// Don't allow window to be resized
frame.setResizable(true);
}

public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
 
There's no problems :
- create 3 JRadioButton and add them to your Jframe.
- create a ButtonGroup (that is a non visual component in charge to group radiobuttons to achieve exclusivity)
- Add your 3 radio to the buttonGroup
- use buttonGroup method getSelection or isSelected to determine wich radio was selected.

PS : don't forget to set the value of one of the buttons to true.

Water is not bad as long as it stays out human body ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top