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

Posted on 2006-04-28
Last Modified: 2008-01-09
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:

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
Question by:rashmind2003
    LVL 30

    Expert Comment

    >> 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.
    LVL 30

    Expert Comment

    Meaning if you have:

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

    Replace it with:

    Object o = comboBox.getSelectedItem () ;
    String s = ( o != null ) ? o.toString () : "" ;

    Author Comment

    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.
    LVL 30

    Expert Comment

    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?

    Author Comment

    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
    LVL 30

    Accepted Solution

    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?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now