by Dinesh Thakur Category: Swing

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");


About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles