wood burning stoves*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Obtaining recNo from JTable - wrapping recNo in Room Object(UrlyBird) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Obtaining recNo from JTable - wrapping recNo in Room Object(UrlyBird)" Watch "Obtaining recNo from JTable - wrapping recNo in Room Object(UrlyBird)" New topic
Author

Obtaining recNo from JTable - wrapping recNo in Room Object(UrlyBird)

bernard adaba
Greenhorn

Joined: May 15, 2009
Posts: 28
Hi All,
I am implementing my BookRoom() in the GUI for the UrlyBird assignment.
I was wondering how to get the recNo from the JTable when i read from Roel that he wrapped the
recNos in the Room Object.
I see that as a convenient and simple approach.
My question is, how do you wrap the recNo in the Room Object ?
I was thinking that one would have to create extra getter and setter methods in the Room class
for the recNo and set the recNo when obtaining the records from the database.
I was wondering if i am correct.
Would be glad if any clarification on obtaining this recNo from the JTable could be given

thanks
Bernard


SCJP 5, SCJD
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hi Bernard,


I'd strongly recommend you check this thread.

I did create a set and get methods for the record number field in my Room class.


Best Regards,


Carlos.


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

Joined: Jul 19, 2004
Posts: 5233
    
  12

Carlos Morillo wrote:I'd strongly recommend you check this thread.
Thanks for linking to that topic each time this question is asked, Carlos. Saves me a lot of time

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
bernard adaba
Greenhorn

Joined: May 15, 2009
Posts: 28
Hi Carlos and Roel,
thanks very much for the quick response.
I have already actually read that post and that is where i read the marvelous technique by our big man Roel.
Ok, given that you are always reading directly from the database and not reading from a cache,
how are you going to wrap the recNo in the Room Object.
I set the getter and setter methods for a field I created in the Room class,
but any time i have a record and try to set a recNo, i don't get that exact number i set but rather
the default value i declared for the recNo field in the Room class.

My assignment also has an instruction which states
... the new system must reimplement the database
code from scratch without altering the data file format
.

Though i am scared of altering the format by writing into the file by creating a field length for the recNo field in
the database file, that seem not to work either when i tried it.
I suppose the total length of each record is formatted in the database file.

can anyone please help on how to wrap this recNo. i am stuck!

warmest regards
Bernard
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hi Bernard,

In your first post, you are asking if you can use setters and getters for a Room object so it will have the record number,
this in order to display it in the JTable in the GUI.

The Room object and how you display it in the GUI has to be totally decoupled from the Data class.
The only purpose of the Room object containing a record number field, it is so later on you can use the API of the Data class, so you
can create, read, update, delete, lock, unlock, find, etc.

Now, honestly I don't understand why you are bringing up issues in your Data class and cache or no cache and the format
of your database file. You need to provide a clear context and articulate better the problem.

In my assignment, I have that the format of the file must be preserved because it is also used by a legacy application:

The company's IT department has a data file that contains the essential information for the company, but because the data must continue to be manipulated for reports using another custom-written application, the new system must reimplement the database code from scratch without altering the data file format.


I don't see a problem with that.

You use the Room objects in your Business Service and GUI once somehow you bring the String[] data records from the Data class
using the Data class API.
Once you create a Room object with the data from a String[] data record you set the the record number.

Personally, I took the approach of getting my Data class with the locking mechanism to work first and I also made sure
I am able to persist modified and deleted records, before considering working in the Business Service, RMI Server, GUI, etc.


Hope this helps,


Carlos.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5233
    
  12

Hi Bernard,

I'll give a very brief description about my approach.

My Data class just works with the String[], because the contract (interface you have to implement uses String[]). The String[] contains the data for a record, so the record number is not a part of the String[], it is just location, name, smoking, date, customer id,... (so no record number).

My business service (which contains the methods callable by the client: search and book) transfers the String[] into Room-objects and the other way around. So here is the place where I add the record number to the Room-object. And because I use transfer object design pattern I don't have getters and setters at all. All members of my room transfer object are just public.

If you use a record cache or you just read every time the data file has nothing to do with your original question. The implementation of the Data class is (or at least should be) completely independent of wrapping recNo in a room object. So if you have questions about using a record cache or not, just use the search function of this forum and if you still have questions, start a new thread and post them there (think about SearchFirst and UseOneThreadPerQuestion). I agree with Carlos on this one.

Kind regards,
Roel
bernard adaba
Greenhorn

