This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
The name, location and customer text fields are provided on the GUI, and the length of these are limited as per the database schema. In the data layer I have a class DatabaseSchema, which captures this :-
At the GUI layer, I need to constrain, the length of name, location and customer in line with the above. To facilitate this, I can either :-
1) reference the above constants directly from the GUI.
I feel it is not good OO principle to expose such detail at the GUI layer.
2) define new constants in the GUI,
By defining new constants at the GUI layers is effectectively duplication of the same information as held in the data layer.
Any opinions on which approach is good or if there is an alternative.
I use combo boxes that get populated from the database for the name and location, so there's no chance of a) typos b) overwriting the field length. For the owner field I use a MaskFormatter to force input to 8 digits. There is an example in Andrew's book on how to do this. Declaring new constants is an OK option for the GUI if you want to use text inputs.
Just curious to know if you already decided when to repopulate your combo boxes? I was planning to use that approach too, but because i was doubting when to refresh the data (and keeping it easy and simple) just used the jtextfield, using a DocumentFilter to allow a maximum number of characters (or digits).
Partner, here's what I did: the JTextFields where the user has to provided the required info are limited to the maximum length of each field. For instance, the customer ID is limited to 8 characters. In the Data class, before updating or creating a record, each String in the String arrays is filtered. For instance, when creating a record, for each String in the String array, I verify if its length is greater than the maximum length; if so, I just get the first n characters of it.
Ah that JTextField vs JComboBox debate again. I used JComboBox for my name and location in my search dialog. Roel asked when should these boxes repopulate? Well in my app, everytime the search dialog opens. Unless the RMI server shutdowns that would make the whole app to exit too.
In my GUI, I provided create dialog too. This dialog I used JTextField. To keep track of how many characters/digits are inputted, I used a DocumentListener on the text field. Say the name can have up to 64 chars, when user enter 65 chars, it pops up error. You get the idea.
The original question of Pete was/is: if you have a static database schema (hard coded all values) what do you do in the GUI to limit the characters a user can enter in a JTextField? Do you reuse the DatabaseSchema-values in your GUI too? do you create a seperate interface (in the GUI package) keeping all these constants (a 2nd time)? ... or do you follow an other approach (like Roberto: substring in the Data class any value that is longer than allowed by the database schema)?
The combobox vs jtextfield was an off-topic remark of me (we had a big discussion some weeks/months ago about when to refresh these combos) and i was curious if Anne had already made a decision (because she was still doubting about it then, like me). And your solution is an excellent one if you have of course a seperate search dialog, otherwise it would be hard doing it like that At the end I decided just to use simple JTextFields (because scjd assignment is hard enough ), but still interested how other people solved the refreshing issue
Joined: Oct 21, 2008
Firstly thank you all for your helpful responses.
Anne, I am pretty much commited using the JTextField approach, using the JComboBox at this late stage for me is probably not an option. Just might go with your suggestion of defining a constant in the GUI.
Roberto, thats an interesting and obviously a successful approach, however, I perform a check on each of the array elements and throw IllegalArgumentException if any don't agree with the schema.
K, the 64 LOCATION_LENGTH limit, is that a constant you have defined in your GUI or are you reading it from the data layer? If it is the former, is it effectively duplicating the constant already defined in the data layer ?
Roel, Thank you for the clarification of my query.
Pete Palmer wrote:K, the 64 LOCATION_LENGTH limit, is that a constant you have defined in your GUI or are you reading it from the data layer? If it is the former, is it effectively duplicating the constant already defined in the data layer ?
Hi Pete, I do have a constant class but did not have the constants for the database schema - rather constants like default server port, local/network option, magic cookie value. The schema field length for the gui is hard-coded. I didn't read it off from my Data class which I could have done but that would make my interface clumsy.
Joined: Oct 21, 2008
I think I will go with defining a constant in the GUI.