This week's giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Production-Ready Serverless (Operational Best Practices) and have Yan Cui on-line!
See this thread for details.
Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Here is how to add default values in a JOptionPane.showInputDialog  RSS feed

 
Ranch Hand
Posts: 37
1
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wasn't sure where to post this or if there was a helps forum, but having asked many questions here, I thought I would contribute a solution or helper that I found valuable and would like to share!

I was surprised in searching myself for an answer on this subject how difficult it was to find a clear solution or coded around it, making it far more complicated than it needs to be. Even the Oracle docs were hard to follow. In Intellij, you only see a couple overloaded methods instead of the full range of options unless keep typing commas, and so naive people like me think there are not any remaining overloaded methods. By the time you get to the Icon and button parameters you thin you are at the end... Not so here! These default parameters fall after the icon and appear as the last two parameters.

The first of the two defaults I have marked bold, 'null', is a very cool parameter allowing you to enter an array of options (String[]) of 'selection values'.


A list with with a default picked
    JOptionPane.showInputDialog(null, "What primary color is your favorite?", "Pick a color", JOptionPane.PLAIN_MESSAGE, null, ans, ans[0])
or...
No list (null) and default string
    JOptionPane.showInputDialog(null, "What primary color is your favorite?", "Pick a color", JOptionPane.PLAIN_MESSAGE, null, null, "Red")

Choice-or-Default-Only.JPG
[Thumbnail for Choice-or-Default-Only.JPG]
 
Marshal
Posts: 63781
209
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you been through the API documentation? There is only one overloaidng of showInputDialog() with a default response.
 
Scott Vallery
Ranch Hand
Posts: 37
1
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Being a Java newbee, I  missed it. I struggled with this for a few days before finding what I did and thought I would post so the next newbee wouldn't struggle as long. LOL! There are actually 2. The second does the same as the first except you also have the option of restricting the user to options in an array while providing a default. There is also more control, offering a Title and Icon if preferred.

1) showInputDialog​(Object message, Object initialSelectionValue)

2) showInputDialog​(Component parentComponent, Object message, String title, int messageType, Icon icon, Object[] selectionValues, Object initialSelectionValue)
 
Campbell Ritchie
Marshal
Posts: 63781
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, sorry I missed the second such method.
 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!