Java Age Calculator Swing application is developed using Absolute Layout Manager and Button Action Listener Implementation.
 
Inner Action Listener can be defined on Button as below;

JButton b1 = new JButton("Calculate");
b1.addActionListener(new ActionListener()
 {
   public void actionPerformed(ActionEvent e)
   {
     calculateAge();	  
   }
 });

 
Refer YouTube tutorial with detailed explanation (part 1 and part 2)
 

 

 
Complete class AgeCalculator.java can be found below;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

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


public class AgeCalculator{
	static JTextField t1;
	static JTextField t2;
	static JTextField t3;
	
	public static void addComponentsToPane(Container pane) {
        pane.setLayout(null);
 
        JLabel l1 = new JLabel("Enter Date:");
        pane.add(l1);
        Insets insets = pane.getInsets();
        Dimension size = l1.getPreferredSize();
        l1.setBounds(25 + insets.left, 5 + insets.top,
                     size.width, size.height);
        
        t1 = new JTextField();
        pane.add(t1);
        insets = pane.getInsets();
        size = t1.getPreferredSize();
        t1.setBounds(125 + insets.left, 5 + insets.top,
                     100, size.height);
        
        JLabel l2 = new JLabel("Enter Month:");
        pane.add(l2);
        insets = pane.getInsets();
        size = l2.getPreferredSize();
        l2.setBounds(25 + insets.left, 35 + insets.top,
                     size.width, size.height);
        
        t2 = new JTextField();
        pane.add(t2);
        insets = pane.getInsets();
        size = t2.getPreferredSize();
        t2.setBounds(125 + insets.left, 35 + insets.top,
                     100, size.height);
        
        JLabel l3 = new JLabel("Enter Year:");
        pane.add(l3);
        insets = pane.getInsets();
        size = l3.getPreferredSize();
        l3.setBounds(25 + insets.left, 65 + insets.top,
                     size.width, size.height);
        
        t3 = new JTextField();
        pane.add(t3);
        insets = pane.getInsets();
        size = t3.getPreferredSize();
        t3.setBounds(125 + insets.left, 65 + insets.top,
                     100, size.height);
        
        
        JButton b1 = new JButton("Calculate");
        b1.addActionListener(new ActionListener()
        {
    	  public void actionPerformed(ActionEvent e)
    	  {
    		  calculateAge();	  
    	  }
    	});
        pane.add(b1);
        insets = pane.getInsets();
        size = b1.getPreferredSize();
        b1.setBounds(125 + insets.left, 105 + insets.top,
                     size.width, size.height);

    }
 
	public static void calculateAge(){
		
		int year = Integer.parseInt(t3.getText());
		int month = Integer.parseInt(t2.getText());
		month = month - 1; // In Calendar object month starts from 0.
		int date = Integer.parseInt(t1.getText());	
		
		// First prepare today calendar object
		Calendar today = Calendar.getInstance();
		today.setTimeInMillis(System.currentTimeMillis());
		
		// Birthday calendar object
		Calendar birthDay = Calendar.getInstance();
		birthDay.set(year, month, date);
		
		// Get diff between years
		int ageYears = today.get(Calendar.YEAR) - birthDay.get(Calendar.YEAR);
		
		// Get diff between months
		int ageMonths = 0;
		System.out.println(birthDay.get(Calendar.MONTH));
		if(today.get(Calendar.MONTH) < birthDay.get(Calendar.MONTH)){
			ageMonths = 12 - (birthDay.get(Calendar.MONTH) - today.get(Calendar.MONTH));
			ageYears = ageYears - 1; // Adjust the years if the current month is behind the birth date month
		}else{
			ageMonths =  today.get(Calendar.MONTH) - birthDay.get(Calendar.MONTH);
		}
		
		JOptionPane.showMessageDialog(null,"Your Age is: "+ ageYears + " years and "+ageMonths + " months");
	}
	
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Age Calculator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        //Set up the content pane.
        addComponentsToPane(frame.getContentPane());
 
        //Size and display the window.
        Insets insets = frame.getInsets();
        frame.setSize(300 + insets.left + insets.right,
                      200 + insets.top + insets.bottom);
        frame.setVisible(true);
    }
 
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

Share this post

Java Age Calculator Swing application
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *