I’m writing a simple loan calculator with gui using swing. I am using DecimalFormat to ensure correct formatting wtih 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("####.##"));
JButton calculateButton = new JButton("Calculate");
//Calculations based on selection
int monthlyTest;
if (monthlyRadioButton.isSelected()){
monthlyTest = 1;
calculateButton.addActionListener(new CalculateListener(loanAmountField, interestRateField, yearField, monthlyPaymentField, monthlyTest));
}
else{
monthlyTest = 0;
calculateButton.addActionListener(new CalculateListener(loanAmountField, interestRateField, yearField, monthlyPaymentField, monthlyTest));
}
}
The problem I’m having is that when I try to assign a value to loanAmountField, it doesn’t update it on my GUI’s JFormattedTextField.
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){
loanAmountField.setValue(new Double(12.22));
}
}
How do I display the new value on my GUI JFormattedTextField?
By SSCCE, I mean something like this:
Which shows that at least some of the code you’ve posted works fine.