|
Hi folks,
I am new to Java and fairly new to programming in general. I have spent a couple of years in MS Access/VB.
In my quest to broaden my skill set I have started to play with Java. Honestly it seems to require a lot of code and classes to do the littlest thing.
My issue is this I have a mysql db with a customer table. I want by combobox to store the id and customer name but only display the customer name in the combobox.
When the user selects a customer I want the id to query the jtable and display the customer info.
I have this working except the part that stores id and name in the combobox.
If someone could provide an example it would be greatly appreciated.
Thanks!
|
|
|
|
|
Here is a nice sample[^] in the Java Tutorials website.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
I understand the example with animals and the idea of an index (i looked at this from one of your other posts) what I am having hard time grasping is that my index wont be 0-4 it could be a range of ids ie (100,1,201,2304, ect)
So how do I know when the they select pig that it corresponds to id 2304 in my customers list.
Thanks so much for all your help.
|
|
|
|
|
Chrisloys wrote: So how do I know when the they select pig that it corresponds to id 2304 in my customers list. You omitted to mention this in your original post. Are you saying that your users select a customer by using some random number out of a list of hundreds? I would suggest a rethink of your design here. Of course, you could always provide some sort of cross reference between names and numbers; without knowing more about what you are actually trying to do it is difficult to suggest much more.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
No the numbers are not random per say they are developed by mysql as they are autonumber/primarykey. So if we had two customers with same name they would have two differnt ids. I need to reference this id from the combobox to correctly populate the table.
So my table will look like this
id customername address city
1 customer1 123 this st atown
3 customer2 5 that st btown
4 customer 3 989 overhere st ctown
2 customer 4 9999 overthere st dtown
6 customer 5 234 thisstreet st e town
So when the user selects customer2 from the combobox it will in turn say customer2's id is 3.
3 will then be used to query the customer information and display this in the table.
I hope iam explaining this correctly... this was very common in ms access but all i had todo was hide the column with the id so it only displayed the customer name. When a new customer was selected it would take the hidden id column value to populate a table. It was extremely easy.
|
|
|
|
|
So how does a user find the entry for Smith J. if there is more than one of that name?
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Well we would then look at the corresponding customer information to make sure we had the correct info. Sorry this really isnt a real life situation as we shouldnt have two customers with the same name.
|
|
|
|
|
I think your question has more to do with your design and how you manage your customer lists, than with how to use a combo box.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
So how would you design? Very odd this is like DB101. If you had a student table with five students named john smith you identify the difference by there id.
So are you saying that if I want to query a table based on the selection I have to have customer name referenced in other tables??? No that cant be right I look at referential db's most of the day they are linked on ids not names??
Someone out there must have run into this along the way.
|
|
|
|
|
Chrisloys wrote: So how would you design? Well, without spending a lot of time thinking about it I am not sure.
Chrisloys wrote: Someone out there must have run into this along the way. Quite possibly, but I get the feeling this has very little to do with Java.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
the java part is the jcombobox referencing the id of customer name selected.
If anyone could shed some light it would be most appreciated.
Thanks for all your responses.
|
|
|
|
|
Well looking at the documentation[^] it is clear that the items in the combobox can be objects, so you just need a class of object that contains the name and the id of the customer. The object's ToString() method should return the name which (I guess) will appear in the combo box.
One of these days I'm going to think of a really clever signature.
|
|
|
|