Tuesday, June 19, 2007

Leveraging the most out of JOptionPane

How often have we noticed the user being presented with a simple dialog box, used to display a warning. When it comes to Java Swings, crudely the most easiest yet powerful component is JOptionPane. But, often Developers fall out on using this niche class to its fullest potential.

JOptionPane offers such smaller APIS that can be used to prompt user with varied options. Ranging from simple dialog with OK button to as complex to retrieve details. You might be surprised to know that this can contain tabbed panels with EventHandlers as well.

Browse through the source code of JOptionPane.

The following two APIs provide so much options for developers to present crisp yet powerful Dialogs.

953: public static int showConfirmDialog(Component parentComponent,
954: Object message, String title,
955: int optionType, int messageType)

1084: public static Object showInputDialog(Component parentComponent,
1085: Object message, String title,
1086: int messageType, Icon icon,
1087: Object[] selectionValues,
1088: Object initialSelectionValue)

P.S:
1. Observe the second parameter is not just a String, its an Object. It could be a Component too!!!!!
2. In showInputDialog, the 6th option is Object[] selectionValues, Even this could be complex to a considerable degree and can contain tabbed panels with corresponding eventHandlers as well!!!!

So, next time when you are using JOptionPane, dont limit its usage to mere showing up dialogs. If need be push it into effective use.