Want to use radio button or menu bar instead of combo box

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2005
Posts: 1
Reputation: gschoser is an unknown quantity at this point 
Solved Threads: 0
gschoser gschoser is offline Offline
Newbie Poster

Want to use radio button or menu bar instead of combo box

 
0
  #1
Mar 27th, 2005
I at least have the program working correctly thanks to the awesome help from various boards and people I work with. 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. Yes this is homework but I have been busting my hump to get this working. I just want to see if there is something else I can do to make this a little more different than what the other students in class are going to do and learn something along the way.

Thanks Gretchen

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();
}
});
}
}
Attached Files
File Type: java MortApp2.java (6.2 KB, 10 views)
File Type: java Mortcalc2.java (3.3 KB, 16 views)
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 802
Reputation: Phaelax is on a distinguished road 
Solved Threads: 40
Phaelax Phaelax is offline Offline
Practically a Posting Shark

Re: Want to use radio button or menu bar instead of combo box

 
0
  #2
Mar 27th, 2005
A radio button has two parts basically. A different button for each option you want, and a group to know which set of radio buttons work together. The ButtonGroup takes care of making sure only 1 button is selected.

About half way down the page it mentions radio buttons.
http://java.sun.com/docs/books/tutor...ts/button.html
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 6337 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC