I have a program where I calculate for either 1 of 2 variables depending on the radio button selected. I'm trying to use getValue
to get a value from a JFormattedText
field and display it on another JFormattedText
field (eventually I will do some calculations with the number).
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.DecimalFormat;
public class FutureValueFrame extends JFrame
{
public FutureValueFrame()
{
setTitle("Sample App");
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
JFrame f = new FutureValueFrame();
//GUI and BUTTONS
JRadioButton monthlyRadioButton = new JRadioButton("Monthly Payment");
JRadioButton loanAmountButton = new JRadioButton("Loan Amount");
ButtonGroup selection = new ButtonGroup();
selection.add(monthlyRadioButton);
selection.add(loanAmountButton);
JFormattedTextField loanAmountField = new JFormattedTextField(new DecimalFormat("####.##"));
JFormattedTextField interestRateField = new JFormattedTextField(new DecimalFormat("####.##"));
JFormattedTextField yearField = new JFormattedTextField(new DecimalFormat("####.##"));
JFormattedTextField monthlyPaymentField = new JFormattedTextField(new DecimalFormat("####.##"));
JPanel menuPanel = new JPanel();
menuPanel.setLayout(new GridLayout(1,2));
//ACTION LISTENER FOR RADIO BUTTONS
monthlyRadioButton.addActionListener(new SelectionListener(monthlyRadioButton, loanAmountButton, loanAmountField, monthlyPaymentField));
loanAmountButton.addActionListener(new SelectionListener(monthlyRadioButton, loanAmountButton, loanAmountField, monthlyPaymentField));
JPanel topPanel = new JPanel();
topPanel.setLayout(new GridLayout(1,2));
topPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
topPanel.add(monthlyRadioButton);
topPanel.add(loanAmountButton);
JPanel botPanel = new JPanel();
botPanel.setLayout(new GridLayout(4,2));
botPanel.add(new JLabel("Loan Amount:"));
botPanel.add(loanAmountField);
botPanel.add(new JLabel("Yearly Interest Rate:"));
botPanel.add(interestRateField);
botPanel.add(new JLabel("Number of Years:"));
botPanel.add(yearField);
botPanel.add(new JLabel("Monthly Payment:"));
botPanel.add(monthlyPaymentField);
JPanel container = new JPanel();
container.setLayout(new GridLayout(3,1));
container.add(topPanel);
container.add(botPanel);
container.add(menuPanel);
f.add(container);
JButton calculateButton = new JButton("Calculate");
calculateButton.addActionListener(new CalculateMonthlyListener(loanAmountField, interestRateField, yearField, monthlyPaymentField, selection, monthlyRadioButton, loanAmountButton));
JButton exitButton = new JButton("Exit");
exitButton.addActionListener(new ExitListener());
menuPanel.add(calculateButton);
menuPanel.add(exitButton);
f.setVisible(true);
f.setLocationRelativeTo(null);
}
class CalculateMonthlyListener implements ActionListener
{
private JFormattedTextField loanAmountField;
private JFormattedTextField monthlyPaymentField;
private JFormattedTextField interestRateField;
private JFormattedTextField yearField;
private double result;
private ButtonGroup selection;
private JRadioButton monthlyRadioButton;
private JRadioButton loanAmountButton;
public CalculateMonthlyListener (JFormattedTextField loanAmountField,
JFormattedTextField interestRateField,
JFormattedTextField yearField,
JFormattedTextField monthlyPaymentField,
ButtonGroup selection,
JRadioButton monthlyRadioButton,
JRadioButton loanAmountButton)
{
this.interestRateField = interestRateField;
this.yearField = yearField;
this.loanAmountField = loanAmountField;
this.selection = selection;
this.monthlyRadioButton = monthlyRadioButton;
this.loanAmountButton = loanAmountButton;
this.monthlyPaymentField = monthlyPaymentField;
}
public void actionPerformed(ActionEvent e)
{
if (selection.getSelection().equals(monthlyRadioButton.getModel()))
{
result = ((Double) interestRateField.getValue()).floatValue();
monthlyPaymentField.setValue(new Double(result));
System.out.println("You selected monthly");
}
else
{
loanAmountField.setValue(new Double(12.22));
System.out.println("You selected loan");
}
}
}
class ExitListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//f.dispose();
System.exit(0);
//System.out.println("You clicked exit");
}
}
class SelectionListener implements ActionListener
{
private JRadioButton monthlyRadioButton;
private JRadioButton loanAmountButton;
private JFormattedTextField loanAmountField;
private JFormattedTextField monthlyPaymentField;
public SelectionListener (JRadioButton monthlyRadioButton,
JRadioButton loanAmountButton,
JFormattedTextField loanAmountField,
JFormattedTextField monthlyPaymentField)
{
this.monthlyRadioButton = monthlyRadioButton;
this.loanAmountButton = loanAmountButton;
this.loanAmountField = loanAmountField;
this.monthlyPaymentField = monthlyPaymentField;
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == monthlyRadioButton)
{
loanAmountField.setEditable(false);
monthlyPaymentField.setEditable(true);
}
else
{
monthlyPaymentField.setEditable(false);
loanAmountField.setEditable(true);
}
}
}
}
My problem occurs in the snippet of code below:
public void actionPerformed(ActionEvent e) {
if (selection.getSelection().equals(monthlyRadioButton.getModel())) {
result = ((Double) interestRateField.getValue()).floatValue();
monthlyPaymentField.setValue(new Double(result));
System.out.println("You selected monthly");
} else {
loanAmountField.setValue(new Double(12.22));
System.out.println("You selected loan");
}
}
Here, I am trying to assign the getValue
to result. I've looked Oracle documentation and it seems like the code to get the value is simply interestRateField.getValue();
When I tried this though, I got an error saying Can't convert object to double
so I added floatValue()
and cast it to remove the error.
When I press calculate, it does not display the input from interestRateField
in monthlyPaymentField
.
How do I get my value (from JFormattedTextField
using DecimalFormat
) using getValue
and then display it in a different JFormattedTextField
?