aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes JOptionPane.showInputDialog() and validating the Customer ID in a Book Room Dialog Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "JOptionPane.showInputDialog() and validating the Customer ID in a Book Room Dialog" Watch "JOptionPane.showInputDialog() and validating the Customer ID in a Book Room Dialog" New topic
Author

JOptionPane.showInputDialog() and validating the Customer ID in a Book Room Dialog

Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hello Everybody,


I've been looking at the Java Tutorial about how to make Dialogs
and I want to have a Dialog that once the CSR selects the record in my JTable in the main GUI window and it clicks on a "Book Room" button will pop up
prompting the CSR for the Customer ID to be typed in and then he clicks OK.

I do like the simplicity of potentially having everything in a single line such as:


in the actionPerformed() method of the ActionListener for my Book Hotel Room button.

The only aspect I am missing here is how to automatically validate to have an 8 digit number.

Perhaps I could do it in a loop once JOptionPane.showInputDialog() has returned a String but I was thinking perhaps if I could modify the JOptionPane
to have something like when you extend a JTextField
to create a class that could only accept Positive Integer numbers
or maybe use something else?


Thanks in advance,


Carlos.

SCSA, OCA, SCJP 5.0, SCJD, CCDH, CCAH http://www.linkedin.com/in/carlosamorillo
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5408
    
  13

Carlos,

Code fragments should be formatted (for example 80 chars per line), before posted in this forum. So the topic can be rendered without horizontal scrollbars. Please edit your post.

I created my own dialog, which could be opened in different modes (read, update, delete, book, unbook,...). Depending on the mode specific fields are disabled, specific validations are performed,... The advantage of this approach is that user will get always the same room dialog, which makes user very quick familiar with the application.

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hi Roel,

Did you use a similar approach using a Common Panel as in the Monkhouse's Book example
implementing the Observer pattern for the Book Room Dialog?

Thanks,

Carlos.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5408
    
  13

That dialog doesn't use observer pattern. It is just a dialog which opens in different modes and depending on the mode certain fields are disabled, specific validations are executed.
I do use the observer pattern and the common panel for my configuration settings.
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hi Roel,


Just curious, by any chance do you display the whole Hotel Room information
with only accepting input for the customer id and the rest disabled?
Something similar to a JTable with a single row of data with the corresponding
column headers in this Book Room Dialog?


Thanks,


Carlos.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5408
    
  13

Hi Carlos,

That's indeed how my room dialog looks like For every field a label and a input possibility (radio buttons, text field,..). Depending on the mode in which this dialog is shown certain fields will be disabled and others will be enabled.

Kind regards,
Roel
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hi Roel,

I see.
I've been thinking a bit about this ... I want to make it generic and possibly reusable for other
GUI operations and at the same time simple avoiding complex code (using the sample code
in this original posting I would need a loop, perhaps some parsing, perhaps a try-catch block)
and I'd like to take advantage perhaps of an extended JTextField that only takes positive numbers
without the need for extra validation code.

I was also looking and considering as an option at this JTable Tutorial example
Using an Editor to Validate User-Entered Text
and the code looks a bit cumbersome and complex to me at first glance.

Thanks,

Carlos.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5408
    
  13

Hi Carlos,

