• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding generic JComboBox item type  RSS feed

 
Robbie Parks
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! I'm new to this forum, and also fairly new to Java, so maybe I'm missing something easy.

I extended JComboBox<E> to enable the user to delete items from it.
When an item is deleted, I want to preserve any text that was entered.
- If the user had entered text, and sees that an option needs to be deleted, they may delete it and return to their text editing, since they never submitted nor rejected that text.
- When an item is clicked to be deleted, it shouldn't then show up as the selected item.

All this works.

HOWEVER, because JComboBox<E> can hold objects other than Strings, and since you cannot enter any object other than a String in the JTextField component of a JComboBox<E>, saving the entered text and trying to restore it causes a problem with non-String objects. Really, I want my extended JComboBox<E> to be uneditable if it contains non-String items. Ie. it ought to be editable when defined as JComboBox<String> but not JComboBox<Person>.

Also, I want the constructors to match those of JComboBox<E>, so that they are interchangeable.

Since the user of the class can declare it however he wishes:

****** How can I detect during the class constructors what class it was defined to be? ******
That is, what type is E in JComboBox<E>?

The constructors for JCBExtended<E> are:



It is possible for the array case to call but I don't know what to do for the other constructors.

Note that like JComboBox<E>, JCBExtended<E> can be created with empty arrays and empty Vectors, so it is important to avoid "index out of bounds" and null pointer errors.

Thank you for all of your thoughts!

Robbie
 
K. Tsang
Bartender
Posts: 3633
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using getClass() method is one way.

Yet so what when you know the E is a String? Will there be variables like editable or readonly in your JCBExtended class?

I believe Swing JComboBox is readonly by default.
 
Robbie Parks
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, using getClass on the JBCExtended instance doesn't tell when generic parameter was used.

I do need it to hold items other that String, and it needs to have the option of being editable when Strings are used, but not otherwise.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robbie Parks wrote:HOWEVER, because JComboBox<E> can hold objects other than Strings, and since you cannot enter any object other than a String in the JTextField component of a JComboBox<E>, saving the entered text and trying to restore it causes a problem with non-String objects. Really, I want my extended JComboBox<E> to be uneditable if it contains non-String items. Ie. it ought to be editable when defined as JComboBox<String> but not JComboBox<Person>.

You can find one approach here. Note that the solution depends on marking your class abstract, and constructing concrete instances with a do-nothing anonymous extension.
 
K. Tsang
Bartender
Posts: 3633
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After googling a bit found this
 
Robbie Parks
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very interesting! I still seem to get stuck with the type returning only as "E", which isn't what I am looking for.

I've managed to get around it (mostly) for three out of four constructors:



This still leaves the default constructor JCBExtended() unhandled, however.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!