by Dinesh Thakur

The option dialog box provides the feature of all the above three discussed dialog boxes. It is created by using the static method showOptionDialog () of JOptionPane. The general form of showOptionDialog () method is

public static int showOptionDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue))


parentComponent is the frame in which the dialog box is displayed

message is the message to be displayed

title is a string of text to be displayed in the title bar of the dialog box

optionType specifies options available on the dialog box such as YES_NO_OPTION or YES_NO_CANCEL_OPTION

messageType specifies the type of message such as error message, information message, warning message, question message or plain message

icon specifies the icon on the dialog box

options allows the user to use components other than buttons

initialValue determines the initial choice

import java.util.*;

import javax.swing.*;
public class JavaExampleAgeCalculator
    public static void main(String[] as)
               GregorianCalendar Current = new GregorianCalendar();
       int CrntYear;
       int YearofBrth;
       int YrsOld;
       YearofBrth = Integer.parseInt(JOptionPane.showInputDialog(null,"In Which year You Were born?"));
       CrntYear = Current.get(GregorianCalendar.YEAR);
       YrsOld = CrntYear - YearofBrth;JOptionPane.showMessageDialog(null,"At Present year you become " + YrsOld +" Years Old");