Source of FormattedAdderDialog.java


  1: import java.awt.*;
  2: import javax.swing.*;
  3: import java.awt.event.*;
  4: import java.text.Format;
  5: import java.text.NumberFormat;

  7: /**
  8:  * FormattedAdderDialog is fully functional and has cosmetic enhancements
  9:  * compared to the earlier AdderDialogs.
 10:  *
 11:  * @author Mark Young (A00000000)
 12:  */
 13: public class FormattedAdderDialog extends JFrame {

 15:     /**
 16:      * Create an AdderDialog window.
 17:      */
 18:     public FormattedAdderDialog() {
 19:         super("Adding Numbers -- Formatted");
 20:         setSize(600, 250);
 21:         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 22:         makeTheBody();
 23:         // FontFixer.resizeFont(this, 24);
 24:     }

 26:     /**
 27:      * This method actually puts the body of the window together.
 28:      * It also activates the buttons.
 29:      */
 30:     private void makeTheBody() {
 31:         // create the formatting objects
 32:         Format intFormat = NumberFormat.getIntegerInstance();
 33:         Insets margin = new Insets(5, 5, 5, 5);

 35:         // Create the buttons and fields
 36:         JButton calculate = new JButton("Calculate");
 37:         JButton done = new JButton("Done");
 38:         JFormattedTextField box1 = new JFormattedTextField(intFormat);
 39:         JFormattedTextField box2 = new JFormattedTextField(intFormat);
 40:         JFormattedTextField result = new JFormattedTextField(intFormat);

 42:         // format the fields: inside margins; right-align; result uneditable
 43:         box1.setMargin(margin);
 44:         box2.setMargin(margin);
 45:         result.setMargin(margin);
 46:         box1.setHorizontalAlignment(JTextField.RIGHT);
 47:         box2.setHorizontalAlignment(JTextField.RIGHT);
 48:         result.setHorizontalAlignment(JTextField.RIGHT);
 49:         result.setEditable(false);

 51:         // make the ENTER and Alt-ENTER key strokes do the calculation
 52:         getRootPane().setDefaultButton(calculate);
 53:         calculate.setMnemonic(KeyEvent.VK_ENTER);

 55:         // set the layout
 56:         super.setLayout( new GridLayout(5, 2, 10, 5) );

 58:         // add the components
 59:         super.add(new JLabel("Enter two numbers to add together:"));
 60:         super.add(new JLabel(""));   // empty cell!
 61:         super.add(new JLabel("First number"));
 62:         super.add(box1);
 63:         super.add(new JLabel("Second number:"));
 64:         super.add(box2);
 65:         super.add(new JLabel("Result:"));
 66:         super.add(result);
 67:         super.add(calculate);
 68:         super.add(done);

 70:         // add actions to the buttons
 71:         calculate.addActionListener(e -> addTheNumbers(box1, box2, result));
 72:         done.addActionListener(e -> System.exit(0));
 73:     }

 75:     /**
 76:      * Add the numbers from the first two fields and put the result into the
 77:      * third. The arguments must be formatted as integers.
 78:      *
 79:      * @param f1 text field holding the first number
 80:      * @param f2 text field holding the second number
 81:      * @param sum text field to hold the result
 82:      */
 83:     public void addTheNumbers(
 84:             JFormattedTextField box1, 
 85:             JFormattedTextField box2, 
 86:             JFormattedTextField result
 87:     ) {
 88:         Integer num1 = ((Number)box1.getValue()).intValue();
 89:         Integer num2 = ((Number)box2.getValue()).intValue();
 90:         Integer sum = num1 + num2;
 91:         result.setValue(sum);
 92:     }

 94:     /**
 95:      * Create and show the window.
 96:      *
 97:      * @param args command line arguments cheerfully ignored!
 98:      */
 99:     public static void main(String[] args) {
100:         FormattedAdderDialog win = new FormattedAdderDialog();
101:         win.setVisible(true);
102:     }
103: }