<Swing Dev> 6179357: Generics: JList: Constructors & Model
Walter Laan
W.Laan at costengineering.eu
Wed Feb 25 09:24:12 UTC 2009
> -----Original Message-----
> From: swing-dev-bounces at openjdk.java.net [mailto:swing-dev-
> bounces at openjdk.java.net] On Behalf Of Alexander Potochkin
> To: Florian Brunner
> Subject: Re: <Swing Dev> 6179357: Generics: JList: Constructors &
Model
>
> Hello Florian
>
> I am sorry if I ask this question too late,
> but what is the main reason of generifiying these components?
>
> It is quite difficult to make it right
> because we have some difficult to generify public API.
>
> If JList and JComboBox are mostly used with Strings values
> there is an approach #3 - leave them as is
>
> Could you provide some test cases that prove
> the need of generifying those components?
>
> Thanks
> alexp
I don't use JList/ComboBox/Table mostly with Strings, typically they
contain domain objects with an appropriate renderer. I'm currently using
generic ListModels which also implement (most of) the java.util.List
interface. The generic ListTableModel is similar with an object per row.
I usually store references to both view and model and use indices to
retrieve values (with convenience methods on the model like List<T>
getAll(int[] indices) to use with int[] getSelectedIndices()).
Regarding using <E> or <? Extends E>, what I do there is to accept
List<? extends E> (not a model) in the constructor, but the model is
always a ListModel<E> to support mutability. See
http://pastebin.com/f49d4a9b7 for the code.
A reason I tried a generic JList was be to support generic drag and drop
between list, storing a Class<T> type so I could do:
If(type.isInstance(importData)) { model.add(type.cast(importData)); }
But I'm not sure that is enough reason to make the view generic, as the
drag and drop classes like DataFlavor, etc would need to be generic as
well.
Another reason is that it would be needed for generic renderers - it
basically comes down to that if you want to use generics, you have to go
all the way or you still end up casting somewhere.
So since you can use indices, only making the models generic might be an
easier transition (or as first step to make Swing generic).
Greetings,
Walter.
More information about the swing-dev
mailing list