Search code examples
javaappletfocusjoptionpane

JOptionPane.showOptionDialog does not always move to front in Applet


I have a JOptionPane popup in my applet normally, a-la:

      Object[] options = {"Grade", "Save", "Cancel"};
      selection = JOptionPane.showOptionDialog(this,
      "Do you want to grade now or save your work to continue later?",
      "Grade Or Save",
      JOptionPane.DEFAULT_OPTION,
      JOptionPane.QUESTION_MESSAGE,
      null, 
      options,
      options[2]);

this refers to the JApplet object.

The popup works fine and everything, but occasionally it will appear behind the applet instead of popping up in front of it.


Solution

  • Unknowingly you not may be passing in the parent component; specifically "this" into the showOptionDialog(). Make sure "this" is actually the parent component.

    If "this" refers to a Frame you can find what frame is in focus by doing the following:

    (pseduo code)

    myFrames[] = Frame.getFrames();

    if ( myFrames[i].isFocused() ) frame to pass in :)