0

I have a method getDate which returns String value. I'd like to add custom buttons but I can't because Eclipse tells me to change method type to int instead of String. What can I do here?

public String getDate(String info){
          return JOptionPane.showInputDialog(null, info, "Date insertion", JOptionPane.PLAIN_MESSAGE);
    }

This is how this InputDialog looks like:

IMAGE

I want this dialog to have let's say Yes/No/Quit buttons and if I understand correctly, code like this should work:

public String getDate(String info){
      return JOptionPane.showOptionDialog(null,info,
                "Date insertion", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,null,options,
                null);
}

But it says "cannot convert from int to String" and I need it to return String not int.

1 Answers1

0

Have the class that invokes JOptionPane.showOptionDialog() get the integer that it returns, and use that to choose the string that you want to return. You cannot change the showOptionDialog() method to return a string, but you can decide, based on the int that it returns, what string you want the caller to get. There's no need for a custom dialog.

public String getDate(String info)
{
  int[] optionStrings = { "Yes", "No", "Cancel" };
  int option = JOptionPane.showOptionDialog(null, info, "Date insertion",
                        JOptionPane.YES_NO_CANCEL_OPTION, 
                        JOptionPane.QUESTION_MESSAGE,null,options,
                        null);
  return optionStrings[option];
}
arcy
  • 12,845
  • 12
  • 58
  • 103