• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Any way to prevent hardcoding the JTable column index?

 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

Currently serving army now, so my SCJD assignment progress is delayed. Anyway, I have a little issue here.

I got the Bodgitt & Scarper assignment. I'm implementing RMI, and one of the remote methods will allow me to retrieve the database schema (the column names). This will allow me to prevent hardcoding of column names into the client GUI model.

I have a "Book" button at the bottom of the GUI. I will like to implement a tiny effect, where this button will self-disable when the user selects a record which has already been booked. This will prevent them from attempting to book an unavailable record. The problem now is, in my listener code, I have to check the model and see if a customer ID exists for the selected row. If there is a customer ID, that means the record has been booked, and vice versa. It seems that the only way I can check is to hardcode the index of the customer ID column (in my case, 5).

Is this a good way to do things? I mean, I get the schema from the server, yet in my GUI, I assumed that the customer ID is index 5. This seemed contradictory, but I can't think of other ways to get this right. Any suggestions? Or am I in the wrong direction?

PS: Ok I just searched the forum for similar queries. It seemed that there is no way to prevent this. No matter what you do, you have only 2 choices:

i) Hardcode the column name, and search for the index using that name.
ii) Hardcode the index.

So do I conclude that there's no way to prevent hardcodings?
[ November 12, 2005: Message edited by: Liang Anmian ]
 
Gary Hellman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i) Hardcode the column name, and search for the index using that name.
ii) Hardcode the index.


Could you use "i", but place the column name in the properties file?
Hardcode a default name but allow it to be read in from the properties if it is there...?

(probably ok to hardcode it - name or index)
 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That means something like maybe: "Customer ID" for "owner" column. So I store these constants in a properties file, and use the "Customer ID" name to get the database column name.

If I do it this way, doesn't this mean that the database admin will have to update the properties file everytime he decides to make a change to the database (e.g. if he adds in a new column etc)? I guess if I mention this in the choices.txt it should be ok?

PS: Must these be stored in suncertify.properties? I can actually create another properties file and store it inside the JAR file itself (instead of current working directory), right? I guess this does not violate anything.
[ November 12, 2005: Message edited by: Liang Anmian ]
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Liang

I also got my table names from the DB, I believe all I did to that was uppercase it.
Anyway on the GUI side, for that matter on the server as well, I had a Room object represent a row in the database.
Room had constants for the position each property should be in, so when I wanted to get or set a property it made it easier, consistent and ensured minimal change required when the schema changes.

Urm, to solve the problem you mentioned I'd have my table model return my Room object and I'd call room.getOwner() or someObject.getCustomerId in your case.
 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean that your table model is a custom one? As in, you created a custom subclass of AbstractTableModel?
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Liang Anmian:
Do you mean that your table model is a custom one? As in, you created a custom subclass of AbstractTableModel?


Yes it's a custom table model, with methods like public Room getRoom(int rowNo) and also public void addRoom(int recNo, Room room) {
 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your method sounds great! Thank you for sharing! Will get on to work now!
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Liang Anmian:
Your method sounds great! Thank you for sharing! Will get on to work now!


You're welcome
 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops. Apparently I didn't read your reply carefully. In other words, you still have to hardcode the index (according to what you mentioned). It's just that you consolidate all possible future changes to one class, without worrying that the GUI will break down?
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Liang Anmian:
Oops. Apparently I didn't read your reply carefully. In other words, you still have to hardcode the index (according to what you mentioned). It's just that you consolidate all possible future changes to one class, without worrying that the GUI will break down?


Yep, the Room in my project like I said represents a record, in fact it extends Record which has Columns.
You don't have to go to that lengths, I did however see an implementation like that in an SCJD guide a few years back.
Anyway since we're implementing all data related operations something needs to know which index the data is at, in my model there's no candidate more suitable to know about Room's column positions than Room so it's a constant in that class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic