From b5d3fb6e74e2873afa63e000cef4a50ff15efbe7 Mon Sep 17 00:00:00 2001 From: josh Date: Tue, 6 Oct 2009 16:18:20 +0000 Subject: [PATCH] added focus listeners to the test fields; formatting "print" output with commas, removed decimal part of age fields in View git-svn-id: svn://anubis/gvsu@451 45c1a28c-8058-47b2-ae61-ca45b979098e --- cs623/proj1/RetirementCalculator.java | 49 ++++++++++++++++++++------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/cs623/proj1/RetirementCalculator.java b/cs623/proj1/RetirementCalculator.java index 37b161e..0290a0c 100644 --- a/cs623/proj1/RetirementCalculator.java +++ b/cs623/proj1/RetirementCalculator.java @@ -74,6 +74,7 @@ class RetirementCalculatorPanel c.gridx++; c.weightx = 1.0; myNameField = new JTextField(); + myNameField.addFocusListener(this); inner.add(myNameField, c); c.gridx--; c.gridy++; @@ -83,6 +84,7 @@ class RetirementCalculatorPanel c.gridx++; c.weightx = 1.0; field = new JTextField(); + field.addFocusListener(this); myFields.put("age", field); inner.add(field, c); c.gridx--; @@ -98,6 +100,7 @@ class RetirementCalculatorPanel c.gridx++; c.weightx = 1.0; field = new JTextField(); + field.addFocusListener(this); myFields.put("income wanted", field); inner.add(field, c); c.gridx--; @@ -109,6 +112,7 @@ class RetirementCalculatorPanel c.gridx++; c.weightx = 1.0; field = new JTextField(); + field.addFocusListener(this); myFields.put("current income", field); inner.add(field, c); c.gridx--; @@ -127,6 +131,7 @@ class RetirementCalculatorPanel c.gridx++; c.weightx = 1.0; field = new JTextField(); + field.addFocusListener(this); myFields.put("employer pension", field); inner.add(field, c); c.gridx--; @@ -138,6 +143,7 @@ class RetirementCalculatorPanel c.gridx++; c.weightx = 1.0; field = new JTextField(); + field.addFocusListener(this); myFields.put("part time income", field); inner.add(field, c); c.gridx--; @@ -148,6 +154,7 @@ class RetirementCalculatorPanel c.gridx++; c.weightx = 1.0; field = new JTextField(); + field.addFocusListener(this); myFields.put("other income", field); inner.add(field, c); c.gridx--; @@ -164,6 +171,7 @@ class RetirementCalculatorPanel c.gridx++; c.weightx = 1.0; field = new JTextField(); + field.addFocusListener(this); myFields.put("savings", field); inner.add(field, c); c.gridx--; @@ -246,6 +254,18 @@ class RetirementCalculatorPanel print(); } + public void focusGained(FocusEvent e) + { + if (e.getComponent().getClass() == JTextField.class) + { + ((JTextField) e.getComponent()).selectAll(); + } + } + + public void focusLost(FocusEvent e) + { + } + private void calculate() { Iterator it = myFields.keySet().iterator(); @@ -285,7 +305,8 @@ class RetirementCalculatorPanel { JTextField tf = (JTextField) comp; double val = myModel.getField(key); - tf.setText((new Double(val)).toString()); + tf.setText(String.format( + key.equals("age") ? "%.0f" : "%.2f", val)); } else if (comp.getClass() == JComboBox.class) { @@ -324,30 +345,32 @@ class RetirementCalculatorPanel String endl = "
"; String msg = ""; msg += "Name: " + myNameField.getText() + endl; - msg += "Current income: $" + fmt("%.02f", "current income") + endl; + msg += "Age: " + fmt("%.0f", "age") + endl; + msg += "Retirement age: " + fmt("%.0f", "retirement age") + endl; + msg += "Current income: $" + fmt("%,.02f", "current income") + endl; msg += "Desired annual income: $" + - fmt("%.02f", "income wanted") + endl; + fmt("%,.02f", "income wanted") + endl; msg += endl; msg += "Social Security benefit: $" + - fmt("%.02f", "social security") + endl; + fmt("%,.02f", "social security") + endl; msg += "Traditional Employer Pension: $" + - fmt("%.02f", "employer pension") + endl; - msg += "Part-time income: $" + fmt("%.02f", "part time income") + endl; - msg += "Other income: $" + fmt("%.02f", "other income") + endl; + fmt("%,.02f", "employer pension") + endl; + msg += "Part-time income: $" + fmt("%,.02f", "part time income") + endl; + msg += "Other income: $" + fmt("%,.02f", "other income") + endl; msg += "Amount to make up each retirement year: $" + - fmt("%.02f", "amount to make up") + endl; + fmt("%,.02f", "amount to make up") + endl; msg += endl; msg += "Total amount to save: $" + - fmt("%.02f", "amount to save") + endl; + fmt("%,.02f", "amount to save") + endl; msg += "Early retirement addition: $" + - fmt("%.02f", "early retirement penalty") + endl; + fmt("%,.02f", "early retirement penalty") + endl; msg += "Estimated savings at retirement: $" + - fmt("%.02f", "estimated savings") + endl; + fmt("%,.02f", "estimated savings") + endl; msg += endl; msg += "Total additional savings needed at retirement: $" + - fmt("%.02f", "total additional savings needed") + endl; + fmt("%,.02f", "total additional savings needed") + endl; msg += "Required annual savings: $" + - fmt("%.02f", "annual savings needed") + endl; + fmt("%,.02f", "annual savings needed") + endl; msg += ""; JOptionPane.showMessageDialog(null, msg, "Basic Retirement Calculator", JOptionPane.PLAIN_MESSAGE);