Search code examples
javajoptionpane

How to display JOptionPane dialogbox of different instances?


I created different instances of JOptionPane in the if clause as shown as below :

JOptionPane myInstance ;
if(someCondition is true){
     myInstance = new JOptionPane(ErrorMessage,JOptionPane.ERROR_MESSAGE);
}
else{
    myInstance = new JOptionPane(InformationMessage,JOptionPane.INFORMATION_MESSAGE);
}

Now how to display those instance of JOptionPane?? I tried this:

myInstance.setVisible(true)

but it didn't work for me. How to display these instances???


Solution

  • Try it in this way:

    JOptionPane myInstance ;
    if(someCondition is true){
         myInstance = new JOptionPane(ErrorMessage,JOptionPane.ERROR_MESSAGE);
    }
    else{
        myInstance = new JOptionPane(InformationMessage,JOptionPane.INFORMATION_MESSAGE);
    }
    
    JDialog dialog = myInstance.createDialog(parentComponent/* null for new window*/, title);
    dialog.show();