| | |
Want to use radio button or menu bar instead of combo box
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2005
Posts: 1
Reputation:
Solved Threads: 0
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();
}
});
}
}
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();
}
});
}
}
•
•
Join Date: Mar 2004
Posts: 802
Reputation:
Solved Threads: 40
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
About half way down the page it mentions radio buttons.
http://java.sun.com/docs/books/tutor...ts/button.html
![]() |
Similar Threads
- query with combo box input (MS Access and FileMaker Pro)
- VBA Combo Box Problem (Visual Basic 4 / 5 / 6)
- Depending upon the option selected in a combo box, how to perform the corresponding a (ASP)
- Combo Box (C++)
- Adding Horizondal Scroll bar to Combo Box (Visual Basic 4 / 5 / 6)
- combo box (ASP)
- How do I limit text char in Combo box? (VB.NET)
- combo box help (VB.NET)
Other Threads in the Java Forum
- Previous Thread: Need Big Help !!
- Next Thread: Project Ideas
Views: 6337 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Java
actionlistener android api apple applet application arguments array arrays automation binary bluetooth character chat class classes client code component consumer database desktop detection draw eclipse encode error event exception file fractal ftp game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me java javac javaee javaprojects jmf jni jpanel julia linked linux list loop mac map method methods mobile netbeans newbie number object online oracle os print printf problem program programming project properties recursion researchinmotion rotatetext rsa scanner score screen server set size sms socket sort sql string swing template test threads time transfer tree update windows working xstream