Joined: May 15, 2009
Posts: 28
Thanks Carlos and Roel for your clarifications.
I actually went about with Carlos' approach since i am not using transfer object design pattern as Roel did.
I used getters and setters and also have a separate class called RoomConverter which converts a Room object to String[]
and vice versa.
It is during these conversions in the business service that i wrap in the recNos.
My model now has 8 columns with the last column being the recNo values which i use to retrieve the exact record
from the database when i client selects that row from the GUI.
But i hide this last column from the user's view by removing the last column of my JTable. In this sense the user does not see the recNo field but it is actually existing the model which i can always obtain as when needed.
I am now able to select and book an exact record from the JTable but i still have some tests to do.
Thanks Carlos, thanks Roel for the clarifications.
And hey Roel, i have checked the links. I will definitely do more searches and comply.

Warmest regards
Bernard.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5233
    
  12

bernard adaba wrote:But i hide this last column from the user's view by removing the last column of my JTable. In this sense the user does not see the recNo field but it is actually existing the model which i can always obtain as when needed.
I would go for just 7 columns, instead of 8 and hiding/removing 1

Kind regards,
Roel
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hi Bernard,


I also do not display the Record Number in my JTable and I do not use it as a column
in my class extending AbstractTableModel.


Best,


Carlos.
Mxolisi Veco
Ranch Hand

Joined: Jan 14, 2010
Posts: 59
Good day all.

In my table model, I have two ArrayLists, tableRows and recordNumbers. tableRows stores String[] that represent the hotel room and recordNumbers stores the record number. Everytime I add a String[] row to tableRows, I add the record number to recordNumbers.

So to get the record number I just do this recordNumbers.get(intSelectedRow).
Thats it.
bernard adaba
Greenhorn

Joined: May 15, 2009
Posts: 28
Thank you all for your great help and ideas.
Your approaches seem more than desirable.
But in case i go ahead with my approach, would i be penalised?
I know it might not be a better approach.


Warmest regards
Bernard
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5233
    
  12

Hi Bernard,

Of course you can follow your own approach. And you certainly won't get penalized if it is not breaking a must-requirement and it contains no bugs.

Kind regards,
Roel
bernard adaba
Greenhorn

Joined: May 15, 2009
Posts: 28
Thanks Roel,
I need to consider the best option
and document appropriately.
But at the same i want to make it simple and easy and on top of it all
pass well


warmest regards
Bernard
Georgiana Lungu
Ranch Hand

Joined: May 17, 2010
Posts: 34
Hello,

I have the same problem, except that I am charging the only list I have in my TableModel on first position with recNo and I am taking values for rows starting from second position.
Ofcourse I could just pass the array taken from the List to the controller, instead of passing sellected row array, to ask for an update and it could just get the recNo from it, ofcourse, still based on selected row index(index in List = index in Table).
But I was thinking if this is the best practice I could apply and if it is right to call the TableModel's

directly from the MainWindow class.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5233
    
  12

Hello Georgiana,

Not completely sure what you want to know, so trying to give an appropriate answer

I added an extra method to my custom TableModel class which returns a Room-object based on the index of the selected row in the JTable. My MainWindow class calls methods from my custom TableModel class.

Kind regards,
Roel
Georgiana Lungu
Ranch Hand

Joined: May 17, 2010
Posts: 34
The thing is that in the end I haven't used any business object at all, I am just using String[] from the database to the gui and the other way around.
I keep the record number from the database on first position of the array thet becomes a row in the JTable. So for the rest of the values I keep refering at thet array's positions plus 1, as follows:



and in the gui, my methods look like this, as they either add the record number to an array that becomes a row, either remove it from an array that will become a criteria array for search in database:

[edit] do not post the complete code of a method

Since the only reason I could have to use a business object, given my application that retrieves String[] from database anyhow accordingly to the specifications, was to set/get the record number, i just thought this way i keep it simple. But I am not sure now and was wondering if it might not be better to add a business object, but then again, i would just pack a String[] into another object and then unpack it, just for the sake of keeping the record number in one variable instead of a position of the array. I know that in general, this could have more benefits but for my application, Is it worth?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5233
    
  12

I think it is certainly worth using a business object. Reasons why can be found here and here.

I also think your business object should not just be a wrapper around a String[], but even then you can enjoy all benefits described in the 2 threads.
 
 
subject: Obtaining recNo from JTable - wrapping recNo in Room Object(UrlyBird)