Search code examples
javaswinguser-interfacejoptionpane

Need help displaying a list to a user and getting their response using Swing


This is the second part of a presentation that I am working on.

String temp;

// Create the class
Hello helloUser = new Hello();

//Get the users name
temp = JOptionPane.showInputDialog("Please enter your name?");
helloUser.setName(temp);

String hello = helloUser.name(helloUser.getName());

//Greet the user
temp = JOptionPane.showInputDialog(null, hello, "Feeling", 
        JOptionPane.QUESTION_MESSAGE, null, new Object[]{
            "Great", "Good", "Been Better"
        });
helloUser.setFeeling(temp);

After I get the users name I want the program to greet them and ask how they are doing, then provide them a selection to choose from for the answer. The code above for greeting the user keeps giving me this error:

no suitable method found for showInputDialog(<null>,String,String,int,<null>,Object[]) method JOptionPane.showInputDialog(Component,Object,String,int,Icon,Object[],Object) is not applicable (actual and formal argument lists differ in length)

I would like to give the user a list to choose from and store their selection in temp. Can I do that with JOptionPane? If so how?


Solution

  • Your current parameter list does not fit any of the overloaded JOptionPane.showInputDialog() methods provided.

    If you provide the selectionValues parameter, you must also provide initialValue.

    Try this instead:

    Object[] options = {"Great", "Good", "Been Better"};
    temp = JOptionPane.showInputDialog(null, 
                                       hello, 
                                       "Feeling", 
                                       JOptionPane.QUESTION_MESSAGE, 
                                       null,  
                                       options, 
                                       options[0]);