| Author |
How to tell when a value has changed...
|
Jennifer Sohl
Ranch Hand
Joined: Feb 28, 2001
Posts: 455
|
|
Hi. I have a program that is reading data from a database. I am retrieving the data and putting in into a JComboBox. However I want to seperate the data in the JComboBox with headings, like "---Shop Order Types---" and then list the shop order types, and so on. Basically I want to be able to add these headings after the last record of that type, and before the first record of the next type. Not sure how to do this? Hopefully this makes some sort of sense. Thanks in advance!
|
 |
Dave Vick
Ranch Hand
Joined: May 10, 2001
Posts: 3244
|
|
Jennifer I'm not sure which value it is you're trying to see change, the values being added to the list, or the selected item in the list itself after it has been created. For the second, just write the event handler in such a way that it saves the selected item and can compare the old one with a newly selected one to see if it has changed. To actually make the list, I assume there is a field in the recordset that your getting back that indicates the type of the record. You'll have to create a sub loop within the loop that goes through the record set and check each item as you get to it to if the type has changed, if so then just put in the new header and carry on with the loop. Something like this: Did that help at all? Or, did I misunderstand the question?
|
Dave
|
 |
Jennifer Sohl
Ranch Hand
Joined: Feb 28, 2001
Posts: 455
|
|
This is what I am currently doing: A select statement that brings back the description of the order type, and the category that the order type is (i.e. S=shop order type, C=correction orders, etc.). Now I have to get into the result set to determine what the category is. Now I want to save this value to compare it with the next row I read. How can I save the value without overwriting it everytime (because I'm in the while loop)? The code you showed me makes sense, but how can I set my comparison variable outside of the result set loop? (Sorry if I'm confusing you. I'm starting to confuse myself. If you can't tell, I'm not that great at putting things in writing! )
|
 |
Dave Vick
Ranch Hand
Joined: May 10, 2001
Posts: 3244
|
|
You get a char that tells you what the type of the record is so when you do your select do an ORDER BY or a GROUP BY to keep all of the records of the same type together. Then you don't have to remember all of them, just which one you're currently working with, and when that one changes, insert the new type heading into the list and then all of the records below it until you get to the next one. Something like this: -- get all of the records you want - one of the sorting criteria must be the type. -- if you've got results set a currentType variable to some default value, this holds the type of the group you are currently working with. -- start your while loop -- check if the group type of the current record is the same as the one in the currentType variable -- if not, add a new group type header and change the currentType variable to the new one in the current record. -- if they are the same then you can just add the record to the list. This way the only things you need to be concerned with is the group type of the current record you're working with and the group type of the last record you dealt with. As soon as they are different you have a new type so you just add a new header to the list. Did that make sense? In the little piece of code I posted I left out the part about changing the value of the test variable as soon as they are different. You only set it outside of the while loop so that the first record will cause it to fail the test and add the correct group type header and set the variable to the group type of the first record. Then, as long as the group is the same, the currentType stays the same and you're just adding records to the list. As soon as it is done with all of the records of that type it catches it in the if statement, adds a new header, and processes all of the records of that type. Hope that helps, and you're putting it into words just fine - I've got a problem with explaining things clearly  [ September 03, 2002: Message edited by: Dave Vick ]
|
 |
 |
|
|
subject: How to tell when a value has changed...
|
|
|