Calling a Button in another Button
Hello,
I have made a program that has two buttons. One of the buttons calls
over half of what the other button does. I was not sure how to make one
button call another button and harness all of the data so I copied all
of the code from the first button and then add some code for the second
buttons ActionEvent. (This way the user gets choices as to how they can
view the data.) The first button is calculate and the second is show. I
would like for show to do all the actions in calculate, plus.
I worked on trying this for hours and had issues with the array, among
other things.
//Here is the snippet that I am looking for assistance, thanks.
private void calculateJButtonActionPerformed ( ActionEvent event )
{
try
{
// check which of three radio buttons are selected and set the index
on it
if (array1JRadioButton.isSelected())
{
i = 0;
}
if (array2JRadioButton.isSelected())
{
i = 1;
}
if (array3JRadioButton.isSelected())
{
i = 2;
}
double balance =
Double.parseDouble(balanceJTextField.getText());
double interestYears = ( interest[i] / 100 );
double monthlyInt = (interestYears / 12);
double paymentCount = ( years[i] * 12 );
double total = balance * ( monthlyInt / (1 - Math.pow(1
+ monthlyInt, - paymentCount)));
// Places no more than two integers after the decimal point
DecimalFormat currency = new DecimalFormat("$0.00");
// Formats output
NumberFormat totalCurrencyFormatter;
totalCurrencyFormatter = NumberFormat.getCurrencyInstance();
// Output payment amount
totalJTextField.setText(totalCurrencyFormatter.format(total));
valuesJTextArea.setText(""); // clear display
}
catch(NumberFormatException e)
{
// displays error in application
JOptionPane.showMessageDialog(null, "You must type numbers in the
text box before selecting the 'Calculate' button. \t Please Try
Again!", "Invalid Button Selection", JOptionPane.INFORMATION_MESSAGE);
}
}
private void showJButtonActionPerformed ( ActionEvent event )
{
try
{
// check which of three radio buttons are selected and set the index
on it
if (array1JRadioButton.isSelected())
{
i = 0;
}
if (array2JRadioButton.isSelected())
{
i = 1;
}
if (array3JRadioButton.isSelected())
{
i = 2;
}
double balance =
Double.parseDouble(balanceJTextField.getText());
double interestYears = ( interest[i] / 100 );
double monthlyInt = (interestYears / 12);
double paymentCount = ( years[i] * 12 );
double total = balance * ( monthlyInt / (1 - Math.pow(1
+ monthlyInt, - paymentCount)));
// Places no more than two integers after the decimal point
DecimalFormat currency = new DecimalFormat("$0.00");
// Formats output
NumberFormat totalCurrencyFormatter;
totalCurrencyFormatter = NumberFormat.getCurrencyInstance();
// Output payment amount
totalJTextField.setText(totalCurrencyFormatter.format(total));
// grabs the quantities from the mortgage variables
valuesJTextArea.setText ("");
// Creates the header for the Text Area
StringBuffer list = new StringBuffer();
valuesJTextArea.append("Number \t Payment \t Interest \t Current
\n");
valuesJTextArea.append("of Payments \t Amount\t Paid \t
Balance\n");
valuesJTextArea.append("************************************************************************\n");
// the equation for breakdown the months, payments, interest, and
balance throughout the text area
for (short month = 1; month <= paymentCount; ++month)
{
// denotes the amount of payments
double prinpmt, intpmt = balance * monthlyInt;
if (month < paymentCount)
prinpmt = total - intpmt;
else
prinpmt = balance;
balance -= prinpmt;
// relates to a string that is in the text field
showing the breakdown of amounts
valuesJTextArea.append(month + ".\t" +
currency.format(intpmt + prinpmt) + "\t"
+ currency.format(intpmt) + "\t" +
currency.format(balance) + "\n");
}
// types to a string that is in the text field showing
the breakdown of amounts
valuesJTextArea.append(list.toString());
// sets the positioning of the view to the top in the scrollable
Text Area
valuesJTextArea.setCaretPosition(0);
}
catch(NumberFormatException e)
{
// displays error in application
JOptionPane.showMessageDialog(null, "You must type numbers in the
text box before selecting the 'Calculate' button. \t Please Try
Again!", "Invalid Button Selection", JOptionPane.INFORMATION_MESSAGE);
}
}
Thanks in advance,
Mike