Editable jcombobox with typeahead/autocompletion feature as a cell editor for jtable

How to implement an editable Jcombobox ** with typeahead or autocompletion feature** as a cell editor for a Jtable column?

Im trying to follow this example:
http://www.orbital-computer.de/JComboBox/

Problem: My jcombobox takes a list of Objects and the selected item is being returned as an object which I later cast into
myfirm object. This works fine. But if I make Jcombobox editable, it returns the seleted object as a string rather than an
object. So when I try to cast this string into myfirm object I get a class cast exception.

How to make an editable jcombobox return an object instead of a string?
Any guidelines for implementing an editable jcombobox(with autocompletion feature) as a cell editor for a jtable?

Thanks in advance
rashmind2003Asked:
Who is Participating?
 
Mayank SConnect With a Mentor Associate Director - Product EngineeringCommented:
Well perhaps that is because when you make it editable, it allows you to type and it cannot allow you to type a MyFirm :) it can only allow you to type a String so the return-type becomes String. Did you check what String it is returning (what is the value of the String)? What does the MyFirm class look like?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> So when I try to cast this string into myfirm object I get a class cast exception

How about using toString () instead of casting? It will save you from the ClassCastException. Just make sure the object is not null before calling toString ().

- Mayank.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Meaning if you have:

String s = ( String ) comboBox.getSelectedItem () ;

Replace it with:

Object o = comboBox.getSelectedItem () ;
String s = ( o != null ) ? o.toString () : "" ;
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
rashmind2003Author Commented:
Thanks for your respone.

I want to do exactly the opposite. I do not want a string. i want an object.
Im doing Firm f = (firm) combobox.getseleteditem( )
When i seteditable(true) for combbox it returns a string instead of an object.
So I want combobox to return an object not a string.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
A String is an object, in effect. You can always do:

Object o = <whatever string> ;

However, if it is a String, you cannot convert it to MyFirm directly. What kind of String does it return? Meaning can you try to print it out and see what its contents are and if it is possible to create a MyFirm object from it?
0
 
rashmind2003Author Commented:
Yes I can not conver a string into my object directly. So I want combobox to return an object instead of a string. If seteditable(false) my combobox does return an object and things are fine. But setting editable true makes it return a string
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.