aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes showInputDialog problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "showInputDialog problem" Watch "showInputDialog problem" New topic
Author

showInputDialog problem

Dilip kumar
Ranch Hand

Joined: Oct 16, 2000
Posts: 360
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

Joined: Apr 30, 2001
Posts: 28
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


Matthew Robinson<BR>matt@mattrobinson.com<BR>co-author of "<a href=http://www.amazon.com/exec/obidos/ASIN/189311578X/ref=ase_electricporkchop/107-7882751-0234939 target=newwindow rel="nofollow">Swing</a>" (1999, Manning Publications). Freely available at <A HREF="http://www.spindoczine.com/sbe" TARGET=_blank rel="nofollow">http://www.spindoczine.com/sbe</A>
Dilip kumar
Ranch Hand

Joined: Oct 16, 2000
Posts: 360
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

Joined: Apr 30, 2001
Posts: 28
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

Joined: Mar 23, 2000
Posts: 51
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: showInputDialog problem