edthatcher
asked on
add data validation
I have the following code for a future value calculator:
I need to add data validation so that the user must enter data in all three fields: can someone please get me started adding the validation code?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
public class FutureValueApp
{
public static void main(String[] args)
{
JFrame frame = new FutureValueFrame();
frame.setVisible(true);
}
}
class FutureValueFrame extends JFrame
{
public FutureValueFrame()
{
setTitle("Future Value Calculator");
centerWindow(this);
setSize(267, 200);
setResizable(false);
setDefaultCloseOperation(J Frame.EXIT _ON_CLOSE) ;
JPanel panel = new FutureValuePanel();
this.add(panel);
}
private void centerWindow(Window w)
{
Toolkit tk = Toolkit.getDefaultToolkit( );
Dimension d = tk.getScreenSize();
setLocation((d.width-w.get Width())/2 , (d.height-w.getHeight())/2 );
}
}
class FutureValuePanel extends JPanel implements ActionListener
{
private JTextField paymentTextField,
rateTextField,
yearsTextField,
futureValueTextField;
private JLabel paymentLabel,
rateLabel,
yearsLabel,
futureValueLabel;
private JButton calculateButton,
exitButton;
public FutureValuePanel()
{
// display panel
JPanel displayPanel = new JPanel();
displayPanel.setLayout(new FlowLayout(FlowLayout.RIGH T));
// payment label
paymentLabel = new JLabel("Monthly Payment:");
displayPanel.add(paymentLa bel);
// payment text field
paymentTextField = new JTextField(10);
displayPanel.add(paymentTe xtField);
// rate label
rateLabel = new JLabel("Yearly Interest Rate:");
displayPanel.add(rateLabel );
// rate text field
rateTextField = new JTextField(10);
displayPanel.add(rateTextF ield);
// years label
yearsLabel = new JLabel("Number of Years:");
displayPanel.add(yearsLabe l);
// years text field
yearsTextField = new JTextField(10);
displayPanel.add(yearsText Field);
// future value label
futureValueLabel = new JLabel("Future Value:");
displayPanel.add(futureVal ueLabel);
// future value text field
futureValueTextField = new JTextField(10);
futureValueTextField.setEd itable(fal se);
futureValueTextField.setFo cusable(fa lse);
displayPanel.add(futureVal ueTextFiel d);
// button panel
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGH T));
// calculate button
calculateButton = new JButton("Calculate");
calculateButton.addActionL istener(th is);
buttonPanel.add(calculateB utton);
// exit button
exitButton = new JButton("Exit");
exitButton.addActionListen er(this);
buttonPanel.add(exitButton );
// add panels to main panel
this.setLayout(new BorderLayout());
this.add(displayPanel, BorderLayout.CENTER);
this.add(buttonPanel, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEven t e)
{
Object source = e.getSource();
if (source == exitButton)
System.exit(0);
else if (source == calculateButton)
{
double payment = Double.parseDouble(payment TextField. getText()) ;
double rate = Double.parseDouble(rateTex tField.get Text());
int years = Integer.parseInt(yearsText Field.getT ext());
double futureValue = FinancialCalculations.calc ulateFutur eValue(
payment, rate, years);
NumberFormat currency = NumberFormat.getCurrencyIn stance();
futureValueTextField.setTe xt(currenc y.format(f utureValue ));
}
}
}
I need to add data validation so that the user must enter data in all three fields: can someone please get me started adding the validation code?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
public class FutureValueApp
{
public static void main(String[] args)
{
JFrame frame = new FutureValueFrame();
frame.setVisible(true);
}
}
class FutureValueFrame extends JFrame
{
public FutureValueFrame()
{
setTitle("Future Value Calculator");
centerWindow(this);
setSize(267, 200);
setResizable(false);
setDefaultCloseOperation(J
JPanel panel = new FutureValuePanel();
this.add(panel);
}
private void centerWindow(Window w)
{
Toolkit tk = Toolkit.getDefaultToolkit(
Dimension d = tk.getScreenSize();
setLocation((d.width-w.get
}
}
class FutureValuePanel extends JPanel implements ActionListener
{
private JTextField paymentTextField,
rateTextField,
yearsTextField,
futureValueTextField;
private JLabel paymentLabel,
rateLabel,
yearsLabel,
futureValueLabel;
private JButton calculateButton,
exitButton;
public FutureValuePanel()
{
// display panel
JPanel displayPanel = new JPanel();
displayPanel.setLayout(new
// payment label
paymentLabel = new JLabel("Monthly Payment:");
displayPanel.add(paymentLa
// payment text field
paymentTextField = new JTextField(10);
displayPanel.add(paymentTe
// rate label
rateLabel = new JLabel("Yearly Interest Rate:");
displayPanel.add(rateLabel
// rate text field
rateTextField = new JTextField(10);
displayPanel.add(rateTextF
// years label
yearsLabel = new JLabel("Number of Years:");
displayPanel.add(yearsLabe
// years text field
yearsTextField = new JTextField(10);
displayPanel.add(yearsText
// future value label
futureValueLabel = new JLabel("Future Value:");
displayPanel.add(futureVal
// future value text field
futureValueTextField = new JTextField(10);
futureValueTextField.setEd
futureValueTextField.setFo
displayPanel.add(futureVal
// button panel
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGH
// calculate button
calculateButton = new JButton("Calculate");
calculateButton.addActionL
buttonPanel.add(calculateB
// exit button
exitButton = new JButton("Exit");
exitButton.addActionListen
buttonPanel.add(exitButton
// add panels to main panel
this.setLayout(new BorderLayout());
this.add(displayPanel, BorderLayout.CENTER);
this.add(buttonPanel, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEven
{
Object source = e.getSource();
if (source == exitButton)
System.exit(0);
else if (source == calculateButton)
{
double payment = Double.parseDouble(payment
double rate = Double.parseDouble(rateTex
int years = Integer.parseInt(yearsText
double futureValue = FinancialCalculations.calc
payment, rate, years);
NumberFormat currency = NumberFormat.getCurrencyIn
futureValueTextField.setTe
}
}
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
this ...trim().length() == 0
ensures that even if folks type some spaces in the field that will not be passing .
It would be better to do better chacek, because they
should enter strings which should evaluate to double ot int
thewre for you want to have more accurate check
which would make sure that they put not just something but something which can be evaluate as numbers