My room dialog counts approximately 450 lines (code and comments). Almost half of all these lines are needed to create all the controls on the panel (labels, text fields, radio buttons,...). Furthermore I have some other (important) methods:
  • initialize the panel (enable/disbale controls depending on the mode + give the input controls a value according to the room object)
  • validate the values entered by the user (depending on the mode)
  • create a new room object with the values entered by the user


  • As you can see the most complex code is in the methods to create the panel and that's just because creating a decent GUI interface with Swing needs a bit of work (thanks to the layout managers ), all the other methods are just so incredibly simple (a junior programmer will understand with ease and without any explanation of a senior). The biggest advantage is that this panel/dialog can be reused for every possible operation that can be performed on a room (and I can think of): delete, create, update, book, unbook,...

    I certainly would advice against creating an editable JTable. The code you need will be more complex and it will be a lot harder to maintain, extend and will be more error-prone.

    Just my 2 cents.
    Kind regards,
    Roel
    Carlos Morillo
    Ranch Hand

    Joined: Jun 06, 2009
    Posts: 221

    Hi Roel,


    Great!
    I already got this morning a prototype of my Book Room Dialog working.

    Need to polish it a bit and make it more generic and reusable.
    In its current form is Book Room operation specific.

    I might also need to change the Layout Manager perhaps to a GridBagLayoutManager, since
    My JTextField for the customer id (8 digits) is currently showing 12 despite I am constructing
    that JTextField with 8 columns but the GridLayoutManager is resizing it a bit with 12 columns
    instead of 8. I don't want a CSR to be able to type more than 8 characters.


    Other than this and finish all the documentation and choices.txt I am pretty close to the end
    and I have 20 days to submit my assignment!


    Thanks,


    Carlos.
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5408
        
      13

    Hi Carlos,

    I created the input field for the customer id as follows:
    and I applied a DocumentFilter to the field which restricts the user: only digits are allowed and no more than 8.

    Kind regards,
    Roel
    Andy Jung
    Ranch Hand

    Joined: Feb 07, 2010
    Posts: 150
    Hi Roel,

    I read this thread today for the first time. It feels good to see that I did it the same way as you .

    My approach also was to generate a generic modal dialog usable for all operations on room records, that is, show details, book, unbook, update and create new rooms. This dialog even is applicable for the searching issue, since my search criteria is subclassed from entity room.
    Enabling/disabling fields and field-validation is performed depending on the mode parameter, specifying the type of the dialog.

    Since my assignment demanded to implement all operations on server side, this way it was actually little effort to implement all operations on client side as well.

    Kind regards,
    Andy


    SCJP, SCJD
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5408
        
      13

    Andy Jung wrote:This dialog even is applicable for the searching issue, since my search criteria is subclassed from entity room.
    I don't think search criteria can be considered as a specialization of the entity room, so inheritance would not be applicable here. But that's just my opinion

    Andy Jung wrote:this way it was actually little effort to implement all operations on client side as well
    Using a test case would be even less effort than adding this functionality to the client side

    Kind regards,
    Roel
    Andy Jung
    Ranch Hand

    Joined: Feb 07, 2010
    Posts: 150
    Roel De Nijs wrote:
    I don't think search criteria can be considered as a specialization of the entity room, so inheritance would not be applicable here. But that's just my opinion


    Actually you're right, it just fitted for technical purposes.
    So I have to rethink the design here: Maybe find a superclass which can be used for both search criteria and room ? However, my opinion is,
    that reusing of code should be applied here.
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5408
        
      13

    In my opinion: you should not reuse code just for the sake of reusing it. Both my value object (with room details) as my search value object (with search criteria) are 2 seperate objects. When someone inspects the search value object, it is obvious which criteria are used to search on. In your situation you have a collection of obsolete (not used) criteria (all room properties except name and location).
    And what do you do if you have to provide a search which returns date with a date between a start and end date? Will you reuse the date from the room value object or not?
    Andy Jung
    Ranch Hand

    Joined: Feb 07, 2010
    Posts: 150
    ... yes, I can see your arguments.

    But since I have a generic record dialog responsible for handling all business operations like searching, booking, unbooking and so on, I need a common subclass or interface for my two value objects "search criteria" and room.
    My generic dialog is based on a specific type. Maybe I have to emphasize in my choices.txt .
    Carlos Morillo
    Ranch Hand

    Joined: Jun 06, 2009
    Posts: 221

    Hi Roel,


    Did you use DocumentFilter to restrict the JTextField maximum length as indicated in this
    tutorial?


    Thanks,


    Carlos.
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5408
        
      13

    Carlos Morillo wrote:Did you use DocumentFilter to restrict the JTextField maximum length as indicated in this tutorial?

    Yes, I did. More info can be found here

    Kind regards,
    Roel
    Carlos Morillo
    Ranch Hand

    Joined: Jun 06, 2009
    Posts: 221

    Hi Roel,


    Great!
    If it isn't too much curiosity, Did you use the DocumentFilter besides restricting the maximum length
    to also allow only digits?

    I see here you considered PlainDocument which is the approach used in the Monkhouse example for positive integers
    in a JTextField and I was also thinking the same the Java 6 API JTextField documentation has a PlainDocument
    example.

    Thanks,


    Carlos.
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5408
        
      13

    Hi Carlos,

    From the thread I referred to in my previous post:
    For the assignment I created 2 filters: one allowing a maximum number of chars, the other one allowing a maximum number of digits. The last one I used for the customer id.

    Using a DocumentFilter was suggested when I asked about which alternative to use, this thread can be found here.

    Kind regards,
    Roel
    Carlos Morillo
    Ranch Hand

    Joined: Jun 06, 2009
    Posts: 221

    Hi Roel,


    Awesome!
    Got it working and I see and it makes 100% sense where you are coming from.

    Once again, Thanks a lot!


    Best Regards,


    Carlos.
     
    Don't get me started about those stupid light bulbs.
     
    subject: JOptionPane.showInputDialog() and validating the Customer ID in a Book Room Dialog