Tom
Tom

Reputation: 109

Typecast combobox in java

How do I typecast my defaultListModel? I want it to hold my objects of type "Account"

Do i need to create a new listmodel that extends listmodel or is there an easier way?

Upvotes: 2

Views: 166

Answers (2)

Thomas
Thomas

Reputation: 88727

As of Java 7 you should be able to do DefaultListModel<Account>.

If you're using Java 6 or below and dealing with Object is not an issue, you should be able to just put the Account instances into your DefaultListModel instance. IIRC for displaying Account should just have a reasonable toString() implementation.

Upvotes: 3

JB Nizet
JB Nizet

Reputation: 691845

Account is a subclass of Object, and DefaultListModel holds instances of Object, so there is no problem. You'll just have to cast the results of the methods (get, getElementAt, etc.) to Account:

Account a = (Account) listModel.getElementAt(i);

Upvotes: 3

Related Questions