File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes jCombobox itemdata trouble Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "jCombobox itemdata trouble" Watch "jCombobox itemdata trouble" New topic
Author

jCombobox itemdata trouble

Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
Dear friends,

I have a jComboBox in which i have to display certain Values and when the user selects those values, I have to retrieve the corresponding ids of those values stored in a separate field.

I searched the net and found that i would have to put some cellrenderer and probably extend something called Mutablecombobox model or something like that. This all appears too tedious to me.

I am new to swings and java.

So what i did is while i have stored the values using addItem(String) in the ComboBox, i stored the corresponding ids in a vector and retrieve it as they are stored at the same index level in the vector.

I think that this is an unprofessional approach. But what you guys say about the pros and cons of such an approach. Moreover from where i can learn that listcellrenderer and all (where it is explained in simple language because the online sunjava tutorial on this subject appears a bit complicated and messy to me).


Anand Shrivastava
SCJA
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Anand Shrivastava:
I have a jComboBox in which i have to display certain Values and when the user selects those values, I have to retrieve the corresponding ids of those values stored in a separate field.

I searched the net and found that i would have to put some cellrenderer and probably extend something called Mutablecombobox model or something like that.


I don't see why you would have to concern yourself with the ComboBoxModel.

Implementing a ListCellRenderer probably wouldn't be as hard as you think, but you might not even need to do that. The default renderer will call toString() on whatever items (they needn't be Strings) you put in the combo box. So if you have control over toString() then you can do something like this:


[ October 12, 2007: Message edited by: Brian Cole ]

bitguru blog
Anand Shrivastava
Ranch Hand

Joined: Jul 22, 2007
Posts: 125
Thanks,

Your reply served a very useful purpose.

This means that the toString() method is used to display the cells in the combobox, but all the methods should be encapsulated in a static class.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Originally posted by Anand Shrivastava:
This means that the toString() method is used to display the cells in the combobox

I believe that this is correct.

but all the methods should be encapsulated in a static class.

And this I believe is incorrect. I think he used a static class as a convenience, but the data in a combobox doesn't have to be objects of a static inner class, not by any means. Any array of a class's objects can be in there. Sometimes folks just use an array of String:

This is probably something you are just going to have to play with. Good luck!
[ October 13, 2007: Message edited by: pete stein ]
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Anand Shrivastava:
This means that the toString() method is used to display the cells in the combobox,


Yes, presuming you haven't done anything to replace the default list cell renderer with something else.

but all the methods should be encapsulated in a static class.


No. I presume you already have some objects that have something like getId() and getStringValue() methods. If so, just use those objects. There's no need to encapsulate anything unless the toString() method is beyond your control.

I used a static nested class in my code example just to keep it simple and allow it to compile from a single file. If you had given any hints as to what your objects were like I would have tried to approximate your situation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jCombobox itemdata trouble