• Post Reply Bookmark Topic Watch Topic
  • New Topic

JOptionPane.showOptionDialog default value not working?  RSS feed

 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm trying to use showOptionDialog to input a user choice as integer. However, if the user simply closes the dialog window, I'd like the method to return 3, for the fourth option. I thought the last argument in showOptionDialog would let me do this (because the last argument is called 'default value' according to the java oracle documentaion), but with the following code, I keep getting -1 when I just close the dialog window. Anyone know why and/or how to fix this?

 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The documentation doesn't exactly say so specifically, but it seems to me that the "initialValue" parameter ought to be one of the options that the user can select from. And it certainly doesn't say that you're supposed to pass the index of the default option in the options array, which is what you did. So try passing the actual option for the "initialValue" parameter.

Edit: actually, have a look at the examples in the documentation to see how they differ from your code.
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point, I changed my code so that the line in question now reads:



However, I am still encountering the same problem.
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's probably documented behaviour as well. I wouldn't find it surprising if a non-answer was returned if the user didn't select any option, and that's what seems to be happening. But what does the documentation say?
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know I can't find that in the documentation. However, I just fixed it with the following line:



Thanks, all!
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Louis Lewis wrote:I don't know I can't find that in the documentation.


The documentation wrote:Returns:
an integer indicating the option chosen by the user, or CLOSED_OPTION if the user closed the dialog
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!