JOptionPane showOptionDialog

Jonny Stewart picture Jonny Stewart · Nov 20, 2012 · Viewed 45.6k times · Source

I want to create a showOptionDialog using JOptionPane that has two buttons: Metric and Imperial. If say, Metric is clicked on, the Metric GUI will load. Conversely, if Imperial is clicked on, then the Imperial GUI will load.

How do I do this? Many thanks.

Answer

gadeynebram picture gadeynebram · Nov 20, 2012
int choice = JOptionPane.showOptionDialog(null, //Component parentComponent
                               "Metric or Imperial?", //Object message,
                               "Choose an option", //String title
                               JOptionPane.YES_NO_OPTION, //int optionType
                               JOptionPane.INFORMATION_MESSAGE, //int messageType
                               null, //Icon icon,
                               {"Metric","Imperial"}, //Object[] options,
                               "Metric");//Object initialValue 
if(choice == 0 ){
   //Metric was chosen
}else{
   //Imperial was chosen
}