Search code examples
javastringintjoptionpane

JOptionPane getting a String from showOptionDialog


So this is a small edited snippet of code from a project im working on and I need to be able to get the string value from this showOptionDialog and display it in a showMessageDialog but I cannot for the life of me figure out how and unfortunately the only other question similar to mine on this website doesn't do me any justice.

    String[] buttons = { "Female", "Male" };
    int response = JOptionPane.showOptionDialog(null, "are you Male or Female?", "Important Question.",
            JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, buttons, buttons[0]);

    JOptionPane.showMessageDialog(null, "You selected that you are a, " + response);

so in showMessageDialog depending on which button you press it should read out something like this: "You selected that you are a, Female" or male if the user chose that option.

also yes I realize that I put an int variable in the showMessageDialog, I just put it there as a placeholder.

any help would be much appreciated.


Solution

  • What you need to do is:

    ...
    JOptionPane.showMessageDialog(null, "You selected that you are a, " + buttons[response]);