Search code examples
javajoptionpanemessagebox

YES_NO_OPTION JOptionPane continue java code on 'yes'


Basically I have a game that brings up the JOptionPane when something happens, I want to be able to return to the game when the user clicks yes. Kind of like an unpause function


Solution

  • NOTE: When I posted this answer, the question was quite different - read the comments.

    Since OpenJDK is open-source (GNU General Public License version 2), you can look at its source code. I usually browse Java source code on grepcode.com. If you install the OracleJDK and select the option to install sources too, you can also find most of the source code in the JDK install directory (src.zip). Just beware, that the license for that might not permit you to reuse this code (it is usually the same) for yourself (but it's certainly a lot better than using a decompiler).

    These links to the specific methods don't work in my favourite browser (Vivaldi, based on Chrome). If you don't wish to have to look for the specific methods yourself, I recommend that you use Firefox.

    Here are the relevant snippets from grepcode.com:

    JOptionPane (constructor) - this is called by every show...Dialog method:

    1830  public JOptionPane(Object message, int messageType, int optionType,
    1831                     Icon icon, Object[] options, Object initialValue) {
              ...
    1838      setOptionType(optionType);
              ...
    1841      updateUI();
    1842  }
    

    JOptionPane (updateUI):

    1877  public void updateUI() {
    1878      setUI((OptionPaneUI)UIManager.getUI(this));
    1879  }
    

    Here we see, that the JOptionPane requests an OptionPaneUI from the UIManager. OptionPaneUI is an abstract class (that looks more like an interface), so you can't find any code there. Its only subclasses are BasicOptionPaneUI or MultiOptionPaneUI. Using the debugger, I found that it is BasicOptionPaneUI for a showConfirmDialog. Afterwards, the result is passed into the setUI method inherited from JComponent. Other than some basic field checking, it calls the ui.installUI method:

    137   public void installUI(JComponent c) {
    138       optionPane = (JOptionPane)c;
    139       installDefaults();
    140       optionPane.setLayout(createLayoutManager());
    141       installComponents();
    142       installListeners();
    143       installKeyboardActions();
    144   }
    

    Let's look at installComponents next:

    171   protected void More ...installComponents() {
    172       optionPane.add(createMessageArea());
    173 
    174       Container separator = createSeparator();
    175       if (separator != null) {
    176           optionPane.add(separator);
    177       }
    178       optionPane.add(createButtonArea());
    179       optionPane.applyComponentOrientation(optionPane.getComponentOrientation());
    180   }
    

    createButtonArea sound quite promising:

    613   protected Container createButtonArea() {
    614       JPanel bottom = new JPanel();
              ...
    630       addButtonComponents(bottom, getButtons(), getInitialValueIndex());
    631       return bottom;
    632   }
    

    This method now calls addButtonComponents. This method is too long to copy here, but, in short, it gets the locale-specific strings for the buttons and adds them as JButtons. It then gives each of them a ButtonActionListener.