Jonny Stewart
Jonny Stewart

Reputation: 161

JOptionPane showOptionDialog

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?

Upvotes: 4

Views: 50931

Answers (3)

droidchef
droidchef

Reputation: 2307

Object[] options = {"Metric","Imperial"};
int n = JOptionPane.showOptionDialog(null,
            "A Message",
            "A Title",
            JOptionPane.YES_NO_CANCEL_OPTION,
            JOptionPane.DEFAULT_OPTION,
            null,
            options,
            options[1]);  

System.out.println(n);  

JFrame metric = new JFrame("Metric");  
metric.setBounds(0, 0, 320, 240);  

JFrame imperial = new JFrame("Imperial");  
imperial.setBounds(0, 0, 320, 240);  

if(n==0){  
     metric.setVisible(true);  
}else if(n==1){
    imperial.setVisible(true);
}else{
    System.out.println("no option choosen");
}

Upvotes: 1

gadeynebram
gadeynebram

Reputation: 725

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
}

Upvotes: 12

matheuslf
matheuslf

Reputation: 309

Check out http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html it pretty much has everything you would need.

"As i understand it, JOptionPane is great for what it can do, but you can't really change the functionality beyond that (not easily). JDialog is better to inherit from if you want to create your own custom Dialogs"

Upvotes: 0

Related Questions