import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class FocusTest extends JFrame implements FocusListener {
private GridBagLayout gbl = new GridBagLayout();
private boolean firstVisit = false;
private JTextField txtFieldFirstName = new JTextField(10);
private JTextField txtFieldLastName = new JTextField();
private JTextField txtFieldStreet = new JTextField();
private JTextField txtFieldZip = new JTextField();
private JTextField txtFieldPlace = new JTextField();
private JLabel lblFirstName = new JLabel("First Name:");
private JLabel lblLastName = new JLabel("Last Name:");
private JLabel lblStreet = new JLabel("Street:");
private JLabel lblZip = new JLabel("ZIP:");
private JLabel lblPlace = new JLabel("Place:");
FocusTest() {
super("FocusTest");
initializeComponents();
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private void initializeComponents() {
setLayout(gbl);
txtFieldFirstName.addFocusListener(this);
txtFieldLastName.addFocusListener(this);
txtFieldStreet.addFocusListener(this);
txtFieldZip.addFocusListener(this);
txtFieldPlace.addFocusListener(this);
gbl.setConstraints(lblFirstName, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(10, 10, 0, 10), 0, 0));
gbl.setConstraints(txtFieldFirstName, new GridBagConstraints(1, 0, 1, 1, 2, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10, 0, 0, 10), 0, 0));
gbl.setConstraints(lblLastName, new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 10, 0, 10), 0, 0));
gbl.setConstraints(txtFieldLastName, new GridBagConstraints(1, 1, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 10), 0, 0));
gbl.setConstraints(lblStreet, new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(10, 10, 0, 10), 0, 0));
gbl.setConstraints(txtFieldStreet, new GridBagConstraints(1, 2, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10, 0, 0, 10), 0, 0));
gbl.setConstraints(lblZip, new GridBagConstraints(0, 3, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 10, 0, 10), 0, 0));
gbl.setConstraints(txtFieldZip, new GridBagConstraints(1, 3, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 10), 0, 0));
gbl.setConstraints(lblPlace, new GridBagConstraints(0, 4, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 10, 10, 10), 0, 0));
gbl.setConstraints(txtFieldPlace, new GridBagConstraints(1, 4, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 10, 10), 0, 0));
add(txtFieldFirstName);
add(txtFieldLastName);
add(txtFieldStreet);
add(txtFieldZip);
add(txtFieldPlace);
add(lblFirstName);
add(lblLastName);
add(lblStreet);
add(lblZip);
add(lblPlace);
}
private void showInfo() {
JOptionPane.showMessageDialog(this, "Check Data for Street, Zip and Place", "Validate", JOptionPane.INFORMATION_MESSAGE);
firstVisit = false;
}
public void focusGained(FocusEvent e) {
Object o = e.getSource();
if(o.equals(txtFieldStreet)) { firstVisit = true; }
else if(o.equals(txtFieldZip)) { firstVisit = true; }
else if(o.equals(txtFieldPlace)) { firstVisit = true; }
else if(o.equals(txtFieldFirstName) && firstVisit) { showInfo(); }
else if(o.equals(txtFieldLastName) && firstVisit) { showInfo(); }
}
public void focusLost(FocusEvent e) {
}
public static void main(String args[]) {
new FocusTest();
}
}