FileDocCategorySizeDatePackage
TextFieldDemo.javaAPI DocExample6729Tue Dec 12 18:59:16 GMT 2000None

TextFieldDemo

public class TextFieldDemo extends JFrame

Fields Summary
private double
amount
private double
rate
private int
numPeriods
private double
payment
private JLabel
amountLabel
private JLabel
rateLabel
private JLabel
numPeriodsLabel
private JLabel
paymentLabel
private static String
amountString
private static String
rateString
private static String
numPeriodsString
private static String
paymentString
private DecimalField
amountField
private DecimalField
rateField
private WholeNumberField
numPeriodsField
private DecimalField
paymentField
private NumberFormat
moneyFormat
private NumberFormat
percentFormat
private DecimalFormat
paymentFormat
private boolean
focusIsSet
Constructors Summary
public TextFieldDemo()


      
        super("TextFieldDemo");

        setUpFormats();

        payment = computePayment(amount, rate, numPeriods);

        //Create the labels.
        amountLabel = new JLabel(amountString);
        rateLabel = new JLabel(rateString);
        numPeriodsLabel = new JLabel(numPeriodsString);
        paymentLabel = new JLabel(paymentString);

        //Create the text fields and set them up.
        MyDocumentListener myDocumentListener = new MyDocumentListener();

        amountField = new DecimalField(amount, 10, moneyFormat);
        amountField.getDocument().addDocumentListener(myDocumentListener);
        amountField.getDocument().putProperty("name", "amount");

        rateField = new DecimalField(rate, 10, percentFormat);
        rateField.getDocument().addDocumentListener(myDocumentListener);
        rateField.getDocument().putProperty("name", "rate");

        numPeriodsField = new WholeNumberField(numPeriods, 10);
        numPeriodsField.getDocument().addDocumentListener(myDocumentListener);
        numPeriodsField.getDocument().putProperty("name", "numPeriods");

        paymentField = new DecimalField(payment, 10, paymentFormat);
        paymentField.setEditable(false);
        paymentField.setForeground(Color.red);

        //Tell accessibility tools about label/textfield pairs.
        amountLabel.setLabelFor(amountField);
        rateLabel.setLabelFor(rateField);
        numPeriodsLabel.setLabelFor(numPeriodsField);
        paymentLabel.setLabelFor(paymentField);

        //Layout the labels in a panel.
        JPanel labelPane = new JPanel();
        labelPane.setLayout(new GridLayout(0, 1));
        labelPane.add(amountLabel);
        labelPane.add(rateLabel);
        labelPane.add(numPeriodsLabel);
        labelPane.add(paymentLabel);

        //Layout the text fields in a panel.
        JPanel fieldPane = new JPanel();
        fieldPane.setLayout(new GridLayout(0, 1));
        fieldPane.add(amountField);
        fieldPane.add(rateField);
        fieldPane.add(numPeriodsField);
        fieldPane.add(paymentField);

        //Put the panels in another panel, labels on left,
        //text fields on right.
        JPanel contentPane = new JPanel();
        contentPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        contentPane.setLayout(new BorderLayout());
        contentPane.add(labelPane, BorderLayout.CENTER);
        contentPane.add(fieldPane, BorderLayout.EAST);

        setContentPane(contentPane);
    
Methods Summary
doublecomputePayment(double loanAmt, double rate, int numPeriods)

        double I, partial1, denominator, answer;

        I = rate / 100.0 / 12.0;         // get monthly rate from annual
        numPeriods *= 12;        // get number of months
        partial1 = Math.pow((1 + I), (0.0 - numPeriods));
        denominator = (1 - partial1) / I;
        answer = (-1 * loanAmt) / denominator;
        return answer;
    
public static voidmain(java.lang.String[] args)

        final TextFieldDemo demo = new TextFieldDemo();

        demo.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }

            //Whenever window gets the focus, let the
            //TextFieldDemo set the initial focus.
            public void windowActivated(WindowEvent e) {
                demo.setFocus();
            }
        });
        demo.pack();
        demo.setVisible(true);
    
private voidsetFocus()

        if (!focusIsSet) {
            amountField.requestFocus();
            focusIsSet = true;
        }
    
private voidsetUpFormats()

        moneyFormat = NumberFormat.getNumberInstance();
        //XXXX: Workaround. With an empty positive suffix
        //the format allows letters in the number.
        ((DecimalFormat)moneyFormat).setPositiveSuffix(" ");

        percentFormat = NumberFormat.getNumberInstance();
        percentFormat.setMinimumFractionDigits(3);
        //XXXX: Workaround. With an empty positive suffix
        //the format allows letters in the number.
        ((DecimalFormat)percentFormat).setPositiveSuffix(" ");

        paymentFormat = (DecimalFormat)NumberFormat.getNumberInstance();
        paymentFormat.setMaximumFractionDigits(2);
        paymentFormat.setNegativePrefix("(");
        paymentFormat.setNegativeSuffix(")");