I’m writing a simple loan calculator with gui using swing. I used DecimalFormat to make sure I capture the input in my specified format ####.## for my JFormattedTextField .
public static void main(String[] args) {
JFormattedTextField loanAmountField = new JFormattedTextField(new DecimalFormat("####.##"));
JFormattedTextField interestRateField = new JFormattedTextField(new DecimalFormat("####.##"));
JFormattedTextField yearField = new JFormattedTextField(new DecimalFormat("####.##"));
JFormattedTextField monthlyPaymentField = new JFormattedTextField(new DecimalFormat("####.##"));
}
When the user press my “calculate” button, it calculates and displays the monthlyPaymentField.
To test if my program will even display it correctly, I tried assigning monthlyPaymentField a value of 12.22, but I’m getting an error that says monthlyPaymentField should be declared as a double.
class CalculateListener implements ActionListener {
public CalculateListener (JFormattedTextField loanAmountField, JFormattedTextField monthlyPaymentField, JFormattedTextField interestRateField, JFormattedTextField yearField, int monthlyTest)
{
this.interestRateField = interestRateField;
this.yearField = yearField;
this.loanAmountField = loanAmountField;
this.monthlyPaymentField = monthlyPaymentField;
this.monthlyTest = monthlyTest;
}
public void actionPerformed(ActionEvent event){
monthlyPaymentField = 12.22;
}
}
How do I go about this so that I can still use my JFormattedTextField with DecimalFormat but can still display a Float on my monthlyPaymentField?
You can use the
setValuemethod for this;