Java application that displays the sum of two numbers. - Java Swing

Java examples for Swing:Focus Event

Introduction

Focus events are used to determine when the sum needs to be recalculated.

Demo Code

import java.awt.FlowLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Main extends JFrame implements FocusListener {
  JTextField value1, value2, sum;
  JLabel plus, equals;//  w w w  .  j  a  va 2 s  .  c o  m

  public Main() {
    super("Add Two Numbers");
    setSize(350, 90);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
    setLayout(flow);

    value1 = new JTextField("0", 5);
    plus = new JLabel("+");
    value2 = new JTextField("0", 5);
    equals = new JLabel("=");
    sum = new JTextField("0", 5);
    // add listeners
    value1.addFocusListener(this);
    value2.addFocusListener(this);
    // set up sum field
    sum.setEditable(false);
    // add components
    add(value1);
    add(plus);
    add(value2);
    add(equals);
    add(sum);
    setVisible(true);
  }

  public void focusGained(FocusEvent event) {
    try {
      float total = Float.parseFloat(value1.getText())
          + Float.parseFloat(value2.getText());
      sum.setText("" + total);
    } catch (NumberFormatException nfe) {
      value1.setText("0");
      value2.setText("0");
      sum.setText("0");
    }
  }

  public void focusLost(FocusEvent event) {
    focusGained(event);
  }

  public static void main(String[] arguments) {
    Main frame = new Main();
  }
}

Related Tutorials