Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JOptionPane with only 'OK' button and Selection List  RSS feed

 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to create a JOptionPane.showInputDialog with a selection list and just an 'OK' button. I can find examples for just an 'OK' button and a selection list but not both.

How do I remove the 'Cancel' button? TIA.
 
Campbell Ritchie
Sheriff
Posts: 55350
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a version of the constants in JOptionPane which allows an OK button on its own? Have a look; I couldn't find it. I presume the method you want is showInputDialog()? I didn't see an overloading which seems to match what you are asking for.

Sorry.
 
Fred Kleinschmidt
Bartender
Posts: 555
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need a selection dialog that has no "Cancel" button? It can be very frustrating for the user if there is no "Cancel" (suppose the user popped up the question by mistake, or needs more information before she can make the correct choice)?

Anyway, if you do make a dialog with no "Cancel" button, what do you expect to happen if the user closes the window using the border "X" button?
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this particular case the user MUST make a selection (even if it is to accept the default) to proceed. The 'X' will be disabled. As for unintentionally getting into this pane (probably not really possible in the context of the process), the cancel opportunity occurred earlier in a different dialog. Also this is a non-destructive dialog until the user returns to the parent and saves.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
showMessagedialog(...) with a JList as the message argument.
 
Campbell Ritchie
Sheriff
Posts: 55350
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice suggestion ... and welcome back Darryl
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I looked at JList first but I'm not sure it will accomplish what I want. I will have to look at it again and think about it. It also seems substantially more complicated to implement to get what I need. In any case I am gathering that there is no way to remove the 'Cancel' button or the unwanted TextField when using showInputDialog.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Putnam wrote:It also seems substantially more complicated to implement to get what I need.

Its not.

Dennis Putnam wrote:In any case I am gathering that there is no way to remove the 'Cancel' button or the unwanted TextField when using showInputDialog.

There is.


Screen-Shot-2016-07-20-at-8.02.03-PM.png
[Thumbnail for Screen-Shot-2016-07-20-at-8.02.03-PM.png]
Screen-Shot-2016-07-20-at-8.03.49-PM.png
[Thumbnail for Screen-Shot-2016-07-20-at-8.03.49-PM.png]
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You embedded a JPanel rather than using the suggested JList!!! Why didn't I think of that??? Thanks.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the JOptionPane#showXXX methods accept an Object as the message. This gives you the flexibility to literally display anything as the dialog content.
Recommended reading: https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I knew that but it just didn't click. Thanks again.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Addition to Maneesh's hint: you can also pass an array of JRadioButton as the message argument.  But don't forget to iterate over the array and add them to a ButtonGroup first.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!