I'm trying to add a focusListener to JDateChooser, so that when the user chooses the dateOfBirth it displays age on the textfield when the focus is lost. But it is not working This is the way i've tried,
import java.awt.BorderLayout;
public class agecalc extends JFrame {
private JTextField age;
private JDateChooser dOB;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
agecalc frame = new agecalc();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public agecalc() {
JPanel panel = new JPanel();
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 290, GroupLayout.PREFERRED_SIZE)
.addContainerGap(134, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(20)
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 195, GroupLayout.PREFERRED_SIZE)
.addContainerGap(47, Short.MAX_VALUE))
);
panel.setLayout(null);
dOB = new JDateChooser();
dOB.setBounds(97, 52, 95, 20);
panel.add(dOB);
age = new JTextField();
age.setBounds(97, 83, 86, 20);
panel.add(age);
age.setColumns(10);
getContentPane().setLayout(groupLayout);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
AGECalc ageCalc=new AGECalc();
dOB.addFocusListener(ageCalc);
}
private class AGECalc implements FocusListener{
@Override
public void focusGained(FocusEvent e) {
}
@Override
public void focusLost(FocusEvent e) {
// TODO Auto-generated method stub
Calendar fromDate;
Calendar toDate;
Date dateOfBirth= dOB.getDate();
Date now = new Date();
fromDate=Calendar.getInstance();
toDate=Calendar.getInstance();
fromDate.setTime(dateOfBirth);
toDate.setTime(now);
int ageYear = toDate.get(Calendar.YEAR) - (fromDate.get(Calendar.YEAR) );
age.setText(Integer.toString(ageYear));
System.out.println(ageYear);
}
}
public JTextField getAge() {
return age;
}
public void setAge(JTextField age) {
this.age = age;
}
public JDateChooser getdOB() {
return dOB;
}
public void setdOB(JDateChooser dOB) {
this.dOB = dOB;
}
}