<Swing Dev> 6179357: Generics: JList: getSelectedValues

Alexander Potochkin Alexander.Potochkin at Sun.COM
Mon Jan 19 15:27:10 UTC 2009


Hello Tom

> but you can use the following workaround:
>>
>> @SuppressWarnings("unchecked")
>> E[] array = (E[])new Object[10];
> 
> Consider this client code:
> 
>     JList<String> list = ...;
>     String[] strs = list.getSelectedValues();
> 
> In that code you have a ClassCastException from Object[] to String[].
> 
> Unfortunately you can *not*, for obscure language reasons, even declare 
> JList<E>.getSelectedValues as:
> 
>     public <T super E> T[] getSelectedValues()
> 
> So, I think if you want the method genericisted, it needs to be 
> deprecated (or as near as we are allowed) and replaced. Although for my 
> money, you're always better off going for a model and leaving the actual 
> JComponent well alone.

I must agree here

The only robust solution would be to use a Class instance as a 
constructor to create the array,
e.g. as EventListenerList.getListeners(Class<T> t) does

however it will require huge API change so we'd better leave it as is

Thanks
alexp

> 
> Tom Hawtin




More information about the swing-dev mailing list