Kenta Nebgen
Kenta Nebgen

Reputation: 1

Can't seem to get the JOptionPane drop-down to work properly

I'm pretty sure that it should be fine but I keep getting this warning!

String[] trollDo= {"Try and sneak the horses away.",
                   "Go back and tell the others.", 
                   "Kill the trolls."};
String trollChoice =(String) JOptionPane.showInputDialog(null,
           "What will "+playerName+" do?",null,JOptionPane.QUESTION_MESSAGE,
           trollDo,trollDo[0]);

Upvotes: 0

Views: 186

Answers (2)

MadukaJ
MadukaJ

Reputation: 761

You have to give proper parameters.

public static Object showInputDialog(Component parentComponent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue) throws HeadlessException

 JOptionPane.showInputDialog(null, e, playerName, messageType, null, trollDo, playerName);

Prompts the user for input in a blocking dialog where the initial selection, possible selections, and all other options can be specified. The user will able to choose from selectionValues, where null implies the user can input whatever they wish, usually by means of a JTextField. initialSelectionValue is the initial value to prompt the user with. It is up to the UI to decide how best to represent the selectionValues, but usually a JComboBox, JList, or JTextField will be used.

Parameters:

parentComponent - the parent Component for the dialog

message - the Object to display

title - the String to display in the dialog title bar

messageType - the type of message to be displayed: ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE

icon - the Icon image to display

selectionValues - an array of Objects that gives the possible selections

initialSelectionValue - the value used to initialize the input field

Returns:

user's input, or null meaning the user canceled the input

Throws:

HeadlessException - if GraphicsEnvironment.isHeadless returns true

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347314

You should be getting a compile error, something like...

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

Which means you are missing the icon parameter...

String trollChoice = (String) JOptionPane.showInputDialog(
                null,
                "What will " + playerName + " do?", 
                null,
                JOptionPane.QUESTION_MESSAGE,
                null, // This one here
                trollDo, 
                trollDo[0]);

Make sure you're consulting the JavaDocs and making use of your IDE to select and fill method parameters

Upvotes: 3

Related Questions