• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

showInputDialog problem

 
Dilip kumar
Ranch Hand
Posts: 360
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The below code is not working properly. I need to display message "Please enter valid number." when user clicks on "OK" button without entering anything in the input box.
seats = JOptionPane.showInputDialog(frame, "Please enter number of tickets :");
// Do nothing if user clicks on Cancel button.
if ( seats == null ) return;
if(Integer.valueOf(seats).intValue()==0 | | seats.trim().length()==0) {
  JOptionPane.showMessageDialog(null, "Please enter valid number.", "Invaid Entry", JOptionPane.INFORMATION_MESSAGE );
  return;
}
Thanks
 
Matt Robinson
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are expecting that the input dialog will stay up after you have retrieved the value for "seats" but this is not the case. Once you click the OK button, the dialog disappears and your value for seats is then set.... You may need to review how JOptionPane works..
In any case you would be better off writing your own subclass of JDialog so that you can verfy input before it disappears.. or you can look at "seats" and if its not valid, show the input dialog again

------------------
Matthew Robinson co-author of "Swing" (1999, Manning Publications). Freely available at http://www.spindoczine.com/sbe
 
Dilip kumar
Ranch Hand
Posts: 360
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt,
Thank you for response.
What will be the value of seats when users clicks OK button without entering anything ? Is it null or "".
Thanks again
Dilip
 
Matt Robinson
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can check with a System.out.println statement..
not sure but it will be on of those..
------------------
Matthew Robinson co-author of "Swing" (1999, Manning Publications). Freely available at http://www.spindoczine.com/sbe
 
Doug Melzer
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might also want to take a look at this:
http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html
Stopping Automatic Dialog Closing
By default, when the user clicks a JOptionPane-created button, the dialog closes. But what if you want to check the user's answer before closing the dialog? In this case, you must implement your own property change listener so that when the user clicks a button, the dialog doesn't automatically close.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic