Search code examples
javajoptionpane

Getting selected value from a JOptionPane input dialog


I'm trying to figure out how to get a selected value from a JOptionPane dialog box with a combo box (well, sort of).

Here is the code:

String [] locations = { "Brisbane", "Sydney", "Melbourne" };
Object city = (String) JOptionPane.showInputDialog(
    null,
    "Location?",
    "Hills School",
    JOptionPane.PLAIN_MESSAGE,
    null,
    locations,
    locations[0]);

As you can probably see I have not actually used a combo box, which is where I am having the issue. It still appears as a combo box in the JOptionPane, but I can't use getSelectedValue(). I have checked the the java website but can't seem to find an answer as to how I can get the selected value.

Is there a way to accomplish this or should I come at the JOptionPane from a different angle?

Thanks


Solution

  • The selected value is in your city variable, which is a String according to the casting.