• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with closing JOptionPane

 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I have a JOptionPane using showInputDialog() method. If the user doesn't key any text and hits OK, I want the dialog to stay up until they key something, or cancel it. My problem is if I hit OK without keying any text, the dialog stays up. But if I hit OK a second time, It goes away. I must be missing something. Could someone please help??
 
John Palmieri
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jennifer,
Try this
String chgDesc;
String inputValue;
for ( ; ; )
{
inputValue = JOptionPane.showInputDialog(null,"Key a description of the change.","Change Order",JOptionPane.INFORMATION_MESSAGE);
if (inputValue == null)
{
return;
}
else
{
if(inputValue.trim().length() != 0)
{
chgDesc = inputValue;
break;
}
}
} // for ( ; ; )
System.out.println("You typed: " + inputValue);
[ October 23, 2002: Message edited by: John Palmieri ]
[ October 23, 2002: Message edited by: John Palmieri ]
 
Bill Liteplo
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code will keep re-showing the dialog until non-empty input is entered. Note that a new dialog is shown each time. (A side effect is that if the dialog box is moved, it will show up in the center of the screen when you hit enter.)
What was happening in your code was that you were showing the dialog twice (each time you call JOptionPane.showInputDialog), but no more. You also weren't resetting the value fo inputValue the second time you called it.
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help guys! I really appreciate it.
 
Paper has not yet been outlawed – so let your Java Swing application print with wild abandon: JPDF
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!