diff --git a/cs623/proj1/RetirementCalculator.java b/cs623/proj1/RetirementCalculator.java index af3c5dd..de7bfe1 100644 --- a/cs623/proj1/RetirementCalculator.java +++ b/cs623/proj1/RetirementCalculator.java @@ -18,10 +18,12 @@ class RetirementCalculatorPanel private JComponent makeDivider() { return new JLabel(" "); } private HashMap myFields; + private HashMap myToolTips; private JButton myCalculateButton; private JButton myPrintButton; private JButton myResetButton; private JLabel myResultBox; + private JLabel myToolTipBox; private JTextField myNameField; class ComboChoice @@ -53,14 +55,9 @@ class RetirementCalculatorPanel new ComboChoice(97, "97 (Female; 90th percentile)") }; - public RetirementCalculatorPanel() + private static int row = 0; + private void addRow(JPanel panel, JComponent c1, JComponent c2) { - super(new BorderLayout()); - - myModel = new RetirementCalculatorModel(); - myFields = new HashMap(); - JPanel inner = new JPanel(new GridBagLayout()); - JComponent field; GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; c.insets = new Insets(0, 4, 0, 4); @@ -68,143 +65,143 @@ class RetirementCalculatorPanel c.gridwidth = 1; c.gridheight = 1; c.gridx = 0; - c.gridy = 0; - c.weightx = 0.0; - inner.add(new JLabel("What is your name?", SwingConstants.RIGHT), c); + c.gridy = row; + panel.add(c1, c); + c.gridx++; c.weightx = 1.0; + panel.add(c2, c); + + row++; + } + private void addDollarRow(JPanel panel, JComponent c1, JComponent c2) + { + JPanel dollar_panel = new JPanel(); + dollar_panel.setLayout(new BoxLayout(dollar_panel, BoxLayout.X_AXIS)); + dollar_panel.add(new JLabel("$")); + dollar_panel.add(c2); + addRow(panel, c1, dollar_panel); + } + private void addRow(JPanel panel, JComponent c1) + { + GridBagConstraints c = new GridBagConstraints(); + c.fill = GridBagConstraints.HORIZONTAL; + c.insets = new Insets(0, 4, 0, 4); + c.ipadx = 2; + c.gridwidth = 2; + c.gridheight = 1; + c.gridx = 0; + c.weightx = 0.0; + c.gridy = row; + panel.add(c1, c); + + row++; + } + + public RetirementCalculatorPanel() + { + super(new BorderLayout()); + + myModel = new RetirementCalculatorModel(); + myFields = new HashMap(); + myToolTips = new HashMap(); + JPanel inner = new JPanel(new GridBagLayout()); + myToolTipBox = new JLabel(" ", SwingConstants.CENTER); + JComponent field; + myNameField = new JTextField(); myNameField.addFocusListener(this); - inner.add(myNameField, c); - c.gridx--; - c.gridy++; + myToolTips.put(myNameField, "Enter your name."); + myToolTipBox.setText("Enter your name."); + addRow(inner, new JLabel("What is your name?", SwingConstants.RIGHT), + myNameField); - c.weightx = 0.0; - inner.add(new JLabel("What is your age?", SwingConstants.RIGHT), c); - c.gridx++; - c.weightx = 1.0; field = new JTextField(); field.addFocusListener(this); myFields.put("age", field); - inner.add(field, c); - c.gridx--; - c.gridy++; + myToolTips.put(field, "Enter your current age."); + addRow(inner, new JLabel("What is your age?", SwingConstants.RIGHT), + field); - c.gridwidth = 2; - inner.add(makeDivider(), c); - c.gridwidth = 1; - c.gridy++; + addRow(inner, makeDivider()); - c.weightx = 0.0; - inner.add(new JLabel("How much annual income will you want in retirement?", SwingConstants.RIGHT), c); - c.gridx++; - c.weightx = 1.0; field = new JTextField(); field.addFocusListener(this); myFields.put("income wanted", field); - inner.add(field, c); - c.gridx--; - c.gridy++; + myToolTips.put(field, "Enter the amount of income that you would like to receive in a year while you are retired."); + addDollarRow(inner, + new JLabel("How much annual income will you want in retirement?", + SwingConstants.RIGHT), + field); - c.weightx = 0.0; - inner.add(new JLabel("What is your current annual income?", - SwingConstants.RIGHT), c); - c.gridx++; - c.weightx = 1.0; field = new JTextField(); field.addFocusListener(this); myFields.put("current income", field); - inner.add(field, c); - c.gridx--; - c.gridy++; + myToolTips.put(field, "Enter the amount of income that you currently receive in a year."); + addDollarRow(inner, + new JLabel("What is your current annual income?", + SwingConstants.RIGHT), + field); - c.gridwidth = 2; - inner.add(makeDivider(), c); - c.gridy++; - inner.add(new JLabel("What is your expected annual income after retirement -"), c); - c.gridwidth = 1; - c.gridy++; + addRow(inner, makeDivider()); + + addRow(inner, + new JLabel("What is your expected annual income after retirement -")); - c.weightx = 0.0; - inner.add(new JLabel(" - from an employer pension plan?", - SwingConstants.RIGHT), c); - c.gridx++; - c.weightx = 1.0; field = new JTextField(); field.addFocusListener(this); myFields.put("employer pension", field); - inner.add(field, c); - c.gridx--; - c.gridy++; + myToolTips.put(field, "If your employer has a pension plan that will pay you a certain amount after you retire from them, then enter that amount here."); + addDollarRow(inner, + new JLabel(" - from an employer pension plan?", + SwingConstants.RIGHT), + field); - c.weightx = 0.0; - inner.add(new JLabel(" - from a part-time job?", - SwingConstants.RIGHT), c); - c.gridx++; - c.weightx = 1.0; field = new JTextField(); field.addFocusListener(this); myFields.put("part time income", field); - inner.add(field, c); - c.gridx--; - c.gridy++; + myToolTips.put(field, "Enter how much income you expect to make working at a part-time job after retirement."); + addDollarRow(inner, + new JLabel(" - from a part-time job?", SwingConstants.RIGHT), + field); - c.weightx = 0.0; - inner.add(new JLabel(" - other?", SwingConstants.RIGHT), c); - c.gridx++; - c.weightx = 1.0; field = new JTextField(); field.addFocusListener(this); myFields.put("other income", field); - inner.add(field, c); - c.gridx--; - c.gridy++; + myToolTips.put(field, "If there is any other annual income you expect to receive after retirement not already accounted for, enter the amount here."); + addDollarRow(inner, new JLabel(" - other?", SwingConstants.RIGHT), field); - c.gridwidth = 2; - inner.add(makeDivider(), c); - c.gridwidth = 1; - c.gridy++; + addRow(inner, makeDivider()); - c.weightx = 0.0; - inner.add(new JLabel("How much savings do you have to date?", - SwingConstants.RIGHT), c); - c.gridx++; - c.weightx = 1.0; field = new JTextField(); field.addFocusListener(this); myFields.put("savings", field); - inner.add(field, c); - c.gridx--; - c.gridy++; + myToolTips.put(field, "Enter the amount of money that you have saved right now."); + addDollarRow(inner, + new JLabel("How much savings do you have to date?", + SwingConstants.RIGHT), + field); - c.weightx = 0.0; - inner.add(new JLabel("At what age do you expect to retire?", - SwingConstants.RIGHT), c); - c.gridx++; - c.weightx = 1.0; field = new JComboBox(retirement_age_choices); + field.addFocusListener(this); myFields.put("retirement age", field); - inner.add(field, c); - c.gridx--; - c.gridy++; + myToolTips.put(field, "Select the age at which you are planning to reture."); + addRow(inner, + new JLabel("At what age do you expect to retire?", + SwingConstants.RIGHT), + field); - JPanel p = new JPanel(new BorderLayout()); - p.setAlignmentX(Component.RIGHT_ALIGNMENT); - p.add(new JLabel("What is your life expectancy?", - SwingConstants.RIGHT), BorderLayout.CENTER); field = new JComboBox(life_expectancy_choices); + field.addFocusListener(this); myFields.put("life expectancy", field); - p.add(field, BorderLayout.EAST); - c.gridwidth = 2; - inner.add(p, c); - c.gridwidth = 1; - c.gridy++; + myToolTips.put(field, "Select what you think your life expectancy is."); + addRow(inner, + new JLabel("What is your life expectancy?", + SwingConstants.RIGHT), + field); - c.gridwidth = 2; - inner.add(makeDivider(), c); - c.gridwidth = 1; - c.gridy++; + addRow(inner, makeDivider()); JPanel buttons = new JPanel(); buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS)); @@ -221,21 +218,17 @@ class RetirementCalculatorPanel myResetButton.addActionListener(this); buttons.add(myResetButton); buttons.add(Box.createHorizontalGlue()); - c.gridwidth = 2; - inner.add(buttons, c); - c.gridwidth = 1; - c.gridy++; - c.gridwidth = 2; - inner.add(makeDivider(), c); - c.gridwidth = 1; - c.gridy++; + addRow(inner, makeDivider()); + + addRow(inner, myToolTipBox); + + addRow(inner, buttons); + + addRow(inner, makeDivider()); - c.gridwidth = 2; myResultBox = new JLabel(" ", SwingConstants.CENTER); - inner.add(myResultBox, c); - c.gridwidth = 1; - c.gridy++; + addRow(inner, myResultBox); JScrollPane scrollpane = new JScrollPane(inner, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, @@ -261,6 +254,11 @@ class RetirementCalculatorPanel { ((JTextField) e.getComponent()).selectAll(); } + String tooltip = myToolTips.get(e.getComponent()); + if (tooltip != null) + { + myToolTipBox.setText(tooltip); + } } public void focusLost(FocusEvent e)